提问人:Gilseung Ahn 提问时间:11/11/2023 更新时间:11/11/2023 访问量:33
如何在openpyxl中提高图像分辨率?
How to increase image resolution in openpyxl?
问:
我有一些高分辨率的图像,但是当使用 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)
问题是什么以及如何解决?
答:
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
谢谢你的回答。但是我的真实代码已经考虑了图像的单位(厘米、点等)。
评论