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)फ़िल मोड को नियंत्रित करें
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 संग्रह सभी स्लाइड्स में साझा किया गया है: एक ही छवि बाइट्स को केवल एक बार संग्रहीत किया जाता है, भले ही चित्र फ्रेम कई स्लाइड्स पर दिखाई दे।