Hi I have a quick question. I didn't find answer in internet maybe someone of you can help me.
So i want to save workbook as attachment but I don't know how lets see an example :
from openpyxl import Workbook from openpyxl.cell import get_column_letter wb = Workbook(encoding='utf-8') dest_filename = 'file.xlsx' ws = wb.worksheets[0] ws.title = "range names" for col_idx in xrange(1, 40): col = get_column_letter(col_idx) for row in xrange(1, 600): ws.cell('%s%s'%(col, row)).value = '%s%s' % (col, row) ws = wb.create_sheet() ws.title = 'Pi' ws.cell('F5').value = 3.14
Then I tried :
response = HttpResponse(wb, content_type='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename="foo.xls"' return response
It's returning xlsx file indeed but in file there is only object adres not the content of file:
<openpyxl.workbook.Workbook object at 0x00000000042806D8>
Can someone help ?
Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files.
Is openpyxl safe to use? The python package openpyxl was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as safe to use.
If wb = openpyxl. Workbook() is calling wb. active , it gives the title of the default first worksheet, which is Sheet .
Give it a try:
from openpyxl.writer.excel import save_virtual_workbook ... response = HttpResponse(save_virtual_workbook(wb), content_type='application/vnd.ms-excel')
save_virtual_workbook
was specially designed for your use case. Here's a docstring:
"""Return an in-memory workbook, suitable for a Django response."""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With