How to get a fjango file instance from pyexcelerate workbook?
I have recently checked the pyexcelerate
to help improve the performace of exporting a pandas data frame to excel file i have the following code
values = [my_df.columns] + list(my_df.values)
wb = Workbook()
wb.new_sheet('outputs', data=values)
wb.save('outputfile.xlsx')
I have a django model that has a filefield into it how can i save the generated wb
to a django file field ?
If you are using Django and pyexcelerate, you can return/download the file with the following:
Import:
from django.http import HttpResponse
In your Django views.py:
excel_filename = "example.xlsx"
response = HttpResponse(content_type='xlsx')
response['Content-Disposition'] = 'attachment;filename=' + excel_filename
workbook = Workbook()
sheet1 = workbook.new_sheet("data example", data=data)
workbook.save(response)
return response