使用 python-pptx 创建演示文稿和插入图片时,如何获取图片占位符的尺寸以调整图像大小?

How can I get the dimensions of a picture placeholder to re-size an image when creating a presentation and inserting a picture using python-pptx?

我正在尝试使用 python-pptx 插入一张图片,该图片的大小已重新调整以适合来自模板的图片占位符的尺寸。我不相信 API 可以从我在文档中找到的内容直接访问它。有没有关于我如何使用图书馆或其他方式做到这一点的建议?

我有一个 运行 代码,可以将一系列图像插入一组模板幻灯片中,以使用 Powerpoint 自动创建报告。

这是执行大部分相关工作的函数。应用程序的其他部分正在创建演示文稿和插入幻灯片等。

def insert_images(slide, slide_num, images_path, image_df):

    """
    Insert images into a slide.
    :param slide: = slide object from Presentation class 
    :param slide_num: the template slide number for formatting
    :param images_path: the directory to the folder with all the images
    :param image_df: Pandas data frame regarding information of each image in images_path
    :return: None
    """

    placeholders = get_image_placeholders(slide)
    #print(placeholders)
    image_pool = image_df[image_df['slide_num'] == slide_num]

    try:
        assert len(placeholders) == len(image_pool.index)
    except AssertionError:
        print('Length of placeholders in slide does not match image naming.')
    i = 0
    for idx, image in image_pool.iterrows():
        #print(image)
        image_path = os.path.join(images_path, image.path)
        pic = slide.placeholders[placeholders[i]].insert_picture(image_path)
        #print(image.path)
        # TODO: Add resize - get dimensions of pic placeholder
        line = pic.line
        print(image['view'])
        if image['view'] == 'red':
            line.color.rgb = RGBColor(255, 0, 0)
        elif image['view'] == 'green':
            line.color.rgb = RGBColor(0, 255, 0)
        elif image['view'] == 'blue':
            line.color.rgb = RGBColor(0, 0, 255)
        else:
            line.color.rgb = RGBColor(0, 0, 0)
        line.width = Pt(2.25)
        i+=1

问题是当我将图片插入图片占位符时,图片被裁剪,而不是重新调整大小。我不希望用户知道硬编码到我的脚本中的维度。如果使用的图像相对较大,它可以裁剪很大一部分并且无法使用。

PicturePlaceholder.insert_picture() 返回的图片对象与其派生的占位符具有相同的位置和大小。它被裁剪以完全填充 space。裁剪顶部和底部或裁剪左侧和右侧,具体取决于占位符和您插入的图像的相对纵横比。这与将图片插入图片占位符时 PowerPoint 表现出的行为相同。

如果要取消裁剪,只需将所有裁剪值设置为 0:

picture = placeholder.insert_picture(...)
picture.crop_top = 0
picture.crop_left = 0
picture.crop_bottom = 0
picture.crop_right = 0

这不会改变(top-left 角的位置)但几乎总是会改变尺寸,使其变宽或变高(但不会同时变高)。

所以这很容易解决第一个问题,但当然会出现第二个问题,即如何将图片放置在您想要的位置以及如何在不改变纵横比(拉伸或挤压)的情况下适当缩放图片).

这在很大程度上取决于您要实现的目标以及您认为最满意的结果。这就是为什么它不是自动的;只是无法预测。

您可以这样找到图像的 "native" 宽度和高度:

width, height = picture.image.size  # ---width and height are int pixel-counts

从那里您需要比较原始占位符和您插入的图像的纵横比,并调整图片形状的宽度或高度。

所以说你想保持相同的位置,但将占位符的宽度和高度保持为各自的最大值,这样整个图片适合 space 但有一个 "margin"底部或右侧:

available_width = picture.width
available_height = picture.height
image_width, image_height = picture.image.size
placeholder_aspect_ratio = float(available_width) / float(available_height)
image_aspect_ratio = float(image_width) / float(image_height)

picture.crop_top = 0
picture.crop_left = 0
picture.crop_bottom = 0
picture.crop_right = 0

# ---if the placeholder is "wider" in aspect, shrink the picture width while
# ---maintaining the image aspect ratio
if placeholder_aspect_ratio > image_aspect_ratio:
    picture.width = int(image_aspect_ratio * available_height)
# ---otherwise shrink the height
else:
    picture.height = int(available_width/image_aspect_ratio)

这可以详细说明 "center" 原始 space 中的图像,并可能使用 "negative cropping" 保留原始占位符大小。

我还没有对此进行测试,您可能需要进行一些调整,但希望这能让您了解如何继续。这将是一件好事,可以提取到它自己的功能,比如 adjust_picture_to_fit(picture).

这对我有用。我的图片比占位符大 (slide.shapes[2])。

picture = slide.shapes[2].insert_picture(img_path)
picture.crop_top = 0
picture.crop_left = 0
picture.crop_bottom = 0
picture.crop_right = 0