Slide masters and layouts provide the template structure for presentations in python-pptx. A slide master defines the overall look and common elements, while slide layouts provide specific arrangements for different slide types.
from pptx import Presentationprs = Presentation()slide_masters = prs.slide_masters# Iterate through all mastersfor master in slide_masters: print(f"Master: {master.name}")# Access by indexfirst_master = slide_masters[0]
from pptx import Presentationprs = Presentation()# Get layouts from the first slide masterslide_layouts = prs.slide_layouts# Or get layouts from a specific mastermaster = prs.slide_masters[0]slide_layouts = master.slide_layouts# Count available layoutsprint(f"Available layouts: {len(slide_layouts)}")# Iterate through layoutsfor layout in slide_layouts: print(f"Layout: {layout.name}")
from pptx import Presentationprs = Presentation()slide_layouts = prs.slide_layouts# Get a specific layout by nametitle_layout = slide_layouts.get_by_name('Title Slide')if title_layout: slide = prs.slides.add_slide(title_layout)
The .get_by_name() method returns the layout if found, or None if not found.
from pptx import Presentationprs = Presentation()slide_layouts = prs.slide_layouts# Use different layouts for different slide typestitle_slide = prs.slides.add_slide(slide_layouts[0]) # Title layoutcontent_slide = prs.slides.add_slide(slide_layouts[1]) # Content layoutblank_slide = prs.slides.add_slide(slide_layouts[6]) # Blank layout
from pptx import Presentationprs = Presentation('presentation.pptx')slide = prs.slides[0]# Check which layout the slide useslayout = slide.slide_layoutprint(f"Slide uses layout: {layout.name}")# Get the master from the layoutmaster = layout.slide_masterprint(f"Layout inherits from master: {master.name}")
from pptx import Presentationprs = Presentation()slide_master = prs.slide_master# Access all shapes on the masterfor shape in slide_master.shapes: print(f"Shape: {shape.name}")# Access placeholders specificallyfor placeholder in slide_master.placeholders: print(f"Placeholder idx={placeholder.placeholder_format.idx}")
from pptx import Presentationfrom pptx.util import Inchesfrom pptx.enum.dml import MSO_THEME_COLORprs = Presentation()slide_master = prs.slide_master# Set solid color backgroundbackground = slide_master.backgroundfill = background.fillfill.solid()fill.fore_color.theme_color = MSO_THEME_COLOR.ACCENT_1
Accessing the .fill property on a background can be destructive. It may remove existing background style references. Only use when you intend to modify the background.
You can remove unused slide layouts from a slide master.
from pptx import Presentationprs = Presentation()slide_master = prs.slide_masterslide_layouts = slide_master.slide_layouts# Get a layout to removelayout_to_remove = slide_layouts.get_by_name('Blank')if layout_to_remove: # Check if layout is in use if not layout_to_remove.used_by_slides: slide_layouts.remove(layout_to_remove) print("Layout removed successfully") else: print(f"Cannot remove: layout used by {len(layout_to_remove.used_by_slides)} slides")
A layout that is used by one or more slides cannot be removed. The .remove() method raises ValueError if the layout is in use.
from pptx import Presentationprs = Presentation()slide_layouts = prs.slide_layoutsfor idx, layout in enumerate(slide_layouts): print(f"Index {idx}: {layout.name}") # Find which slides use this layout slides_using_layout = layout.used_by_slides print(f" Used by {len(slides_using_layout)} slides")
Always create slides from appropriate layouts rather than blank slides. This ensures consistency across your presentation and makes it easier to apply theme changes.
Avoid removing layouts prematurely
Before removing a layout, check if it’s in use with .used_by_slides. Attempting to remove a layout in use will raise an error.
Test background changes carefully
Accessing .background.fill can remove existing style references. Always test on a copy of your presentation first.
Name your layouts clearly
Use .get_by_name() with descriptive layout names rather than relying on numeric indices, which may vary between templates.