Sometimes it is useful to automatically generate powerpoint files for weekly or biweekly meeting, there’s nothing better to handle this issue than Python, in this tutorail, Python will be used to generate powerpoint slides based on a predefined template so users don’t have to create files by clicking again and again.
Objective
- Use Python pptx to create a powerpoint slides file based on a template.
- Fill in the title slide and table of content slide with predefined info.
- Name the file with a specific timestamp.
Template File
Template file is one of the most important part for Python modules to work on, thus one needs to know how to design and save a template, althrough the extension potx is regarded as the template, but the Python pptx refuses to work with this extension. So you need to save the file as pptx.
When creating a new slides file with the default theme, you could go to view->master slides to manage the template, there are things that you could work on, like setting the name, background color as well as other properties. You could refer to the guide here1.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pptx import Presentation
# Load the PowerPoint template
template_path = 'path of demo.pptx' # Replace with your template file path
presentation = Presentation(template_path)
# Access the slide masters
for slide_master in presentation.slide_masters:
print(f"Slide Master ID: {slide_master}")
# Access the slide layouts for each master
for layout in slide_master.slide_layouts:
print(f" Layout Name: {layout.name}")
Based on the code above, first slide master is referred as presentation.slide_masters[0]
, the first slide within the first slide master can also be annotated as presentation.slide_masters[0].slide_layouts[0]
. Those 2 code snippets will be very useful when composing the powerpoint slides in the latter part of this article.
Slide Master ID: <pptx.slide.SlideMaster object at 0x1088c1910>
Layout Name: Title Slide
Layout Name: Title and Content
Slide Master ID: <pptx.slide.SlideMaster object at 0x1088c1890>
Layout Name: Title and Content
Layout Name: Two Content
Layout Name: Comparison
Layout Name: Title Only
Layout Name: Blank
Layout Name: Content with Caption
Layout Name: Picture with Caption
Layout Name: Title and Vertical Text
Layout Name: Vertical Title and Text
So there are 2 sets of slide master, each contains some layouts with name shown within the one of the slide master set.
Python Workflow
The Python workflow for creating a powerpoint slides file is based on the predefefined powerpower template, by adding a slide based on the specific slide within the template file, a concatnation of slides with different configurations will be added to the slide one wants to generate. Without a template file, pptx will create slides file with aspect ratio of 4:3, which will not utilize the widescreen widely used at the moment. Users could create one’s one template file to 16:9 and setup specific master slides.
The python workflow is shown as follows:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#generate pptx file from template
#fill in the 1st slide with time and date
import pathlib as pl
from pptx import Presentation
template= pl.Path(__file__).parent/'demo.pptx'
new_ppt_dir= pl.Path(__file__).parent/'new_ppt.pptx'
from pptx import Presentation
from pptx.util import Inches, Pt
import datetime
from datetime import datetime, timedelta
_weekday = 0 # Monday
def get_next_monday(_date=datetime.today(), recur=False):
if _date.weekday() == _weekday:
if recur == True:
return _date
else:
return _date + timedelta(days=7)
else:
return get_next_tuesday(_date + timedelta(days=1), recur=True)
presentation = Presentation(template)
# Add a title slide
title_slide_layout = presentation.slide_masters[0].slide_layouts[0] # Assuming the first layout is for the title slide
title_slide = presentation.slides.add_slide(title_slide_layout)
# Set the title
title_placeholder = title_slide.shapes.title
content=title_slide.placeholders[1]
title_placeholder.text = "Title of Slide Automated"
content.text = f'''Your_Name\n{get_next_monday().strftime('%Y-%m-%d')} Hour-Hour\nConference Place\n'''
toc_slide_layout = presentation.slide_masters[1].slide_layouts[0] # Assuming the second layout is for content
toc=presentation.slides.add_slide(toc_slide_layout)
title=toc.shapes.title
title.text='Table of Contents'
content=toc.placeholders[1]
for i in range(1,3):
slide=presentation.slides.add_slide(toc_slide_layout)
title=slide.shapes.title
presentation.save(new_ppt_dir)
Suppose there is a recurring presentation every Monday, get_next_monday()
could get the next Monday from the current date. Once the code is run, a powerpower slide will be created with essential time stamp of next Monday to be filled into the slides.
presentation = Presentation(template)
creates a presentation file object regarding the template file. Normally there’s a big difference between title slide and the rest of the slides, thus 2 slide masters will be used for both the title slide and the remaining ones. For the first slide, which is named as title_silde, the first slide of the slide master will be added. Thus the following code will be used.
# Add a title slide
title_slide_layout = presentation.slide_masters[0].slide_layouts[0] # Assuming the first layout is for the title slide
title_slide = presentation.slides.add_slide(title_slide_layout)
Once the title_slide is added, its contents will be filled as follows:
# Set the title
title_placeholder = title_slide.shapes.title
content=title_slide.placeholders[1]
title_placeholder.text = "Title of Slide Automated"
content.text = f'''Your_Name\n{get_next_monday().strftime('%Y-%m-%d')} Hour-Hour\nConference Place\n'''
Similarly, one table of contents and remaining 3 more slides will be added with same template slide using the following code block.
toc_slide_layout = presentation.slide_masters[1].slide_layouts[0] # Assuming the second layout is for content
toc=presentation.slides.add_slide(toc_slide_layout)
title=toc.shapes.title
title.text='Table of Contents'
content=toc.placeholders[1]
for i in range(1,3):
slide=presentation.slides.add_slide(toc_slide_layout)
title=slide.shapes.title
Conclusion
Automating recurring tasks could be useful as it reduces manual operations which could be more efficient and less prune to error. By referring to various part of the template, one could design a useful template and use the template to append different slides into the powerpoint file to be editted.
Leave a Reply