Python 通过工作表插入图像时设置图像的宽度和高度。插入\u图像

Python 通过工作表插入图像时设置图像的宽度和高度。插入\u图像,python,excel,xlsxwriter,Python,Excel,Xlsxwriter,从中,insert_image功能采用以下选项: { 'x_offset': 0, 'y_offset': 0, 'x_scale': 1, 'y_scale': 1, 'url': None, 'tip': None, 'image_data': None, 'positioning': None, } 问题是,我需要插入的输入图像的大小可能会有所不同,但它们所需的单

从中,
insert_image
功能采用以下选项:

{
    'x_offset':    0,
    'y_offset':    0,
    'x_scale':     1,
    'y_scale':     1,
    'url':         None,
    'tip':         None,
    'image_data':  None,
    'positioning': None,
}

问题是,我需要插入的输入图像的大小可能会有所不同,但它们所需的单元格大小是固定的。是否有可能以某种方式提供宽度和高度,并让Excel将图像调整为所提供的尺寸?

我认为它没有内置的方式来进行缩放并保持纵横比。你得自己计算

如果您想以目标分辨率调整文件大小并提交文件(可能会降低文件大小),请使用
pillow
的图像方法以及
xlsxwriter
image\u data
选项:

import io
from PIL import Image

def get_resized_image_data(file_path, bound_width_height):
    # get the image and resize it
    im = Image.open(file_path)
    im.thumbnail(bound_width_height, Image.ANTIALIAS)  # ANTIALIAS is important if shrinking

    # stuff the image data into a bytestream that excel can read
    im_bytes = io.BytesIO()
    im.save(im_bytes, format='PNG')
    return im_bytes

# use with xlsxwriter
image_path = 'asdf.png'
bound_width_height = (240, 240)
image_data = get_resized_image_data(image_path, bound_width_height)

# sanity check: remove these three lines if they cause problems
im = Image.open(image_data)
im.show()  # test if it worked so far - it does for me
im.seek(0)  # reset the "file" for excel to read it.

worksheet.insert_image(cell, image_path, {'image_data': image_data})
如果要保持原始分辨率,让excel进行内部缩放,但也要符合您提供的范围,则可以在将其提供给excel之前计算正确的缩放因子:

from PIL import Image


def calculate_scale(file_path, bound_size):
    # check the image size without loading it into memory
    im = Image.open(file_path)
    original_width, original_height = im.size

    # calculate the resize factor, keeping original aspect and staying within boundary
    bound_width, bound_height = bound_size
    ratios = (float(bound_width) / original_width, float(bound_height) / original_height)
    return min(ratios)

# use with xlsxwriter
image_path = 'asdf.png'
bound_width_height = (240, 240)
resize_scale = calculate_scale(image_path, bound_width_height)
worksheet.insert_image(cell, image_path, {'x_scale': resize_scale, 'y_scale': resize_scale})

您可以使用XlsxWriter和基于单元格和图像的高度和宽度的
x_比例
y_比例
从外部或在Excel中缩放图像

例如:

import xlsxwriter

workbook = xlsxwriter.Workbook('image_scaled.xlsx')
worksheet = workbook.add_worksheet()

image_width = 140.0
image_height = 182.0

cell_width = 64.0
cell_height = 20.0

x_scale = cell_width/image_width
y_scale = cell_height/image_height

worksheet.insert_image('B2', 'python.png',
                       {'x_scale': x_scale, 'y_scale': y_scale})

workbook.close()

这样缩放的好处是,用户可以通过在Excel中将缩放设置回100%来恢复原始图像。

谢谢您的回答!是的,我也可以在插入之前调整大小。图像是一个PNG文件,已保存在磁盘上。@Unos已使用基本代码和备忘进行更新,以备选择。@Unos已使用无需创建新文件即可工作的方法进行更新,并将图像保持在原始纵横比的范围内。@Unos另一次更新-我想我找到了一种方法,可以让您在不保存新文件的情况下使用内置的PIL缩略图功能。如果您希望在excel中保留完整大小的图像,但同时保持纵横比,则另一种方法也与以前一样有效。您希望拉伸图像还是保持纵横比?我希望缩小图像,同时可能保持纵横比不变。但是图片的尺寸一直在变化——特别是高度。从你那里得到答案真是太棒了。我也在尝试这种方法。如果我把图像放在一个单元格区域上,会有不同吗?如果我先合并单元格区域,然后插入图像,会有不同吗?在合并或未合并的单元格区域上应该可以。相关问题:我们如何近似计算行高/列宽(以像素为单位)?示例中显示了默认的Excel单元格宽度和高度(以像素为单位)。