python-pptx 利用prs.slides._sldIdLst 进行ppt的删除、重新排列及复制
python-pptx 删除及重新排列ppt;
# -*- coding = UTF-8 -*-
import pptx
from pptx import Presentation
prs = Presentation(r"test.pptx")
#
slides = list(prs.slides._sldIdLst)
prs.slides._sldIdLst.remove(slides[1]) # 删除
prs.slides._sldIdLst.append(slides[1]) # 添加
#
prs.save(r"test2.pptx")
注:多次append是没用的,不能用来复制ppt;
另外也可用以下方式删除ppt;
del prs.slides._sldIdLst[index] #index是ppt序号
参考资料:https://blog.csdn.net/xiaotuzigaga/article/details/88655516
https://blog.csdn.net/u014779536/article/details/106790460
python-pptx 复制ppt;
# -*- coding = UTF-8 -*-
import pptx
import copy
from pptx import Presentation
prs = Presentation(r"test.pptx")
#
def duplicate_slide(pres,index):
slide = pres.slides[index]
blank_slide_layout = pres.slide_layouts[6] #空白页
copied_slide = pres.slides.add_slide(blank_slide_layout)
for shape in slide.shapes:
el = shape.element
newel = copy.deepcopy(el)
copied_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
for value in slide.part.rels.values():
# Make sure we don't copy a notesSlide relation as that won't exist
if "notesSlide" not in value.reltype:
copied_slide.part.rels.add_relationship(value.reltype,value._target, value.rId)
return copied_slide
copied_slide = duplicate_slide(prs, 1)
prs.save(r"test2.pptx")
参考资料:
https://segmentfault.com/q/1010000021456549
不太清楚原理,请看到的大神解释一下,学习!
20201211