Python में प्रस्तुतियों को लोड कैसे करें
Aspose.Slides FOSS for Python आपको किसी भी .pptx फ़ाइल को खोलने, उसकी सामग्री का निरीक्षण करने, और या तो इसे PPTX में वापस सहेजने या उससे डेटा निकालने की अनुमति देता है। यह गाइड फ़ाइल खोलने, स्लाइड्स को इटररेट करने, शैप टेक्स्ट पढ़ने, और सहेजने की राउंड‑ट्रिपिंग को कवर करता है।
चरण-दर-चरण गाइड
चरण 1: पैकेज स्थापित करें
pip install aspose-slides-fossचरण 2: मौजूदा प्रस्तुति खोलें
फ़ाइल पथ को slides.Presentation() को पास करें। क्लीनअप सुनिश्चित करने के लिए कॉन्टेक्स्ट मैनेजर का उपयोग करें।
import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat
with slides.Presentation("input.pptx") as prs:
print(f"Slide count: {len(prs.slides)}")
prs.save("output.pptx", SaveFormat.PPTX)स्रोत फ़ाइल में अज्ञात XML भागों को शाब्दिक रूप से संरक्षित किया जाता है: लाइब्रेरी कभी भी ऐसी सामग्री को नहीं हटाती जो वह अभी तक नहीं समझती।
चरण 3: स्लाइड्स की जाँच करें
सभी स्लाइड्स पर इटररेट करें और उनका इंडेक्स प्रिंट करें:
import aspose.slides_foss as slides
with slides.Presentation("deck.pptx") as prs:
for i, slide in enumerate(prs.slides):
shape_count = len(slide.shapes)
print(f"Slide {i}: {shape_count} shapes")चरण 4: आकार पाठ पढ़ें
आकारों पर इटररेट करें और उन आकारों से टेक्स्ट पढ़ें जिनमें TextFrame है:
import aspose.slides_foss as slides
with slides.Presentation("deck.pptx") as prs:
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "text_frame") and shape.text_frame is not None:
text = shape.text_frame.text
if text.strip():
print(f" Shape text: {text!r}")चरण 5: दस्तावेज़ गुण पढ़ें
prs.document_properties से कोर दस्तावेज़ गुणों तक पहुँचें:
import aspose.slides_foss as slides
with slides.Presentation("deck.pptx") as prs:
props = prs.document_properties
print(f"Title: {props.title}")
print(f"Author: {props.author}")
print(f"Subject: {props.subject}")चरण 6: राउंड-ट्रिप सहेजें
प्रेजेंटेशन की जांच करने या संशोधित करने के बाद, इसे फिर से PPTX में सहेजें:
prs.save("output.pptx", SaveFormat.PPTX)विभिन्न पथ पर सहेजने से एक नई फ़ाइल बनती है। उसी पथ पर सहेजने से मूल फ़ाइल ओवरराइट हो जाती है।
सामान्य समस्याएँ और समाधान
FileNotFoundError
जाँचें कि कार्य निर्देशिका के सापेक्ष .pptx फ़ाइल का पथ सही है। मजबूत पथ निर्माण के लिए pathlib.Path का उपयोग करें:
from pathlib import Path
path = Path(__file__).parent / "assets" / "deck.pptx"
with slides.Presentation(str(path)) as prs:
...Exception: File format is not supported
लाइब्रेरी केवल .pptx (Office Open XML) का समर्थन करती है। लेगेसी .ppt (बाइनरी PowerPoint 97–2003) फ़ाइलें समर्थित नहीं हैं।
Shapes में कोई text_frame एट्रिब्यूट नहीं है
कुछ आकार (Connectors, PictureFrames, GroupShapes) में text_frame नहीं होता है। टेक्स्ट तक पहुँचने से पहले hasattr(shape, "text_frame") and shape.text_frame is not None से सुरक्षा करें।
अक्सर पूछे जाने वाले प्रश्न
क्या लोडिंग सभी मूल सामग्री को संरक्षित करती है?
हाँ। अज्ञात XML भागों को राउंड‑ट्रिप सहेजने पर शब्दशः संरक्षित रखा जाता है। लाइब्रेरी किसी भी XML सामग्री को नहीं हटाएगी जिसे वह अभी तक समझ नहीं पाती।
क्या मैं पासवर्ड‑संरक्षित PPTX लोड कर सकता हूँ?
पासवर्ड-प्रोटेक्टेड (एन्क्रिप्टेड) प्रस्तुतियों का इस संस्करण में समर्थन नहीं किया जाता है।
क्या मैं एम्बेडेड छवियों को निकाल सकता हूँ?
इमेज संग्रह तक पहुँचें: prs.images ImageCollection लौटाता है। प्रत्येक इमेज में एक content_type और एक bytes प्रॉपर्टी होती है जो कच्चा इमेज डेटा पढ़ती है।
क्या इन‑मेमोरी स्ट्रीम से लोड करना समर्थित है?
वर्तमान API में io.BytesIO से सीधे लोड करना उपलब्ध नहीं है। पहले बाइट्स को एक अस्थायी फ़ाइल में लिखें:
import tempfile, os
import aspose.slides_foss as slides
with tempfile.NamedTemporaryFile(suffix=".pptx", delete=False) as tmp:
tmp.write(pptx_bytes)
tmp_path = tmp.name
try:
with slides.Presentation(tmp_path) as prs:
print(f"Slides: {len(prs.slides)}")
finally:
os.unlink(tmp_path)