# ppt 增加流程图
def add_flow_chart(path):
from pptx import Presentation
from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPE
from pptx.enum.text import PP_ALIGN
from pptx.dml.color import RGBColor
from pptx.util import Inches, Pt
ppt = Presentation()
slide = ppt.slides.add_slide(ppt.slide_layouts[5])
shapes = slide.shapes
shapes.title.text = '流程图'
left = Inches(0.7)
top = Inches(2)
width = Inches(2)
height = Inches(1)
for n in range(1, 6):
# 添加箭头形状
shape = shapes.add_shape(autoshape_type_id=MSO_AUTO_SHAPE_TYPE.CHEVRON, # v 形箭头
left=left,
top=top,
width=width,
height=height)
# shape.text = 'Step %d' % n # 文本颜色默认为白色,位于形状中心
p = shape.text_frame
p.text = 'Step %d' % n # 文本颜色默认为白色,位于形状中心
p.fit_text(font_file=r"c:\windows\fonts\calibriz.ttf",
max_size=20, bold=True, italic=True)
# 添加文本框
textbox = shapes.add_textbox(left=left,
top=top + height + Inches(0.3),
width=width,
height=Inches(4)
)
tf = textbox.text_frame
para = tf.add_paragraph()
para.text = 'do\n\n\n something'
para.alignment = PP_ALIGN.CENTER # 居中
# 设置字体属性
font = para.font
font.size = Pt(24) # 大小
font.name = 'Arial' # 字体
font.bold = True # 加粗
font.italic = True # 倾斜
font.color.rgb = RGBColor(225, 225, 0) # 黄色
# 更新位置参数
left = left + width - Inches(0.3)
ppt.save(path)
if __name__ == '__main__':
add_flow_chart('./办公自动化/files/ppt_flow_chart.pptx')