[Django]-Use pyExcelerator to generate dynamic Excel file with Django. Ensure unique temporary filename

11👍

pyExcelerator is unmaintained, but it has a fork, xlwt, which is maintained and has more features, including allowing you to save to any file-like object. This includes saving straight to a Django HttpResponse:

from django.http import HttpResponse
import xlwt

def my_view(request):
    response = HttpResponse(mimetype="application/ms-excel")
    response['Content-Disposition'] = 'attachment; filename="foo.xls"'
    wb = xlwt.Workbook()
    wb.save(response)
    return response

3👍

Why can you not use the tempfile module?

How about:

import tempfile
fd, filename = tempfile.mkstemp()
fd.close()
workbook.save(filename)

Leave a comment