Search code examples
pythondjangoexcelxlsxlwt

How to generate an excel file and save it directly to a path in Django


I am new to Django and I was wondering if the following is possible:

Now, in my django app(not admin page) I create xls files using the xlwt library. Pushing a button the user choose where he wants to save the generated file, but isn't what I want.

The ideal for me is when the user clicks on the "Save Excel" button then I want the file to be generated and directly be saved in a specific path in the server or in the database(via FileField), not somewhere in the user's pc. Without asking the user .

Thanks in advance.


Solution

  • You can just specify the path as a string argument in save, as you do.

    when clicked:
        from xlwt import Workbook
        wb = Workbook()
        sheet1 = wb.add_sheet("Sheet 1")
        wb.save("/tmp/workbook.xls")