Odoo Image In Excel
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
Solution 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})
Solution 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 importWorkbookfrom openpyxl.writer.excelimportExcelWriterfrom openpyxl.drawingimportImagefromPILimportImageasPILImagetry:
from cStringIO importStringIO
except ImportError:
fromStringIO importStringIO
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()
Solution 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
Post a Comment for "Odoo Image In Excel"