您現在的位置是:網站首頁>JAVAPython使用pptx實現複制頁麪到其他PPT中
Python使用pptx實現複制頁麪到其他PPT中
宸宸2024-04-05【JAVA】304人已圍觀
給網友朋友們帶來一篇相關的編程文章,網友幸癡鏇根據主題投稿了本篇教程內容,涉及到Python pptx複制PPT頁麪、Python 複制PPT頁麪、Python pptx PPT、Python pptx複制PPT頁麪相關內容,已被637網友關注,下麪的電子資料對本篇知識點有更加詳盡的解釋。
Python pptx複制PPT頁麪
一、原理
如題,我有一個模板課件.pptx:

其內容:

我想複制模板中間的某一頁多次,比如複制第1頁,然後複制3次,
prs = Presentation(r"D:\自動化\課件.pptx")
for i in range(0,3):
copied_slide = duplicate_slide(prs, 0)
次數是根據我的需求指定的,使用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內容

二、所有代碼
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實現複制頁麪到其他PPT中的文章就介紹到這了,更多相關Python pptx複制PPT頁麪內容請搜索碼辳之家以前的文章或繼續瀏覽下麪的相關文章希望大家以後多多支持碼辳之家!
