Django Pandas to http response (download file)

前端 未结 6 966
醉话见心
醉话见心 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:19

    I will elaborate on what @jmcnamara wrote. This if for the latest versions of Excel, Pandas and Django. The import statements would be at the top of your views.py and the remaining code could be in a view:

    import pandas as pd
    from django.http import HttpResponse
    try:
        from io import BytesIO as IO # for modern python
    except ImportError:
        from io import StringIO as IO # for legacy python
    
    # this is my output data a list of lists
    output = some_function()
    df_output = pd.DataFrame(output)
    
    # my "Excel" file, which is an in-memory output file (buffer) 
    # for the new workbook
    excel_file = IO()
    
    xlwriter = pd.ExcelWriter(excel_file, engine='xlsxwriter')
    
    df_output.to_excel(xlwriter, 'sheetname')
    
    xlwriter.save()
    xlwriter.close()
    
    # important step, rewind the buffer or when it is read() you'll get nothing
    # but an error message when you try to open your zero length file in Excel
    excel_file.seek(0)
    
    # set the mime type so that the browser knows what to do with the file
    response = HttpResponse(excel_file.read(), content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    
    # set the file name in the Content-Disposition header
    response['Content-Disposition'] = 'attachment; filename=myfile.xlsx'
    
    return response
    

提交回复
热议问题