我正在使用Odoo10。并且我正在尝试使用xlwt库将徽标图像插入到Excel工作表中。我从以下链接尝试了该解决方案,但对我而言不起作用。
Insert an image base 64 on excel using xlwt
我的代码:
from PIL import Image
img = Image.open("/my_module/static/description/logo.png")
r, g, b, a = img.split()
img = Image.merge("RGB", (r, g, b))
img.save('imagetoadd.bmp')
xlwt.insert_bitmap('imagetoadd.bmp', 0, 0)
错误: IOError:[Errno 2]没有这样的文件或目录:'/my_module/static/description/logo.png'
我已经仔细检查了我的路径并且拼写正确,并且我也重新启动了服务器,也进行了升级。
当我给出这样的完整路径时: / home /用户名/文件夹/custom-addons/my_module/static/description/logo.png 我收到以下错误: ValueError:需要超过3个值才能解包
请帮助我如何使用xlwt将图像插入Excel工作表。谢谢!
答案 0 :(得分:0)
由于您在ValueError: need more than 3 values to unpack
到4个变量img.split()
的情况下unpacking
返回的元组少于4个,因此可能发生了r g b a
错误。检查img.split()
返回的长度并相应地分配给变量,或使用索引来访问前3个元素,例如
image_parts = img.split()
r = image_parts[0]
g = image_parts[1]
b = image_parts[2]
更新:
from PIL import Image
import xlsxwriter
from io import BytesIO
workbook = xlsxwriter.Workbook('filename.xlsx')
worksheet1 = workbook.add_worksheet()
img = Image.open("/home/username/folder/custom-addons/my_module/static/description/logo.png")
image_parts = img.split()
r = image_parts[0]
g = image_parts[1]
b = image_parts[2]
img = Image.merge("RGB", (r, g, b))
fo = BytesIO()
img.save(fo, format='bmp')
worksheet1.insert_image('A1', 'python.png', {'image_data': fi})
workbook.close()
好吧,我在xlwt
库的源代码中找到了所需的功能。
from PIL import Image
import xlwt
from io import BytesIO
workbook = xlwt.Workbook()
worksheet1 = workbook.add_sheet('Test')
img = Image.open("/home/username/folder/custom-addons/my_module/static/description/logo.png")
image_parts = img.split()
r = image_parts[0]
g = image_parts[1]
b = image_parts[2]
img = Image.merge("RGB", (r, g, b))
fo = BytesIO()
img.save(fo, format='bmp')
worksheet1.insert_bitmap_data(fo.getvalue(),0,0)
workbook.save('filename.xls')
img.close()