在openpyxl中插入图像 [英] Insert image in openpyxl

查看:613
本文介绍了在openpyxl中插入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用openpyxl插入图像(jpeg,png等)?

Is it possible to insert an image (jpeg, png, etc) using openpyxl?

基本上,我想在生成的图像下方放置一个图表.

Basically I want to place a generated image with a chart below it.

我在文档中没有看到任何内容,与代码的成熟度相比似乎有些不足.

I don't see anything in the documentation, which seems to be a little lacking compared to the maturity of the code.

推荐答案

以下代码将图像插入单元格A1中.调整图像位置以满足您的需要,或者自己处理PIL图像的创建并将其交给Image()

The following inserts an image in cell A1. Adjust the image location to your needs or handle the creation of the PIL image yourself and hand that to Image()

import openpyxl

wb = openpyxl.Workbook()
ws = wb.worksheets[0]
img = openpyxl.drawing.image.Image('test.jpg')
img.anchor = 'A1'
ws.add_image(img)
wb.save('out.xlsx')

在较旧的openpyxl版本中,可以进行以下操作:

In older versions of openpyxl the following works:

import openpyxl

wb = openpyxl.Workbook()
ws = wb.worksheets[0]
img = openpyxl.drawing.Image('test.jpg')
img.anchor(ws.cell('A1'))
ws.add_image(img)
wb.save('out.xlsx')

这篇关于在openpyxl中插入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆