title: Python 3: Download OpenPYXL XLSX file
body: <p>Via a simple Python 3 CGI App, I'm wanting to generate an XLSX file (using OpenPYXL), save it in memory, and force a download of the file to a browser.  The test code that I'm using is as follows:</p>
  <pre><code>#!/usr/bin/env python
  
  import os,sys
  import io
  import openpyxl
  
  print(&quot;Content-Type: application/octet-stream; name = test.xlsx&quot;)
  print(&quot;Content-Disposition: attachment; filename = test.xlsx\n&quot;)
  
  wb = openpyxl.Workbook()
  ws = wb.active
  ws['A1'] = &quot;hello&quot;
  ws['A2'] = &quot;there!&quot;
  
  output = io.BytesIO()
  wb.save(output)
  print(output.getvalue())
  </code></pre>
  <p>The resulting file on disk starts as follows:</p>
  <pre><code>b'PK\x03\x04\x14\x00\x00\x00\x08\x00Tu\xd0T\x07AMb....
  </code></pre>
  <p>XLS files are ZIP compressed - and this looks like a segment of a compressed file.  The problem is that I'm not sure how to write it out as a binary file that Excel will be able to open.</p>
  <p>I have tried:</p>
  <pre><code>print(output.getvalue().decode('utf-8'))
  </code></pre>
  <p>The problem is that I end up with:</p>
  <pre><code>UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 10: invalid continuation byte
  </code></pre>
  <p>because this isn't a UTF-8 file - XLSX documents are compressed files.</p>
  <p>Using other codecs like raw_unicode_escape and unicode_escape look <strong>closer</strong> to the expected format - but they are still corrupt when trying to open it in Excel (or extract via unzip).</p>
  <p>Can anyone point me in the right direction, as to how to create a file that can be correctly downloaded and opened via Excel.</p>