python-pptx的使用首先需要了解几个基本概念:
1.引入python-pptx
frompptximportpresentation
# 实例化Presentation
prs= Presentation()
2.ppt模板的选择
a、使用ppt自带的模板
prs= Presentation()
prs.slide_layouts[index]
ppt自带了常用的1-48种模板通过index选择对应的模板
b、使用自定义ppt模板
prs= Presentation('template.pptx')
3.新建一页幻灯片
slide= prs.slides.add_slide(prs.slide_layouts[1])
# prs.slides.add_slide()增加一页幻灯片方法
4.编辑幻灯中的元素
a、根据placeholdes索引获取一页幻灯片中的元素
body_shape= slide.shapes.placeholders
# body_shape为本页ppt中所有shapes
body_shape[0].text= 'this is placeholders[0]'
body_shape[1].text= 'this is placeholders[1]'
在ppt中所有的元素均被当成一个shape,slide.shapes表示幻灯片类中的模型类,placeholders中为每个模型,采用slide_layouts[1]中包含两个文本框,所以printlen(slide.shapes.placeholders) 话为2