Python 我可以以编程方式将 matplotlib 图形插入 Excel 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15177705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I insert matplotlib graphs into Excel programmatically?
提问by pythOnometrist
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.
我将 matplotlib 文件保存为 .tiff 图像。我希望能够然后打开一个 excel 文件并将图像粘贴到那里。
openpyxl doesnot seem to support image embedding. xlwt does but only bmp.
openpyxl 似乎不支持图像嵌入。xlwt 可以,但只有 bmp。
ALternatively if i can programmatically convert tiff to bmp, that might help also.
或者,如果我可以以编程方式将 tiff 转换为 bmp,那也可能有所帮助。
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).
但是,从 tiff 转换为 bmp 是可以接受的,因为我的图形量很小(每个文件大约 10 个)。
采纳答案by pythOnometrist
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:
这是我从网络上的两个不同链接中发现的,这对我来说非常有效。Matplotlib 允许保存 png 文件,这是我在这里使用的:
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.
代码的图像部分来自 Ene Urans 在这里的回复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.
xlwt 只是我在http://www.simplistix.co.uk/presentations/python-excel.pdf 上找到的 xlwt 文档的简单形式。
回答by Sam Goral
Openpyxl actually does support image embedding, and might work better for those using .png or existing .xlsx files! The code below appends an image to cell A1 of input.xlsx and saves the file as output.xlsx.
Openpyxl 实际上确实支持图像嵌入,对于那些使用 .png 或现有 .xlsx 文件的人来说可能会更好!下面的代码将图像附加到 input.xlsx 的单元格 A1 并将文件保存为 output.xlsx。
import matplotlib.pyplot as plt
import openpyxl
# Your plot generation code here...
plt.savefig("myplot.png", dpi = 150)
wb = openpyxl.load_workbook('input.xlsx')
ws = wb.active
img = openpyxl.drawing.Image('myplot.png')
img.anchor(ws.cell('A1'))
ws.add_image(img)
wb.save('output.xlsx')
回答by Carlos Bettin
This worked out for me:
这对我有用:
import openpyxl
wb = openpyxl.load_workbook('input.xlsx')
ws = wb.active
img = openpyxl.drawing.image.Image('myplot.png')
ws.add_image(ws.cell('A1'))
ws.save('output.xlsx')

