I am in the middle of a reporting function which will break up reports into different workbooks then zip the files and send them out to users.
At this time, I get a successful book for the second iteration, but the first iteration seems to be overridden/lost when the bytesIO object reinitializes on subsequent iterations.
def runPullReports():
sharedOrderFxs.sendEmail([], 'Starting..', "Running")
start = datetime.date(2022, 7, 7) #now
start += datetime.timedelta(2)#running sunday means we've already run tuesday labels
dateList = [start + datetime.timedelta(days=x) for x in range(4)]
food_programs = list(refModels.FoodProgram.objects.all().values_list('id', flat=True))
pack_list = ['HOT PACK', 'COLD PACK']
workBookList = []
SA = refModels.ServiceArea.objects.all()
for service_area in SA:
fileName = str(service_area.name) + '.xlsx'
output = BytesIO()
book = Workbook(output, {'in_memory': True})
## Other Processing here irrelevant to the question
book.close()
output.seek(0)
# resetting BytesIO() buffer before we add workbooks to the list, since we're using BytesIO again in the generate_zip function
workBookList.append((fileName, output))
archive = generate_zip(workBookList)
dateRange = f'{dateList[0].month}-{dateList[0].day}_{dateList[-1]}'
#email send fnx sending archive.
The Zip/Email function:
def generate_zip(files):
zip_buffer = BytesIO()
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file:
for fName, file in files:
zip_file.writestr(fName, file.read())
zip_file.close()
zip_buffer.seek(0)
return zip_buffer.getvalue()