I am looking to generate an Excel workbook based on the input from users in an HTML form. After the form is submitted, a Python script will generate the file and send it to the user. I am using xlsxwriter to create the spreadsheet.
I have been able to create a sample file and then provide a link to it, but I want to create the file in memory so there is no clean up.
My code creates a file and sends it to me but when I try to open it in Excel, I receive a message saying
Excel found unreadable content in 'sample.xlsx'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.
If i click yes, I get a blank sheet. If I open it in a text editor, I can see the file is not empty; the file is 6k in size. Do I need to change my content-type? Or make some other change?
#!/Temp/Python27/python
import io
from sys import stdout
import xlsxwriter
output = io.BytesIO()
workbook = xlsxwriter.Workbook(output)
worksheet = workbook.add_worksheet()
worksheet.write(1, 1, 'Hello, world!')
workbook.close()
contents = output.getvalue()
output.close()
print('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\nContent-Disposition:attachment; filename="sample.xlsx"\n')
print (contents)