Python使用pptx實現(xiàn)復制頁面到其他PPT中
一、原理
如題,我有一個模板課件.pptx:

其內(nèi)容:

我想復制模板中間的某一頁多次,比如復制第1頁,然后復制3次,
prs = Presentation(r"D:\自動化\課件.pptx")
for i in range(0,3):
copied_slide = duplicate_slide(prs, 0)
次數(shù)是根據(jù)我的需求指定的,使用python pptx模塊復制,
def duplicate_slide(pres,index):
template = pres.slides[index]
blank_slide_layout = pres.slide_layouts[index]
copied_slide = pres.slides.add_slide(blank_slide_layout)
for shp in template.shapes:
el = shp.element
newel = copy.deepcopy(el)
copied_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
for _, value in six.iteritems(template.part.rels):
# 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
然后保存成另一個pptx文件
path = r'D:\自動化\result.pptx' prs.save(path)

復制后的ppt內(nèi)容

二、所有代碼
import copy,six
from pptx import Presentation
def duplicate_slide(pres,index):
template = pres.slides[index]
blank_slide_layout = pres.slide_layouts[index]
copied_slide = pres.slides.add_slide(blank_slide_layout)
for shp in template.shapes:
el = shp.element
newel = copy.deepcopy(el)
copied_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
for _, value in six.iteritems(template.part.rels):
# 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
prs = Presentation(r"D:\自動化\課件.pptx")
for i in range(0,3):
copied_slide = duplicate_slide(prs, 0)
path = r'D:\自動化\result.pptx'
prs.save(path)
到此這篇關于Python使用pptx實現(xiàn)復制頁面到其他PPT中的文章就介紹到這了,更多相關Python pptx復制PPT頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python把csv數(shù)據(jù)寫入list和字典類型的變量腳本方法
今天小編就為大家分享一篇Python把csv數(shù)據(jù)寫入list和字典類型的變量腳本方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Python數(shù)據(jù)分析之雙色球統(tǒng)計單個紅和藍球哪個比例高的方法
這篇文章主要介紹了Python數(shù)據(jù)分析之雙色球統(tǒng)計單個紅和藍球哪個比例高的方法,涉及Python數(shù)值運算及圖形繪制相關操作技巧,需要的朋友可以參考下2018-02-02
Pandas中數(shù)據(jù)表合并的幾種實現(xiàn)方法
Pandas提供了merge()、concat()和join()三種方法來合并數(shù)據(jù)表,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-12-12
Python實現(xiàn)將MySQL數(shù)據(jù)庫表中的數(shù)據(jù)導出生成csv格式文件的方法
這篇文章主要介紹了Python實現(xiàn)將MySQL數(shù)據(jù)庫表中的數(shù)據(jù)導出生成csv格式文件的方法,涉及Python針對mysql數(shù)據(jù)庫的連接、查詢、csv格式數(shù)據(jù)文件的生成等相關操作技巧,需要的朋友可以參考下2018-01-01
Python SqlAlchemy動態(tài)添加數(shù)據(jù)表字段實例解析
這篇文章主要介紹了Python SqlAlchemy動態(tài)添加數(shù)據(jù)表字段實例解析,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02

