可以使用编程方式将matplotlib图形插入到Excel中吗? [英] Can I insert matplotlib graphs into Excel programmatically?
问题描述
openpyxl似乎不支持图像嵌入。 xlwt只是bmp。
或者,如果我可以以编程方式将tiff转换为bmp,那也可能有帮助。
欢迎您的想法。
类似于
然而,从tiff转换为bmp是可以接受的我的图形体积很小(每个文件约10个)。
这是我从网络上的两个不同链接中找到的,对我来说完美无瑕。 Matplotlib允许保存png文件,这是我在这里使用的:
从PIL导入图像
file_in =image.png
img = Image.open(file_in)
file_out ='test1.bmp'
print len(img.split())#test
if len(img.split())== 4:
#防止IOError:无法将模式RGBA写为BMP
r,g,b,a = img.split()
img = Image。合并(RGB,(r,g,b))
img.save(file_out)
else:
img.save(file_out)
from xlwt import Workbook
w = Workbook()
ws = w.add_sheet('Image')
ws.insert_bitmap(file_out,0,0)
w.save('images.xls ')
代码的图像部分来自Ene Urans这里的响应 http://www.daniweb。 com / software-development / python / threads / 253957 / conversion-an-image-file-png-to-a-bitmap-file 。
xlwt只是形成了我在 http://www.simplistix.co.uk/presentations/python-excel.pdf 。
I am saving matplotlib files as .tiff images. I'd like to be able to then open an excel file and paste the image there.
openpyxl doesnot seem to support image embedding. xlwt does but only bmp.
ALternatively if i can programmatically convert tiff to bmp, that might help also.
Ideas on either are welcome.
Similar to
Embed multiple jpeg images into EXCEL programmatically?
However converting from tiff to bmp is acceptable as my volume of graphs is small (approximately 10 per file).
Here is what I found from two different links on the web, that worked perfectly for me. Matplotlib allows saving png files which is what I make use of here:
from PIL import Image
file_in = "image.png"
img = Image.open(file_in)
file_out = 'test1.bmp'
print len(img.split()) # test
if len(img.split()) == 4:
# prevent IOError: cannot write mode RGBA as BMP
r, g, b, a = img.split()
img = Image.merge("RGB", (r, g, b))
img.save(file_out)
else:
img.save(file_out)
from xlwt import Workbook
w = Workbook()
ws = w.add_sheet('Image')
ws.insert_bitmap(file_out, 0, 0)
w.save('images.xls')
The image part of the code is from Ene Urans response here http://www.daniweb.com/software-development/python/threads/253957/converting-an-image-file-png-to-a-bitmap-file.
The xlwt is simply form the documentation of xlwt I found at http://www.simplistix.co.uk/presentations/python-excel.pdf.
这篇关于可以使用编程方式将matplotlib图形插入到Excel中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!