英文:
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()
英文:
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()
答案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('D5', image_file, {'x_scale': 0.5, 'y_scale': 0.5})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论