'Alter slide layout with Python pptx

I am attempting to alter some text which is on a layout that I will be using for a handful of slides. I will be creating the same pptx document for a number of different parties, and I would like to alter the text on the layout, depending on who the pptx is for.

I know it is possible to get the slide layouts as below. Is it possible to edit the shapes on the layout?

import pptx

prs = pptx.Presentation(importPath)

layouts = prs.slide_layouts
layout1 = layouts[0]

## Edit layout1's shapes here...


Solution 1:[1]

A slide layout is a special variant of a slide, so it also has a .shapes property you can use to access the shapes on the layout. Many of these will be placeholders, but background shapes (such as text-boxes or pictures/logos) will be there too. Once accessed, those shapes are manipulated the same way as shapes on any other slide.

Solution 2:[2]

Here is a script that will modify the text of the title shape in one of the layout slides. It also includes code that lists all of the placeholders and shapes on that layout. This is useful in figuring out how to access the shape you want to modify.

from pptx import Presentation

prs = Presentation()                    # create a new blank presentation
idx_layout = 1
slide = prs.slide_layouts[idx_layout]   # reference a specific layout

print('Placeholder indices for template %d' % (idx_layout))
for shape in slide.placeholders:
        print('idx:{:>3d}   name: {}'.format(shape.placeholder_format.idx, shape.name))

print('\nList of shapes for template %d' % (idx_layout))
for shape in slide.shapes:
    print(shape.name)

slide.shapes[0].text = "New Title"      # set the text in the first shape
prs.save("test.pptx")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 scanny
Solution 2 swimfar2