I'm able to copy the second slide of a test.pptx to a copy.pptx, and create a new copy2.pptx. But the new PPT is corrupted.
Here is my code:
import collections
import collections.abc
import copy
from pptx import Presentation
def copy_slide(prs, source, target_index):
dest = prs.slides.add_slide(source.slide_layout)
for shp in dest.shapes:
shp.element.getparent().remove(shp.element)
# Copy shapes from source, in order
for shape in source.shapes:
new_shape = copy.deepcopy(shape.element)
dest.shapes._spTree.insert_element_before(new_shape, 'p:extLst')
# Copy rels from source
for key, val in source.part.rels.items():
target = val._target
dest.part.rels.add_relationship(val.reltype, target, val.rId, val.is_external)
# Move appended slide into target_index
prs.slides.element.insert(target_index, prs.slides.element[-1])
return dest
prs = Presentation('test.pptx')
source = prs.slides[1]
prs2 = Presentation('copy.pptx')
copyslide = copy_slide(prs2, source, 1)
prs2.save('copy2.pptx')
print(copyslide)
I'm getting following error in the terminal:
UserWarning: Duplicate name: 'ppt/slideLayouts/_rels/slideLayout9.xml.rels'
return self._open_to_write(zinfo, force_zip64=force_zip64)