Add Slides to Presentations with Python
Contents
[
Hide
]
Overview
Before adding slides to a presentation, it helps to understand how PowerPoint organizes them. Each presentation contains a master slide, optional layout slides, and one or more normal slides. Every slide has a unique ID, and normal slides are ordered by a zero-based index. This article shows how to use Aspose.Slides for Python to create slides and choose appropriate layouts.
Add Slides to Presentations
Aspose.Slides allows you to append new slides based on existing layout slides. The example below iterates through each layout in the presentation, adds a slide that uses that layout, and then saves the file.
- Create an instance of the Presentation class.
- Access the SlideCollection.
- For each item in
presentation.layout_slides
, calladd_empty_slide
to append a slide that uses that layout. - Optionally modify the newly added slides.
- Save the presentation as a PPTX file.
import aspose.slides as slides
# Instantiate the Presentation class.
with slides.Presentation() as presentation:
# Access the slide collection.
slides = presentation.slides
for layout_slide in presentation.layout_slides:
# Add an empty slide to the slide collection.
slides.add_empty_slide(layout_slide)
# Do some work on the newly added slides.
# Save the presentation to disk.
presentation.save("empty_slides.pptx", slides.export.SaveFormat.PPTX)