Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
11429 Lượt xem

I am working on Odoo 10 and I am working on creating xlsx file and i am almost done because i created file successfully but the next i want to do is to add the currently created file into database table `ir.attachment` and my column requires bytea data so i need to convert the file in binary data. Which i tried in last line but its giving me error AttributeError: 'Workbook' object has no attribute 'read'. Below is the code from my function which i am using/

   file_name = 'test'
    workbook = xlsxwriter.Workbook(file_name)
    bold = workbook.add_format({'bold': True})  # Add a bold format to use to highlight cells.
    row_count = 2
    for customer in customers:
        customer_orders = self.env['amgl.order_line'].search([('customer_id', '=', customer.id)])
        column_count = 0
        for customer_order in customer_orders:
            worksheet.write(row_count, column_count, 'GST_Customer_account_number')
            column_count += 1
            worksheet.write(row_count, column_count, customer.full_name)
            column_count += 1
            worksheet.write(row_count, column_count, customer.account_number)
            column_count += 1
            worksheet.write(row_count, column_count, customer_order.create_date)
            column_count += 1
            worksheet.write(row_count, column_count, 'PR')
            column_count += 1
            worksheet.write(row_count, column_count, customer_order.products.product_code)
            column_count += 1
            worksheet.write(row_count, column_count, customer_order.quantity)
            column_count += 1
            worksheet.write(row_count, column_count, 'amark')
            column_count += 1
            worksheet.write(row_count, column_count, 'amark_customer_code')
            column_count += 1
            worksheet.write(row_count, column_count, 'Descripion')
            column_count += 1
            worksheet.write(row_count, column_count, 'transaction_number')
            row_count += 1

    workbook.close()
    data = 0
    base64.encode(workbook, data)

Update

 I tried to do followings and its making my encoded.But now i face issue that whenever i sent that record as an attachment. The file shows me the binary not actual text.

Here is what i tried.


with open(file_name, "rb") as xlfile:
byte_data = xlfile.read()

test_id = self.env['ir.attachment'].create({'name': file_name,
'datas': byte_data,
'datas_fname': file_name,
'res_model': 'res.users',
'res_id': 1, })
template = self.env.ref('amgl.mmr_approval_complete', raise_if_not_found=True)
mail_id = template.with_context(mmr_name='Export Report').send_mail(
1,
force_send=True,
raise_exception=True,
email_values={'attachment_ids': [test_id.id]}
)

I added these lines after workbook.close()


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi AandB,

You can try this way,

Thank you.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

there is an another option like you can create a mail with report using the template. try to add report file in template and it will automatically add attachments with mail.


Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 10 17
4748
1
thg 10 17
19992
3
thg 10 24
18113
11
thg 4 23
28821
5
thg 10 24
29165