将图像导出为ex cel

问题描述 投票:0回答:3

我正在使用xlsxwriter创建一个excel文件,并且需要将我的公司徽标放入这些excel文件中。我一直在尝试使用insert_image而不是成功。我想这就像解析了一个伙伴。图像进入缓冲区...但我卡住了...请尽快帮助。这是我的代码。

  @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())})
python excel image odoo xlsxwriter
3个回答
2
投票

在Odoo v11中我使用:

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

2
投票

这是在工作表中添加图像的格式

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()

如果在Odoo中存储图像,请在此处查看使用openpyxl的示例,请使用相同的格式。

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()

0
投票

最后用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()

它适用于Odoo 11和Python 3

© www.soinside.com 2019 - 2024. All rights reserved.