Creating, accessing, and managing slides within presentations. Includes slide layouts, notes slides, and slide-specific properties and operations.
Add, access, and manage slides within a presentation using the slides collection.
class Slides:
"""Collection of slides in a presentation."""
def add_slide(self, slide_layout: SlideLayout) -> Slide:
"""
Add a new slide to the presentation.
Parameters:
- slide_layout: SlideLayout object defining the slide structure
Returns:
Newly created Slide object
"""
def __getitem__(self, index: int) -> Slide:
"""Get slide by index (0-based)."""
def __len__(self) -> int:
"""Number of slides in presentation."""
def __iter__(self):
"""Iterate over slides."""Work with individual slides, their properties, and associated elements.
class Slide:
"""Individual slide with content and properties."""
@property
def shapes(self) -> SlideShapes:
"""Collection of shapes on this slide."""
@property
def placeholders(self) -> SlidePlaceholders:
"""Collection of placeholders on this slide."""
@property
def slide_layout(self) -> SlideLayout:
"""Layout template this slide is based on."""
@property
def slide_master(self) -> SlideMaster:
"""Slide master for this slide."""
@property
def notes_slide(self) -> NotesSlide:
"""Notes slide associated with this slide."""
@property
def name(self) -> str:
"""Slide name (may be empty)."""
@name.setter
def name(self, name: str):
"""Set slide name."""
@property
def slide_id(self) -> int:
"""Unique slide identifier."""Access and work with placeholders that provide structured content areas on slides.
class SlidePlaceholders:
"""Collection of placeholders on a slide."""
def __getitem__(self, index: int) -> SlidePlaceholder:
"""Get placeholder by index."""
def __len__(self) -> int:
"""Number of placeholders."""
def __iter__(self):
"""Iterate over placeholders."""
class SlidePlaceholder:
"""Placeholder shape on a slide."""
@property
def name(self) -> str:
"""Placeholder name."""
@property
def placeholder_format(self) -> PlaceholderFormat:
"""Placeholder-specific formatting."""
@property
def text(self) -> str:
"""Text content of placeholder."""
@text.setter
def text(self, text: str):
"""Set text content."""
@property
def text_frame(self) -> TextFrame:
"""Text frame for rich text formatting."""
@property
def shape_type(self) -> MSO_SHAPE_TYPE:
"""Type of placeholder shape."""Work with speaker notes associated with slides.
class NotesSlide:
"""Speaker notes slide associated with a slide."""
@property
def shapes(self) -> NotesSlideShapes:
"""Collection of shapes on notes slide."""
@property
def placeholders(self) -> NotesSlidePlaceholders:
"""Collection of placeholders on notes slide."""
@property
def notes_text_frame(self) -> TextFrame:
"""Text frame for notes content."""
class NotesMaster:
"""Master template for notes slides."""
@property
def shapes(self) -> MasterShapes:
"""Shape collection for notes master."""
@property
def placeholders(self) -> MasterPlaceholders:
"""Placeholder collection for notes master."""Detailed operations on slide layout templates.
class SlideLayout:
"""Layout template defining slide structure."""
@property
def name(self) -> str:
"""Layout name (e.g., 'Title Slide', 'Title and Content')."""
@property
def shapes(self) -> LayoutShapes:
"""Shape collection for this layout."""
@property
def placeholders(self) -> LayoutPlaceholders:
"""Placeholder collection for this layout."""
@property
def slide_master(self) -> SlideMaster:
"""Slide master this layout belongs to."""
@property
def used_by_slides(self) -> list:
"""List of slides using this layout."""
class LayoutPlaceholders:
"""Collection of placeholders in a layout."""
def get_by_idx(self, idx: int) -> LayoutPlaceholder:
"""Get placeholder by placeholder index."""
def __getitem__(self, index: int) -> LayoutPlaceholder:
"""Get placeholder by collection index."""
def __len__(self) -> int:
"""Number of placeholders."""
class LayoutPlaceholder:
"""Placeholder in a slide layout."""
@property
def name(self) -> str:
"""Placeholder name."""
@property
def placeholder_type(self) -> PP_PLACEHOLDER_TYPE:
"""Type of placeholder (title, body, etc.)."""
@property
def idx(self) -> int:
"""Placeholder index number."""Usage examples:
from pptx import Presentation
# Create presentation and add slides
prs = Presentation()
# Add title slide
title_layout = prs.slide_layouts[0] # Title Slide layout
slide1 = prs.slides.add_slide(title_layout)
slide1.shapes.title.text = "Presentation Title"
slide1.placeholders[1].text = "Subtitle"
# Add content slide
content_layout = prs.slide_layouts[1] # Title and Content layout
slide2 = prs.slides.add_slide(content_layout)
slide2.shapes.title.text = "Content Slide"
# Add bullet points to content placeholder
content_placeholder = slide2.placeholders[1]
tf = content_placeholder.text_frame
tf.text = "First bullet point"
p = tf.add_paragraph()
p.text = "Second bullet point"
p.level = 1
# Work with notes
notes_slide = slide2.notes_slide
notes_tf = notes_slide.notes_text_frame
notes_tf.text = "These are speaker notes for this slide"
# Access slide properties
print(f"Total slides: {len(prs.slides)}")
print(f"Slide 2 layout: {slide2.slide_layout.name}")
print(f"Slide 2 has {len(slide2.shapes)} shapes")
# Iterate through slides
for i, slide in enumerate(prs.slides):
print(f"Slide {i+1}: {slide.slide_layout.name}")
if slide.shapes.title:
print(f" Title: {slide.shapes.title.text}")
prs.save('multi-slide-presentation.pptx')Different placeholder types provide structured content areas:
from pptx.enum.shapes import PP_PLACEHOLDER_TYPE
# Common placeholder types
PP_PLACEHOLDER_TYPE.TITLE # Slide title
PP_PLACEHOLDER_TYPE.BODY # Main content area
PP_PLACEHOLDER_TYPE.SUBTITLE # Subtitle on title slide
PP_PLACEHOLDER_TYPE.CHART # Chart placeholder
PP_PLACEHOLDER_TYPE.TABLE # Table placeholder
PP_PLACEHOLDER_TYPE.PICTURE # Picture placeholder
PP_PLACEHOLDER_TYPE.CLIP_ART # Clip art placeholder
PP_PLACEHOLDER_TYPE.MEDIA # Media placeholder
PP_PLACEHOLDER_TYPE.HEADER # Header text
PP_PLACEHOLDER_TYPE.FOOTER # Footer text
PP_PLACEHOLDER_TYPE.SLIDE_NUMBER # Slide number
PP_PLACEHOLDER_TYPE.DATE # Date placeholder
# Check placeholder type
for placeholder in slide.placeholders:
print(f"Placeholder {placeholder.placeholder_format.idx}: {placeholder.placeholder_format.type}")