I am creating a excel file using openpyxl which I would like to return as a file download (so not saving locally).
I can create the excel file fine and save it to disk. However, I cannot get this file to download.
Attempt 1:
import flask_excel as excel
...
create_excel_sheet(data) # internally save the sheet with name sheet.xlsx
output = excel.make_response()
output.headers["Content-Disposition"] = "attachment; filename=" + \
'sheet.xlsx'
output.headers["Content-type"] = "application/vnd.openxmlformats-\
officedocument.spreadsheetml.sheet"
return output
This returns an empty text file with name sheet.xlsx
Attempt 2: wb = create_excel_sheet(data) # return openpyxl workbook
output = excel.make_response(wb)
output.headers["Content-Disposition"] = "attachment; filename=" + \
'sheet.xlsx'
output.headers["Content-type"] = "application/vnd.openxmlformats-\
officedocument.spreadsheetml.sheet"
return output
I don't want to use pyexcel for the data as I need openpyxl to create a fancy excel sheet. Obviously if pyexcel and openpyxl communicated that would be fine.
any thoughts?
Cheers, Mike
Based on Charlie Clark's hint, I finally settled on the following solution.
output = make_response(create_sheet(data))
output.headers["Content-Disposition"] = "attachment; filename=" + \
"sheet.xlsx"
output.headers["Content-type"] = \
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
where
def create_sheet(data):
returns
return save_virtual_workbook(wb)