如何在openpyxl中提高图像分辨率?

How to increase image resolution in openpyxl?

提问人:Gilseung Ahn 提问时间:11/11/2023 更新时间:11/11/2023 访问量:33

问:

我有一些高分辨率的图像,但是当使用 openpyxl 将它们添加到 excel 时,它们的分辨率会变低。
我的示例代码在这里。

wb = openpyxl.Workbook()
ws = wb.active

img = openpyxl.drawing.image.Image('high_resolution_image.png')
img.anchor = AbsoluteAnchor(pos=position, ext=size)
ws.add_image(img)

问题是什么以及如何解决?

python excel openpyxl 解析

评论

0赞 moken 11/12/2023
您使用的是哪个 Excel 版本。如果 2016 年或之后设置为高保真图像?
0赞 Charlie Clark 11/14/2023
您需要自己创建将图像定位在工作表上的定位点。

答:

0赞 aazizzailani 11/11/2023 #1
from openpyxl.drawing.image import Image

wb = openpyxl.Workbook()
ws = wb.active

img = Image('high_resolution_image.png')
img.width = img.width * 2.54  # Convert width to points (1 inch = 2.54 cm)
img.height = img.height * 2.54  # Convert height to points

ws.add_image(img, 'A1')

wb.save('output.xlsx')

此代码将图像宽度和高度乘以 2.54,以将它们从厘米转换为磅。根据您的具体情况调整这些值。

评论

0赞 Gilseung Ahn 11/11/2023
谢谢你的回答。但是我的真实代码已经考虑了图像的单位(厘米、点等)。