How to create and then write to xlsx file

I am new to python and odoo. I came across a script that I want to create an excel file first from my dynamic records, and then want to save it in the ir.attachment table. So that I can link this as an email application.

Here is what I tried but did not write to the file

  workbook = xlsxwriter.Workbook('demo.xlsx') worksheet = workbook.add_worksheet() worksheet.set_column('A:A', 20) bold = workbook.add_format({'bold': True}) worksheet.write('A1', 'Hello') worksheet.write('A2', 'World', bold) worksheet.write(2, 0, 123) worksheet.write(3, 0, 123.456) workbook.close() 

Refresh

I can create an xlsx file, actullay it was my way. Now I just want to know how to add this file to ir.attachment

+7
python excel openerp odoo-10
source share
2 answers

You can create the xlsx file dynamically and connect via email.

 from cStringIO import StringIO import base64 workbook = xlsxwriter.Workbook('demo.xlsx') worksheet = workbook.add_worksheet() worksheet.set_column('A:A', 20) bold = workbook.add_format({'bold': True}) worksheet.write('A1', 'Hello') worksheet.write('A2', 'World', bold) worksheet.write(2, 0, 123) worksheet.write(3, 0, 123.456) fp = StringIO() workbook.save(fp) fp.seek(0) datas = base64.encodestring(fp.read()) file_name = "name_%s" %(time.strftime('%Y%m%d%H%M%S.xlsx')) attachment=[] attachment_data = { 'name':file_name, 'datas_fname':file_name, 'datas':datas, 'res_model':"modelname", } attachment.append(self.env['ir.attachment'].create(attachment_data).id) mail_obj=self.env['mail.mail'] mail_template=self.env.ref('mail_template_id') msg_ids=mail_template.send_mail(id of object) msgs=mail_obj.browse(msg_ids) msgs.write({'attachment_ids': [(6, 0, attachment)]}) 

In the above code, we create one worksheet entry and then create an attachment entry.

You need to specify the name, datas_fname, datas, res_model to create the mount. You can also give res_id to create an attachment, after which the system will automatically see the visible attachment inside this model and record.

After creating the attachment, you can use it dynamically in the letter.

It might help you.

+5
source share

You can create your own Excel report using these modules: report_xls report_xlsx

There are modules that already use the report_xls module, but you can search for them in Odoo applications or in the OCA repository

Once you have a report, you can use them as other reports in PDF format

0
source share

All Articles