3

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 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.

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

I added these lines after workbook.close()

Ahsan Attari
  • 987
  • 3
  • 12
  • 26
  • Can you do the reverse of [this](https://stackoverflow.com/questions/37405521/converting-a-binary-to-an-excel-file-python) and preface with [b](https://stackoverflow.com/questions/8908287/why-do-i-need-b-to-encode-a-python-string-with-base64) ? – QHarr Dec 27 '17 at 15:58
  • Don't know exactly, because its converting the text in file but my file is excel file. – Ahsan Attari Dec 27 '17 at 16:01
  • What happens if you save as a .xlsb ? – QHarr Dec 27 '17 at 16:03
  • I didn't tried and i cannot do this because this data will be sent as an attachment in email and i believe that `.xlsb` format is not supported every where/ – Ahsan Attari Dec 27 '17 at 16:06
  • did you find a solution to this problem? –  Jul 17 '18 at 11:43
  • anyone found solution ? – bakarin Sep 17 '18 at 09:23

0 Answers0