[Python] Python 3: Download OpenPYXL XLSX file

Discussão em 'Python' iniciado por Stack, Junho 11, 2025.

  1. Stack

    Stack Membro Participativo

    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:

    #!/usr/bin/env python

    import os,sys
    import io
    import openpyxl

    print("Content-Type: application/octet-stream; name = test.xlsx")
    print("Content-Disposition: attachment; filename = test.xlsx\n")

    wb = openpyxl.Workbook()
    ws = wb.active
    ws['A1'] = "hello"
    ws['A2'] = "there!"

    output = io.BytesIO()
    wb.save(output)
    print(output.getvalue())


    The resulting file on disk starts as follows:

    b'PK\x03\x04\x14\x00\x00\x00\x08\x00Tu\xd0T\x07AMb....


    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.

    I have tried:

    print(output.getvalue().decode('utf-8'))


    The problem is that I end up with:

    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 10: invalid continuation byte


    because this isn't a UTF-8 file - XLSX documents are compressed files.

    Using other codecs like raw_unicode_escape and unicode_escape look closer to the expected format - but they are still corrupt when trying to open it in Excel (or extract via unzip).

    Can anyone point me in the right direction, as to how to create a file that can be correctly downloaded and opened via Excel.

    Continue reading...

Compartilhe esta Página