Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
1 Beantwoorden
7374 Weergaven

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
Annuleer
Beste antwoord

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
Annuleer
Auteur

Thanks, @faOtools

Gerelateerde posts Antwoorden Weergaven Activiteit
1
mrt. 24
4454
1
feb. 24
3367
1
jun. 20
3744
2
sep. 21
5347
9
nov. 16
19808