-
-
Save robintw/3df1464e5c8a7ee8835e to your computer and use it in GitHub Desktop.
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 |
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)
Hi, how did you solve the problem (if you have)?
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.
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)
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'