Odoo image in excel

邮差的信 提交于 2020-01-06 07:11:07

问题


I'm creating an excel file with xlsxwriter and need to place my company logo into these excel file.. I've been trying with insert_image but not success. I suppose that is something like parse partner.image into a buffer... but im stuck.. Pleace your help. this is my code.

  @api.multi
def report_print(self):
    output=io.BytesIO()
    book=xlsxwriter.Workbook(output)

    sheet1=book.add_worksheet("PCA")
    sheet1.write('A1','PCA')
    #=======================================================================
    # Looking for partner data
    #=======================================================================
    user=self.env['res.users'].browse(self.env.uid)
    partner = self.env['res.partner'].browse(user.company_id.id)

    #copy partner name in B1
    partner_name = partner.name
    sheet1.write("B1",partner_name) 


    #put partner logo in B3
    buf_image=io.BytesIO(partner.image)
    sheet1.insert_image('B3',base64.b64encode(buf_image.getvalue()),{'image_data': buf_image})

    book.close()

    self.write({
        'file':base64.b64encode(output.getvalue())})

回答1:


In Odoo v11 I use :

buf_image=io.BytesIO(base64.b64decode(partner.image))
sheet1.insert_image('B3', "any_name.png", {'image_data': buf_image})



回答2:


this is the format for adding images in worksheet

import xlsxwriter


# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('images.xlsx')
worksheet = workbook.add_worksheet()

# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 30)

# Insert an image.
worksheet.write('A2', 'Insert an image in a cell:')
worksheet.insert_image('B2', 'python.png')

# Insert an image offset in the cell.
worksheet.write('A12', 'Insert an image with an offset:')
worksheet.insert_image('B12', 'python.png', {'x_offset': 15, 'y_offset': 10})

# Insert an image with scaling.
worksheet.write('A23', 'Insert a scaled image:')
worksheet.insert_image('B23', 'python.png', {'x_scale': 0.5, 'y_scale': 0.5})

workbook.close()

In case of a stored image in Odoo look here an example using openpyxl, use the same format.

from openpyxl import Workbook
from openpyxl.writer.excel import ExcelWriter
from openpyxl.drawing import Image

from PIL import Image as PILImage
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

wb = Workbook()
ws = wb.get_active_sheet()

#extra has the data of the image from the database
im = PILImage.open(StringIO(extra))
img = Image(im)
img.anchor(ws.cell('F1'))
ws.add_image(img)

handler = StringIO()
writer = ExcelWriter(wb)
writer.save(handler)
xls = handler.getvalue()
handler.close()



回答3:


Finaly did it with openpyxl

    @api.multi
    def report_print(self):
        user=self.env['res.users'].browse(self.env.uid)
        partner = self.env['res.partner'].browse(user.company_id.id)

        partner_name = partner.name

        wb = Workbook()
        ws = wb.get_active_sheet()

        binaryData=partner.image_medium
        data=base64.b64decode(binaryData)

        im = PILImage.open(BytesIO(data))
        img = OPYImage(im)
        ws.add_image(img, "A3")
        width, height = im.size

        #=======================================================================
        # more code             
        #=======================================================================

        output=BytesIO()
        wb.save(output)

        self.write({

            'file':base64.b64encode(output.getvalue()),
            'file_name':'my_file_name_.xlsx'
            })
        wb.close()
        output.close()

It works in Odoo 11 and Python 3



来源:https://stackoverflow.com/questions/50811051/odoo-image-in-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!