كيفية تحميل العروض التقديمية في Python
Aspose.Slides FOSS for Python lets you open any .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 ملفات (binary PowerPoint 97–2003) غير مدعومة.
الأشكال لا تملك خاصية 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 خاصية لقراءة البيانات الخام للصورة.
هل يتم دعم التحميل من تدفق في الذاكرة؟?
التحميل مباشرةً من 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)