Django Pandas to http response (download file)

前端 未结 6 967
醉话见心
醉话见心 2020-12-29 06:36

Python: 2.7.11

Django: 1.9

Pandas: 0.17.1

How should I go about creating a potentially large xlsx file download? I\'m creating a xlsx file with panda

6条回答
  •  感动是毒
    2020-12-29 07:21

    With Pandas 0.17+ you can use a StringIO/BytesIO object as a filehandle to pd.ExcelWriter. For example:

    import pandas as pd
    import StringIO
    
    output = StringIO.StringIO()
    
    # Use the StringIO object as the filehandle.
    writer = pd.ExcelWriter(output, engine='xlsxwriter')
    
    # Write the data frame to the StringIO object.
    pd.DataFrame().to_excel(writer, sheet_name='Sheet1')
    writer.save()
    xlsx_data = output.getvalue()
    
    print len(xlsx_data)
    

    After that follow the XlsxWriter Python 2/3 HTTP examples.

    For older versions of Pandas you can use this workaround.

提交回复
热议问题