I am generating an excel file and returning it for download in a flask app. If I use 'in_memory': True
then the file is corrupt and excel can't open it. The same code if written directly to disk works. For the in-memory code I'm following this example from the XlsxWriter docs.
I was skeptical that the bytes were different, so I wrote a small script to test, and they do differ slightly (44 bytes out of 5730).
Here's my script generating identical workbooks, one in memory and one on disk. It then compares the bytes and finds that they are different. Why?
from io import BytesIO
from xlsxwriter import Workbook
def fill_workbook(workbook):
"""Populate the workbook with some test data"""
first_sheet = workbook.add_worksheet("First")
first_sheet.write(0, 0, "test")
next_sheet = workbook.add_worksheet("Next")
next_sheet.write(0, 0, "sample")
next_sheet.write(0, 1, "value")
workbook.close()
def get_bytes():
"""Get the bytes for the in-memory and on-disk workbooks"""
output = BytesIO()
in_mem = Workbook(output, {'in_memory': True})
filename = "direct.xlsx"
on_disk = Workbook(filename)
fill_workbook(in_mem)
fill_workbook(on_disk)
output.seek(0)
mem_bytes = output.read()
with open(filename, "rb") as f:
disk_bytes = f.read()
return mem_bytes, disk_bytes
def compare_bytes():
"""Compare the bytes of the two workbooks"""
mem_bytes, disk_bytes = get_bytes()
print(mem_bytes == disk_bytes)
same = 0
diff = 0
for mb, db in zip(mem_bytes, disk_bytes):
if mb == db:
same += 1
else:
diff +=1
print(f"{same} bytes same")
print(f"{diff} bytes different")
if __name__ == '__main__':
compare_bytes()
I ran my script on Python 3.7.3
with XlsxWriter==1.2.8