This question has been flagged
1 Reply
10575 Views

Hello,

I've created xls report from wizard.Now I want to add my signature image of parent company that is stored in database. I've tried below code , but I get output as a string for that field instead of Image.I've already checked resources from net to include image using Workbook, but didn't really much helpful as needed.

wizard/bank_report.py

...
...
def print_bank_report(self, cr, uid, ids, context=None):
employee_obj = self.pool.get('hr.employee')
payslip_obj = self.pool.get('hr.payslip')
payslip_line_obj = self.pool.get('hr.payslip.line')
wiz_obj=self.browse(cr,uid,ids)

amount = 0.0
total_amount = 0.0
total_employee = employee_obj.search(cr,uid,[('company_id','=',wiz_obj.company_id.id)],context=context)
total_payslip = payslip_obj.search(cr,uid,[('date_from','=',wiz_obj.from_date),('date_to','=',wiz_obj.to_date)],context=context)

sig = wiz_obj.company_id.parent_id.signature_image  

res1 = []
res2 = []
for employee in employee_obj.browse(cr, uid, total_employee, context=context):
for payslip in payslip_obj.browse(cr, uid, total_payslip, context=context):
if employee.id == payslip.employee_id.id:
if employee.bank_name.name == 'SBOP':
print employee.bank_name.name
total_lines = payslip_line_obj.search(cr,uid,[('slip_id','=',payslip.id),('code','=','NET')])
for lines in payslip_line_obj.browse(cr,uid,total_lines):
amount = lines.amount
total_amount += lines.amount
if employee.bank_name.name == 'SBOP':
res1.append ({
'employee':employee.name,
'account_number':employee.bank_ac,
'amount':amount,
'total_amount':total_amount,
'remark': employee.company_id.name,
'ifsc_code': employee.ifsc_code,
'sig':sig,
})

...
..

Here, In .py file, I've created 'res1' then added successfully this into worksheet using workbook = xlwt.Workbook().

In output of sig field, I got too long String in .xls file. How can I get image in .xls file? Or suggest me different possibilities to decode this string into other image format?!



Avatar
Discard
Best Answer

Hi @PujaBaa Zala

From what I know from trying to do the same you have 2 choices here:

1- You could always store the image data in a temp file in order to be used in your xlwt code.

2- You can use openpyxl as the report excel generator, I successfully done what you need only with that library. I used specifically 1.7.0, I don't know if latter versions remove that feature.

==========================================

Example of using openpyxl with an in-memory image data

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()
Avatar
Discard
Author

Hello @Axel Mendoza , Thanx for suggesting openpyxl ! I've read about Inserting an image using openpyxl, But How can I use it to add image that is stored in database. can you pls help me how to use it?

I edited my answer to include an example

Author

@Axel Mendoza : thanx a lot. I'll check it

could you accept the answer on the check mark?