Django Pandas to http response (download file) Django Pandas to http response (download file) pandas pandas

Django Pandas to http response (download file)


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 pdfrom django.http import HttpResponsetry:    from io import BytesIO as IO # for modern pythonexcept ImportError:    from io import StringIO as IO # for legacy python# this is my output data a list of listsoutput = some_function()df_output = pd.DataFrame(output)# my "Excel" file, which is an in-memory output file (buffer) # for the new workbookexcel_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 Excelexcel_file.seek(0)# set the mime type so that the browser knows what to do with the fileresponse = HttpResponse(excel_file.read(), content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')# set the file name in the Content-Disposition headerresponse['Content-Disposition'] = 'attachment; filename=myfile.xlsx'return response


Jmcnamara is pointing you in the rigth direction. Translated to your question you are looking for the following code:

sio = StringIO()PandasDataFrame = pandas.DataFrame(self.csvdict)PandasWriter = pandas.ExcelWriter(sio, engine='xlsxwriter')PandasDataFrame.to_excel(PandasWriter, sheet_name=sheetname)PandasWriter.save()sio.seek(0)workbook = sio.getvalue()response = StreamingHttpResponse(workbook, content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')response['Content-Disposition'] = 'attachment; filename=%s' % filename

Notice the fact that you are saving the data to the StringIO variable and not to a file location. This way you prevent the file being saved before you generate the response.


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

import pandas as pdimport StringIOoutput = 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.