8

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

Here is what i tried, but its not writing into 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()

Update

I am able to create the xlsx file, actullay it was my path issue. Now i just want to know that how to add that file in ir.attachment

Ancient
  • 3,007
  • 14
  • 55
  • 104
  • What do you mean by "it's not writing into the file"? Is the file being created but not populated? Also, you seem to be using imports that are not provided here. Please provide the full code for your [mcve] – asongtoruin Dec 19 '17 at 17:30
  • sorry, updating my question in a bit – Ancient Dec 19 '17 at 18:21
  • @Ancient, I've run your code without any problems, as is, and I can see the xlsx file as intended. Do you still have a problem? – Eugene Dec 19 '17 at 22:29
  • @Eugene see mu update – Ancient Dec 21 '17 at 14:24
  • @Ancient, You may try to convert .xlsx file into binary and then pass it, create `ir.attachment` record. For example: `{'datas': binary_file_output}` and you need to pass other parameters which are required to create `ir.attachment` – Bhavesh Odedra Dec 27 '17 at 06:49
  • why can't you try with pandas library – Midhun Mohan Dec 29 '17 at 03:02

2 Answers2

9

You can create xlsx file dynamically and attach 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 above code we have create one worksheet record and after that create attachment record.

You need to give name,datas_fname,datas,res_model to create attachment. you can also give res_id to create attachment, after that system will automatic visible attachment inside that model and record.

After create attachment you can use dynamically in the email.

This may help you.

0

You can build your own excel report with the help of these modules: report_xls report_xlsx

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

Once you have the report you can use them as the rest of the PDF reports

ChesuCR
  • 9,352
  • 5
  • 51
  • 114