#πŸ”’ How to create an in memory XLSX file and send it as bytes to the end user?

5 messages Β· Page 1 of 1 (latest)

pure kestrel
#

I am building a web app that allows users to download data as XLSX file. How to keep a spreadsheet in memory and send it as bytes.
I was trying to use openpyxl but I cannot cast bytes or str.

It's not that easy as CSV.

@get(
    "/download",
    response_headers={
        "content-disposition": 'inline; filename="file.csv"',
    },
    media_type="text/csv",
)
async def download_csv() -> bytes:
     data = [
        ["Name", "Age", "City"],
        ["Alice", 30, "New York"],
        ["Bob", 25, "London"],
        ["Charlie", 35, "Paris"]
    ]

    output = io.StringIO()
    writer = csv.writer(output)
    writer.writerows(data)
    output.seek(0)
    return output
stark shoreBOT
#

@pure kestrel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

pure kestrel
#

!solved

stark shoreBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.