Serving Excel 2007 file generated by Python
<p>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. </p>

<p>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. </p>

<p>My code creates a file and sends it to me but when I try to open it in Excel, I receive a message saying</p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>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?</p>

<pre><code>#!/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)
</code></pre>

<p>I continued searching after I posted my question and found the answer <a href="http://stackoverflow.com/questions/2374427/python-2-x-write-binary-output-to-stdout">here</a></p>

python python-2.7 mime-types xlsxwriter