You really don't need to save data anywhere on disk; Openpyxl has a way to do this, although this is not well documented. Once upon a time, I created something like this using xlwt, but recently I also created something similar in the Falcon environment using openpyxl.
Combining the two, your code will look something like this:
from django.http import HttpResponse from openpyxl import Workbook from openpyxl.writer.excel import save_virtual_workbook workbook = Workbook() worksheet = workbook.active
If you are creating larger files, I would recommend using StreamingHttpResponse, but I'm sure this will at least help you.
This is just a piece of code based on the merger of the two projects that I worked on, so it may not be entirely correct. It should be pretty close though. The output in Falcon looked like this:
response.content_type = 'application/octet-stream;' response.set_header('Content-Disposition', 'attachment; filename=myexport.xlsx') response.body = save_virtual_workbook(workbook)
UPDATE: Now this is much simpler since I completely rewrote my old django-excel-response library using openpyxl! Now it can be found here: https://github.com/tarkatronic/django-excel-response
You can install it with pip install django-excel-response and start using it as an alternative to Django HttpResponse ! Minimum documentation included, improvements / suggestions welcome. :)
Joey wilhelm
source share