Allen YangWhen automating PowerPoint presentation generation, configuring document-level properties is just...
When automating PowerPoint presentation generation, configuring document-level properties is just as important as adding content and shapes. This includes setting slide dimensions, page orientation, playback modes, and document metadata. Programmatic configuration of these properties ensures that generated presentations meet specific display requirements and branding guidelines.
This article demonstrates how to configure PowerPoint presentation page settings, playback properties, and document information using Python.
First, install the Spire.Presentation library:
pip install Spire.Presentation
PowerPoint defaults to 16:9 or 4:3 aspect ratios, but certain scenarios require custom slide dimensions, such as creating square posters, widescreen banners, or content with specific proportions.
from spire.presentation import *
from spire.presentation.common import *
# Create a presentation instance
presentation = Presentation()
# Set custom slide size
size = SizeF(800.0, 600.0)
presentation.SlideSize.Size = size
# Set slide type to custom
presentation.SlideSize.Type = SlideSizeType.Custom
# Set page orientation to landscape
presentation.SlideSize.Orientation = SlideOrienation.Landscape
# Save the document
presentation.SaveToFile("CustomSlideSize.pptx", FileFormat.Pptx2013)
presentation.Dispose()
In this code, the SlideSize object controls slide dimensions and orientation. The SizeF class specifies width and height in points. The SlideOrienation enum provides two options: Landscape and Portrait.
After setting slide dimensions, background images and content elements typically need adjustment to fit the new size:
from spire.presentation import *
from spire.presentation.common import *
import math
presentation = Presentation()
# Set slide dimensions
size = SizeF(600.0, 600.0)
presentation.SlideSize.Size = size
presentation.SlideSize.Orientation = SlideOrienation.Portrait
presentation.SlideSize.Type = SlideSizeType.Custom
# Add background image to fill the entire slide
rect = RectangleF.FromLTRB(
0, 0,
presentation.SlideSize.Size.Width,
presentation.SlideSize.Size.Height
)
presentation.Slides[0].Shapes.AppendEmbedImageByPath(
ShapeType.Rectangle,
"background.png",
rect
)
# Add a centered text box
left = math.trunc(presentation.SlideSize.Size.Width / 2) - 200
rec = RectangleF.FromLTRB(left, 150, 400 + left, 350)
shape = presentation.Slides[0].Shapes.AppendShape(
ShapeType.Rectangle,
rec
)
shape.ShapeStyle.LineColor.Color = Color.get_White()
shape.Fill.FillType = FillFormatType.none
# Set text content and formatting
shape.AppendTextFrame("Custom-Sized Presentation")
textRange = shape.TextFrame.Paragraphs[0].TextRanges[0]
textRange.LatinFont = TextFont("Arial")
textRange.FontHeight = 24
textRange.Fill.FillType = FillFormatType.Solid
textRange.Fill.SolidColor.Color = Color.FromArgb(255, 36, 64, 97)
presentation.SaveToFile("PageSetupDemo.pptx", FileFormat.Pptx2010)
presentation.Dispose()
The math.trunc function calculates the center position, ensuring the text box displays correctly on slides of different sizes.
In certain display scenarios, such as exhibition screens, digital signage, or self-service kiosks, presentations need to loop automatically.
from spire.presentation import *
# Load an existing presentation
ppt = Presentation()
ppt.LoadFromFile("InputTemplate.pptx")
# Enable loop playback
ppt.ShowLoop = True
# Enable animations
ppt.ShowAnimation = True
# Enable narration
ppt.ShowNarration = True
# Use slide transition timings for automatic advancement
ppt.UseTimings = True
# Save the configuration
ppt.SaveToFile("LoopPresentation.pptx", FileFormat.Pptx2013)
ppt.Dispose()
Setting ShowLoop to True causes the presentation to automatically return to the first slide after reaching the last one. The UseTimings property enables preset slide transition times, achieving fully automated playback.
Kiosk mode is a special playback mode designed for public display scenarios. In this mode, users cannot manually switch slides by clicking or pressing keys; navigation occurs only through preset timings or hyperlinks.
from spire.presentation import *
ppt = Presentation()
ppt.LoadFromFile("InputTemplate.pptx")
# Set playback type to kiosk mode
ppt.ShowType = SlideShowType.Kiosk
ppt.SaveToFile("KioskMode.pptx", FileFormat.Pptx2013)
ppt.Dispose()
The SlideShowType enum provides several playback modes:
Kiosk: Kiosk browse mode with manual switching disabledPresented: Standard presenter-controlled playbackWindowed: Windowed playback modeIn practical applications, presentations may come from web requests, databases, or other non-file-system sources. Spire.Presentation supports loading documents from Stream objects.
from spire.presentation import *
# Create a presentation instance
ppt = Presentation()
# Load presentation from file stream
file_stream = Stream("InputTemplate.pptx")
ppt.LoadFromStream(file_stream, FileFormat.Pptx2013)
# Perform operations...
# Release resources
file_stream.Dispose()
ppt.Dispose()
This approach is particularly useful for web applications, allowing direct processing of uploaded file streams without saving to disk first.
Similarly, generated presentations can be saved to streams for further processing or direct transmission:
from spire.presentation import *
presentation = Presentation()
# Add content
shape = presentation.Slides[0].Shapes.AppendShape(
ShapeType.Rectangle,
RectangleF.FromLTRB(50, 100, 650, 250)
)
shape.Fill.FillType = FillFormatType.none
shape.ShapeStyle.LineColor.Color = Color.get_White()
shape.TextFrame.Text = "Stream Output Example"
# Save to stream
output_stream = Stream("OutputPresentation.pptx")
presentation.SaveToFile(output_stream, FileFormat.Pptx2013)
output_stream.Close()
presentation.Dispose()
Stream operations offer the advantage of avoiding temporary file creation and deletion, improving application performance and security.
Document properties contain metadata such as author, company, and title, which facilitate document management and retrieval.
from spire.presentation import *
presentation = Presentation()
presentation.LoadFromFile("Template.pptx")
# Set document properties
doc_property = presentation.DocumentProperty
doc_property.Application = "Spire.Presentation"
doc_property.Author = "Development Team"
doc_property.Company = "Company Name"
doc_property.Keywords = "Presentation, Automation, Python"
doc_property.Comments = "This is an automatically generated presentation"
doc_property.Category = "Technical Documentation"
doc_property.Title = "Project Report Presentation"
doc_property.Subject = "Quarterly Work Summary"
# Save the document
presentation.SaveToFile("WithProperties.pptx", FileFormat.Pptx2010)
presentation.Dispose()
These properties can be viewed in PowerPoint's "File > Info > Properties" panel and are essential for enterprise document management systems.
Presented mode for speaker-controlled pacingKiosk mode with loop playback enabledUseTimings and ShowAnimation for consistent viewing experiencesPresentation instances instead of frequently creating and destroying themDispose() promptly to release resources and prevent memory leaksConfiguring PowerPoint presentation properties and page settings through Python enables highly customized document generation. This article covered key techniques including slide size adjustment, playback mode configuration, stream operations, and document property management. These capabilities allow developers to create professional presentations tailored to different application scenarios, from automated reports to digital signage content.
Mastering these fundamental configuration methods provides a foundation for building complete presentation automation solutions when combined with content elements such as shapes, charts, and animations.