From 029fb718ae2edf7cb1327a3d664af55a67d60b37 Mon Sep 17 00:00:00 2001 From: Raul Sofia Date: Sat, 16 Nov 2024 21:20:05 +0000 Subject: [PATCH] added Slides.remove_slide(slide_id) --- src/pptx/oxml/presentation.py | 8 ++++++++ src/pptx/parts/presentation.py | 8 ++++++++ src/pptx/slide.py | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/src/pptx/oxml/presentation.py b/src/pptx/oxml/presentation.py index 17997c2b..dbbe0c41 100644 --- a/src/pptx/oxml/presentation.py +++ b/src/pptx/oxml/presentation.py @@ -65,6 +65,14 @@ def add_sldId(self, rId: str) -> CT_SlideId: The new `p:sldId` element has its r:id attribute set to `rId`. """ return self._add_sldId(id=self._next_id, rId=rId) + + def remove_sldId(self, slide_id: int) -> None: + """Remove the `p:sldId` element with the specified slide ID.""" + for sldId in self.sldId_lst: + if sldId.id == slide_id: + self.remove(sldId) + return + raise KeyError(f"no slide with id {slide_id}") @property def _next_id(self) -> int: diff --git a/src/pptx/parts/presentation.py b/src/pptx/parts/presentation.py index 1413de45..e80b7d40 100644 --- a/src/pptx/parts/presentation.py +++ b/src/pptx/parts/presentation.py @@ -33,6 +33,14 @@ def add_slide(self, slide_layout: SlideLayout): rId = self.relate_to(slide_part, RT.SLIDE) return rId, slide_part.slide + def remove_slide(self, slide_id: int): + """Remove slide with `slide_id` from this presentation.""" + for sldId in self._element.sldIdLst: + if sldId.id == slide_id: + self.drop_rel(sldId.rId) + return + raise KeyError("no slide with id %d" % slide_id) + @property def core_properties(self) -> CorePropertiesPart: """A |CoreProperties| object for the presentation. diff --git a/src/pptx/slide.py b/src/pptx/slide.py index 3b1b65d8..cd137f66 100644 --- a/src/pptx/slide.py +++ b/src/pptx/slide.py @@ -272,6 +272,11 @@ def add_slide(self, slide_layout: SlideLayout) -> Slide: self._sldIdLst.add_sldId(rId) return slide + def remove_slide(self, slide_id: int) -> None: + """Remove slide with `slide_id` from this presentation.""" + self.part.remove_slide(slide_id) + self._sldIdLst.remove_sldId(slide_id) + def get(self, slide_id: int, default: Slide | None = None) -> Slide | None: """Return the slide identified by int `slide_id` in this presentation.