将PNG导入Excel并更改其高度和宽度。

huangapple go评论95阅读模式
英文:

Import png into Excel and change its height and width

问题

我使用以下代码将image.png文件导入到名为image_in_excel.xlsx的Excel工作表中,位置在单元格D5。我还尝试更改导入图像的宽度和高度。

问题
图像成功导入。但是,无论我在Python代码中设置了什么'width'和'height',导入的图像始终测量为高度=11.25"和宽度=22.01"在Excel中(请参见附图)。而且宽高比似乎被锁定。

问题
如何将PNG导入到xlsx文件的特定位置,然后更改导入照片的尺寸?

import xlsxwriter

# 创建一个新的Excel文件
workbook = xlsxwriter.Workbook('image_in_excel.xlsx')
worksheet = workbook.add_worksheet()

# 插入PNG图像
image_file = 'image.png'
worksheet.insert_image('D5', image_file, {'width': 1, 'height': 20})

# 保存Excel文件
workbook.close()

将PNG导入Excel并更改其高度和宽度。

英文:

I use the following code to import the image.png file into the excel sheet image_in_excel.xlsx at cell location D5. I also attempt to change the width and height of the imported image.

Issue:
The image imports successfully. However, irrespective of whatever I set the 'width' and 'height' in python code, the imported image always measures to be Height=11.25" and Width= 22.01" in Excel (see attached photo). Also the aspect ratio seems to be locked.

Question:
How to import png to xlsx file at a particular location and then change the imported photo's dimension?

import xlsxwriter

# Create a new Excel file
workbook = xlsxwriter.Workbook('image_in_excel.xlsx')
worksheet = workbook.add_worksheet()

# Insert the PNG image
image_file = 'image.png'
worksheet.insert_image('D5', image_file, {'width': 1, 'height': 20})

# Save the Excel file
workbook.close()

将PNG导入Excel并更改其高度和宽度。

答案1

得分: 1

根据 xlsxwriter 文档;https://xlsxwriter.readthedocs.io/worksheet.html?highlight=insert_image#worksheet-insert-image 更改宽度和高度的选项是 'x_scale' 和 'y_scale'。

worksheet.insert_image('D5', image_file, {'x_scale': 0.5, 'y_scale': 0.5})
英文:

Per the xlsxwriter documentation; https://xlsxwriter.readthedocs.io/worksheet.html?highlight=insert_image#worksheet-insert-image the option to change width and height are 'x_scale' and 'y_scale'.<br>

worksheet.insert_image(&#39;D5&#39;, image_file, {&#39;x_scale&#39;: 0.5, &#39;y_scale&#39;: 0.5})

huangapple
  • 本文由 发表于 2023年2月8日 08:33:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定