Skip to content

Instantly share code, notes, and snippets.

@robintw
Last active May 19, 2025 06:19
Show Gist options
  • Save robintw/3df1464e5c8a7ee8835e to your computer and use it in GitHub Desktop.
Save robintw/3df1464e5c8a7ee8835e to your computer and use it in GitHub Desktop.
Duplicate slide in python-pptx
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
@ptonelli
Copy link

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