0

I've been trying to use some custom layouts on a python-pptx prs, but when I call the prs.slide_layouts method, it return only some of the layouts, only from one of the layout themes. The presentation I'm using has almost 100 layouts, but when I call the follow code, it prints 12 numbers (in order, from 1 to 11).

from pptx import Presentation
from pptx.util import Inches, Pt

for slide_layout in prs.slide_layouts:
    print(prs.slide_layouts.index(slide_layout))

Output:

0
1
2
3
4
5
6
7
8
9
10
11

How can I acess all layout themes on python-pptx? Thank you.

olenscki
  • 487
  • 7
  • 22

1 Answers1

1

Each slide layout belongs to one-and-only-one slide master.

prs.slide_layouts is a handy shortcut for prs.slide_masters[0].slide_layouts; handy because most presentations only have the one slide-master.

If you have multiple slide masters, then you need to be explicit about which one you want to pull from. Maybe something like:

def iter_all_layouts(prs):
    """Generate each slide layout in the presentation, across all slide masters."""
    for master in prs.slide_masters:
        for layout in master.slide_layouts:
            yield layout

Or if you know you want the third layout of the second master:

prs.slide_masters[1].slide_layouts[2]
scanny
  • 26,423
  • 5
  • 54
  • 80
  • Worked out. Thank you! I've never worked on multiple slide-master presentations and I was really pissed by this. I've spent 2 hours thinking about. Now I can start using the python-pptx lib – olenscki Jan 17 '20 at 20:05