Last active
May 19, 2025 06:19
-
-
Save robintw/3df1464e5c8a7ee8835e to your computer and use it in GitHub Desktop.
Duplicate slide in python-pptx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def duplicate_slide(pres, index): | |
source = pres.slides[index] | |
try: | |
blank_slide_layout = pres.slide_layouts[6] | |
except: | |
blank_slide_layout = pres.slide_layouts[len(pres.slide_layouts) - 1] | |
dest = pres.slides.add_slide(blank_slide_layout) | |
for shp in source.shapes: | |
el = shp.element | |
newel = copy.deepcopy(el) | |
dest.shapes._spTree.insert_element_before(newel, 'p:extLst') | |
for key, value in source.rels.iteritems(): | |
if not "notesSlide" in value.reltype: | |
dest.rels.add_relationship(value.reltype, value._target, value.rId) | |
return dest |
I have this code in my program, but when i want to execute it, the program prints a lot of warning alerts like these
C:\Users\user7\AppData\Local\Programs\Python\Python38\lib\zipfile.py:1517: UserWarning: Duplicate name: 'ppt/slideLayouts/slideLayout1.xml'
return self._open_to_write(zinfo, force_zip64=force_zip64)
C:\Users\user7\AppData\Local\Programs\Python\Python38\lib\zipfile.py:1517: UserWarning: Duplicate name: 'ppt/slideLayouts/_rels/slideLayout1.xml.rels'
return self._open_to_write(zinfo, force_zip64=force_zip64)
C:\Users\user7\AppData\Local\Programs\Python\Python38\lib\zipfile.py:1517: UserWarning: Duplicate name: 'ppt/slideMasters/slideMaster1.xml'
And when i open the presentation, it shows a message box with the repair button
How can I to repair from the code?
@Lirioooo were you able to figure out a fix for those errors?
This code works in version 0.6.19 and below:
def duplicate_slide(pres, index):
source = pres.slides[index]
blank_slide_layout = pres.slide_layouts[len(pres.slide_layouts) - 1]
dest = pres.slides.add_slide(blank_slide_layout)
for shp in source.shapes:
el = shp.element
newel = copy.deepcopy(el)
dest.shapes._spTree.insert_element_before(newel, 'p:extLst')
for key, value in source.part.rels.items():
if not "notesSlide" in value.reltype:
dest.part.rels.add_relationship(value.reltype, value._target, value.rId)
return dest
Does anybody have a version which works with more recent versions ? (1.0.0 and up)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In python-pptx version 0.6.18 (and maybe some earlier versions as well), line 16 should be changed, and
value._target
should not be used.The second for loop should look like this:
Note that I'm using
source.part.rels.items()
instead ofsource.part.rels.iteritems()
because I'm using python 3.