1
votes

I am trying to use an existing template design of powerpoint using python-pptx library. My problem is that I have two or more different templates ready and when I viewed their slide master, the "title and content layout" of each template are not on the same order. So, the index that I will use will be 1 if I used the first templates and 2 for the second templates.

Using the python-pptx library:

Sample Python Code 1 for fist templates

bullet_slide_layout = self.prs.slide_layouts[1]

Sample Python Code 2 for second templates

bullet_slide_layout = self.prs.slide_layouts[2]

Both of them works, but I do not want to change the indices every now and then whenever a new template design is added.

Please help. Also, If I am not clear with the problem I presented, please tell me. Thank you

1

1 Answers

1
votes

If you want to retrieve a slide layout by something other than its position in the layout sequence, you will have to write something of your own.

There are a few approaches:

  • Use the slide layout name
  • Use the slide layout id
  • Characterize the slide by the number and type of placeholders it contains and perhaps their size and position.

So as an example, something simple would be:

def get_layout_by_name(prs, layout_name):
    for layout in prs.slide_layouts:
        if layout.name == layout_name:
            return layout
    return None