from pptx import Presentation
from pptx.util import Inches,Pt
#1. 创建一个ppt对象
ppt = Presentation()
#2. ppt中插入一个幻灯片slide
slide = ppt.slides.add_slide(ppt.slide_layouts[1])
#3. 初始化一个bodyshape,获取所有的占位符,可以理解为所有的文本框
body_shape = slide.shapes.placeholders
body_shape[0].text = ‘这是占位符[0]’
body_shape[1].text = ‘这是占位符[1]’
#3.1 取出标题文本框
title_shape = slide.shapes.title
title_shape.text = ‘这里是标题’
#3.2 取出本页第二个文本框
subtitle = slide.shapes.placeholders[1]
#在第二个文本框中写入文字
subtitle.text = ‘这里是文本框’
#3.3 设置文本的格式
new_paragraph = body_shape[1].text_frame.add_paragraph()
new_paragraph.text = ‘新段落’
new_paragraph.font.bold = True #文字加粗
new_paragraph.font.italic = True #文字斜体
new_paragraph.font.size = Pt(15) #文字大小
new_paragraph.font.underline = True #文字下划线
#3.4 添加一个新的文本框
left = Inches(2)
top = Inches(2)
width = Inches(3)
height = Inches(3)
#添加到所有的形状(文本框)里
textbox = slide.shapes.add_textbox(left,top,width,height)
textbox.text = ‘这里是文本框’
new_para = textbox.text_frame.add_paragraph()
new_para.text = ‘这里是文本框里面的第二段’
ppt.save(‘test.pptx’)