如何在 Python 中向 PowerPoint 幻灯片添加图像

如何在 Python 中向 PowerPoint 幻灯片添加图像

Aspose.Slides FOSS 中的图像以 图片框 的形式嵌入,这些形状保存图像并且可以像其他形状一样定位、调整大小和设置样式。图像数据仅在 prs.images 集合中存储一次,并由框架引用。


先决条件

pip install aspose-slides-foss

从文件添加图像

from aspose.slides_foss import ShapeType, PictureFillMode
import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]

    # Load image into the presentation's image collection
    with open("photo.jpg", "rb") as f:
        img = prs.images.add_image(f.read())

    # Add a picture frame at (x=50, y=50, width=400, height=300) in points
    frame = slide.shapes.add_picture_frame(
        ShapeType.RECTANGLE,
        50, 50, 400, 300,
        img,
    )

    prs.save("with-image.pptx", SaveFormat.PPTX)

add_picture_frame 签名:

add_picture_frame(shape_type, x, y, width, height, image) → PictureFrame

所有尺寸均以 points(1 point = 1/72 英寸)为单位。对于标准的 13.33 × 7.5 英寸幻灯片,坐标空间为 960 × 540 points。


从字节添加图像

如果您已经拥有图像的字节(例如,从 URL 下载或从数据库读取):

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

image_bytes = open("logo.png", "rb").read()  # or any bytes source

with slides.Presentation() as prs:
    img = prs.images.add_image(image_bytes)

    prs.slides[0].shapes.add_picture_frame(
        ShapeType.RECTANGLE,
        200, 100, 300, 200,
        img,
    )
    prs.save("logo-slide.pptx", SaveFormat.PPTX)

控制填充模式

picture_fill_formatPictureFrame 上控制图像填充帧边界的方式:

from aspose.slides_foss import ShapeType, PictureFillMode
import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    img = prs.images.add_image(open("texture.png", "rb").read())
    frame = prs.slides[0].shapes.add_picture_frame(
        ShapeType.RECTANGLE, 50, 50, 600, 350, img
    )

    # STRETCH: scale image to fill the frame exactly (default)
    frame.picture_format.picture_fill_mode = PictureFillMode.STRETCH

    # TILE: repeat the image in a grid pattern
    # frame.picture_format.picture_fill_mode = PictureFillMode.TILE

    prs.save("filled.pptx", SaveFormat.PPTX)
PictureFillMode行为
STRETCH将图像缩放以填满框架,忽略纵横比
TILE将图像重复为平铺图案
TILE_FLIP使用交替的水平/垂直翻转进行平铺

向不同幻灯片添加多个图像

import os
from aspose.slides_foss import ShapeType
import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat

image_files = ["slide1.jpg", "slide2.jpg", "slide3.jpg"]

with slides.Presentation() as prs:
    layout = prs.slides[0].layout_slide

    # Ensure enough slides exist
    while len(prs.slides) < len(image_files):
        prs.slides.add_empty_slide(layout)

    for i, path in enumerate(image_files):
        if not os.path.exists(path):
            continue
        img = prs.images.add_image(open(path, "rb").read())
        prs.slides[i].shapes.add_picture_frame(
            ShapeType.RECTANGLE, 0, 0, 960, 540, img
        )

    prs.save("multi-image.pptx", SaveFormat.PPTX)

统计现有演示文稿中的图像

import aspose.slides_foss as slides

with slides.Presentation("with-image.pptx") as prs:
    print(f"Presentation contains {len(prs.images)} image(s)")

prs.images 集合在所有幻灯片中共享:即使图片框出现在多个幻灯片中,图像字节也只存储一次。


另见

 中文