Skip to content

Instantly share code, notes, and snippets.

@robintw
Last active November 12, 2022 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@LeonOnRoad
Copy link

LeonOnRoad commented Jul 24, 2018

In my case, this code is failing with error:
...
dest.part.rels.add_relationship(value.reltype, value._target, value.rId)
AttributeError: 'str' object has no attribute 'reltype'

@LeonOnRoad
Copy link

Note that SlidePart was added in pptx library, that why I have:
dest.part.rels.add_relationship(value.reltype, value._target, value.rId)
instead of:
dest.rels.add_relationship(value.reltype, value._target, value.rId)

@ShuojinHang
Copy link

Hi, how did you solve the problem (if you have)?

@ghallak
Copy link

ghallak commented Mar 10, 2021

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:

    for key, value in source.part.rels.items():
        if not "notesSlide" in value.reltype:
            if value.is_external:
                dest.part.rels.add_relationship(value.reltype, value.target_ref, value.rId, value.is_external)
            else:
                dest.part.rels.add_relationship(value.reltype, value.target_part, value.rId)

Note that I'm using source.part.rels.items() instead of source.part.rels.iteritems() because I'm using python 3.

@Lirioooo
Copy link

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

Untitled

How can I to repair from the code?

@Jacob-fairplay
Copy link

@Lirioooo were you able to figure out a fix for those errors?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment