Python에서 PowerPoint 슬라이드에 이미지 추가하는 방법

Python에서 PowerPoint 슬라이드에 이미지 추가하는 방법

Aspose.Slides FOSS의 이미지들은 picture frames으로 삽입되며, 이미지를 보유하고 다른 도형과 마찬가지로 위치를 지정하고 크기를 조정하며 스타일을 적용할 수 있는 도형입니다. 이미지 데이터는 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

모든 치수는 포인트(1 포인트 = 1/72 인치) 단위입니다. 표준 13.33 × 7.5인치 슬라이드의 좌표 공간은 960 × 540 포인트입니다.


바이트에서 이미지 추가

이미지를 바이트 형태로 이미 가지고 있는 경우(예: 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)

채우기 모드 제어

PictureFrame에 있는 picture_fill_format은 이미지가 프레임 경계에 채워지는 방식을 제어합니다:

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 컬렉션은 모든 슬라이드에서 공유됩니다: 그림 프레임이 여러 슬라이드에 나타나더라도 동일한 이미지 바이트가 한 번만 저장됩니다.


또 보기

 한국어