Core functionality for creating, opening, and saving PowerPoint presentations. Handles presentation-level properties, slide masters, and document metadata.
Create new presentations or load existing .pptx files from disk or file-like objects.
from typing import IO
from pptx import presentation
def Presentation(pptx: str | IO[bytes] | None = None) -> presentation.Presentation:
"""
Create or load a presentation.
Parameters:
- pptx: str | IO[bytes] | None
- str: Path to .pptx file
- IO[bytes]: File-like object containing .pptx file content
- None: Create new presentation from default template
Returns:
presentation.Presentation object
Raises:
- PackageNotFoundError: If file path doesn't exist
- ValueError: If file is not a valid .pptx format
"""Usage example:
from pptx import Presentation
# Create new presentation
prs = Presentation()
# Load existing presentation
prs = Presentation('existing.pptx')
# Load from file-like object
with open('presentation.pptx', 'rb') as f:
prs = Presentation(f)Access and modify presentation-level configuration including slide dimensions, masters, and document properties.
class Presentation:
"""Main presentation object containing slides and configuration."""
@property
def slides(self) -> Slides:
"""Collection of slides in the presentation."""
@property
def slide_layouts(self) -> SlideLayouts:
"""Slide layouts from the first slide master."""
@property
def slide_masters(self) -> SlideMasters:
"""Collection of slide masters in the presentation."""
@property
def slide_width(self) -> int:
"""Slide width in English Metric Units (EMU)."""
@slide_width.setter
def slide_width(self, width: int):
"""Set slide width in EMU."""
@property
def slide_height(self) -> int:
"""Slide height in English Metric Units (EMU)."""
@slide_height.setter
def slide_height(self, height: int):
"""Set slide height in EMU."""
@property
def core_properties(self) -> CoreProperties:
"""Dublin Core document properties (title, author, etc.)."""
@property
def notes_master(self) -> NotesMaster:
"""Notes master for the presentation."""
def save(self, file):
"""
Save presentation to file.
Parameters:
- file: str or file-like object
Path to save location or file-like object to write to
"""Access and modify metadata properties like title, author, subject, and keywords.
class CoreProperties:
"""Dublin Core document properties."""
@property
def title(self) -> str:
"""Document title."""
@title.setter
def title(self, title: str):
"""Set document title."""
@property
def author(self) -> str:
"""Document author."""
@author.setter
def author(self, author: str):
"""Set document author."""
@property
def subject(self) -> str:
"""Document subject."""
@subject.setter
def subject(self, subject: str):
"""Set document subject."""
@property
def keywords(self) -> str:
"""Document keywords."""
@keywords.setter
def keywords(self, keywords: str):
"""Set document keywords."""
@property
def category(self) -> str:
"""Document category."""
@category.setter
def category(self, category: str):
"""Set document category."""
@property
def comments(self) -> str:
"""Document comments."""
@comments.setter
def comments(self, comments: str):
"""Set document comments."""
@property
def created(self) -> datetime:
"""Document creation date."""
@property
def modified(self) -> datetime:
"""Document last modified date."""
@property
def last_modified_by(self) -> str:
"""Last user to modify the document."""
@last_modified_by.setter
def last_modified_by(self, user: str):
"""Set last modified by user."""
@property
def revision(self) -> int:
"""Document revision number."""
@revision.setter
def revision(self, revision: int):
"""Set document revision number."""Manage slide masters that control the formatting and layout templates for slides.
class SlideMasters:
"""Collection of slide masters."""
def __getitem__(self, index: int) -> SlideMaster:
"""Get slide master by index."""
def __len__(self) -> int:
"""Number of slide masters."""
class SlideMaster:
"""Slide master controlling layout and formatting."""
@property
def name(self) -> str:
"""Slide master name."""
@property
def slide_layouts(self) -> SlideLayouts:
"""Slide layouts belonging to this master."""
@property
def shapes(self) -> MasterShapes:
"""Shape collection for this master."""Access layout templates that define placeholder arrangements and formatting.
class SlideLayouts:
"""Collection of slide layouts from a slide master."""
def get_by_name(self, name: str) -> SlideLayout:
"""Get layout by name."""
def __getitem__(self, index: int) -> SlideLayout:
"""Get layout by index."""
def __len__(self) -> int:
"""Number of layouts."""
class SlideLayout:
"""Layout template defining slide structure."""
@property
def name(self) -> str:
"""Layout name."""
@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."""Usage example:
from pptx import Presentation
# Create presentation and set properties
prs = Presentation()
prs.core_properties.title = "My Presentation"
prs.core_properties.author = "John Doe"
prs.core_properties.subject = "Data Analysis Results"
# Work with slide masters and layouts
slide_master = prs.slide_masters[0]
layout = slide_master.slide_layouts[1] # Title and Content layout
print(f"Using layout: {layout.name}")
print(f"Available layouts: {len(slide_master.slide_layouts)}")
# Save presentation
prs.save('presentation.pptx')