MCPcopy Create free account
hub / github.com/scanny/python-pptx / SlideLayouts

Class SlideLayouts

src/pptx/slide.py:344–411  ·  view source on GitHub ↗

Sequence of slide layouts belonging to a slide-master. Supports indexed access, len(), iteration, index() and remove().

Source from the content-addressed store, hash-verified

342
343
344class SlideLayouts(ParentedElementProxy):
345 """Sequence of slide layouts belonging to a slide-master.
346
347 Supports indexed access, len(), iteration, index() and remove().
348 """
349
350 part: SlideMasterPart # pyright: ignore[reportIncompatibleMethodOverride]
351
352 def __init__(self, sldLayoutIdLst: CT_SlideLayoutIdList, parent: SlideMaster):
353 super(SlideLayouts, self).__init__(sldLayoutIdLst, parent)
354 self._sldLayoutIdLst = sldLayoutIdLst
355
356 def __getitem__(self, idx: int) -> SlideLayout:
357 """Provides indexed access, e.g. `slide_layouts[2]`."""
358 try:
359 sldLayoutId = self._sldLayoutIdLst.sldLayoutId_lst[idx]
360 except IndexError:
361 raise IndexError("slide layout index out of range")
362 return self.part.related_slide_layout(sldLayoutId.rId)
363
364 def __iter__(self) -> Iterator[SlideLayout]:
365 """Generate each |SlideLayout| in the collection, in sequence."""
366 for sldLayoutId in self._sldLayoutIdLst.sldLayoutId_lst:
367 yield self.part.related_slide_layout(sldLayoutId.rId)
368
369 def __len__(self) -> int:
370 """Support len() built-in function, e.g. `len(slides) == 4`."""
371 return len(self._sldLayoutIdLst)
372
373 def get_by_name(self, name: str, default: SlideLayout | None = None) -> SlideLayout | None:
374 """Return SlideLayout object having `name`, or `default` if not found."""
375 for slide_layout in self:
376 if slide_layout.name == name:
377 return slide_layout
378 return default
379
380 def index(self, slide_layout: SlideLayout) -> int:
381 """Return zero-based index of `slide_layout` in this collection.
382
383 Raises `ValueError` if `slide_layout` is not present in this collection.
384 """
385 for idx, this_layout in enumerate(self):
386 if slide_layout == this_layout:
387 return idx
388 raise ValueError("layout not in this SlideLayouts collection")
389
390 def remove(self, slide_layout: SlideLayout) -> None:
391 """Remove `slide_layout` from the collection.
392
393 Raises ValueError when `slide_layout` is in use; a slide layout which is the basis for one
394 or more slides cannot be removed.
395 """
396 # ---raise if layout is in use---
397 if slide_layout.used_by_slides:
398 raise ValueError("cannot remove slide-layout in use by one or more slides")
399
400 # ---target layout is identified by its index in this collection---
401 target_idx = self.index(slide_layout)

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…