Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

2

Add Signature Image (stored in database) to excel file

By
PujaBaa Zala
on 9/25/15, 6:34 AM 1,076 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?!



2

Axel Mendoza

--Axel Mendoza--
10203
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 9/25/15, 8:48 AM

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

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?

PujaBaa Zala
on 9/26/15, 1:13 AM

I edited my answer to include an example

Axel Mendoza
on 9/26/15, 1:48 AM

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

PujaBaa Zala
on 9/26/15, 5:59 AM

could you accept the answer on the check mark?

Axel Mendoza
on 12/23/15, 2:41 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 9/25/15, 6:34 AM
Seen: 1076 times
Last updated: 5/31/16, 8:31 AM