Skip to Content
Menu
This question has been flagged
1 Reply
6776 Views

Dear All,

I can create and download reports xlsx format using xlsxwriter. I need to convert xlsx file to zip and download. with the below code, i can create abc.zip file with excel, but the excel file contain base64 raw data.(same as file_download). Any help is highly appreciated.


# create xlsx file    
fp = BytesIO()
workbook = xlsxwriter.Workbook(fp)

....MycodeHere....
....MycodeHere....

workbook.allow_zip64
workbook.close()
file_download = base64.b64encode(fp.getvalue())
fp.close()

# create Zip File
pyzip = PyZip()
pyzip['excel.xls'] = file_download
zip_bytes = pyzip.to_bytes()


# create attachment
attachment_obj = self.env['ir.attachment']
attachment_id = attachment_obj.create(
{'name': "name", 'datas_fname': 'abc.zip', 'datas': zip_bytes})
att_ids.append(attachment_id.id)


# prepare download url
download_url = '/web/content/' + str(attachment_id.id) + '?download=true'


# download
return {
"type": "ir.actions.act_url",
"url": str(download_url),
"target": "new",
}


Avatar
Discard
Best Answer

Use the python library zipfile for that: https://docs.python.org/3/library/zipfile.html

An example in Odoo base: https://github.com/odoo/odoo/blob/14.0/odoo/tools/osutil.py - the method zip_dir

If you have an access for the enterprise Odoo code - a clearer example: have a look  at enterprise/controllers/main.py the method _make_zip



Avatar
Discard
Author

Thanks, @faOtools

Related Posts Replies Views Activity
1
Mar 24
3634
1
Feb 24
2765
1
Jun 20
3264
2
Sep 21
4798
9
Nov 16
19808