วิธีเพิ่มรูปภาพลงในสไลด์ PowerPoint ด้วย Python
ภาพใน 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)ควบคุมโหมดการเติม
picture_fill_format บน PictureFrame ควบคุมวิธีที่ภาพเติมเต็มขอบกรอบ:
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 ถูกแชร์ทั่วทุกสไลด์: ไบต์ของภาพเดียวกันจะถูกเก็บไว้ครั้งเดียวแม้ว่ากรอบรูปจะปรากฏบนหลายสไลด์ก็ตาม.