python-pptx 实践 4:添加形状、文本

1、添加流程箭头

# 加载库
import  os
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.enum.text import PP_ALIGN
from pptx.enum.shapes import MSO_SHAPE
from pptx.dml.color import RGBColor# 设置路径
work_path = r'E:\pyspace\tmp\pptx'
os.chdir(work_path)

# 实例化 ppt 文档对象
prs = Presentation()

# 插入幻灯片
title_only_slide = prs.slide_layouts[5]
slide_1 = prs.slides.add_slide(title_only_slide)

shapes = slide_1.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_SHAPE.CHEVRON,    # v 形箭头
                             left=left,
                             top=top,
                             width=width,
                             height=height)
    shape.text = 'Step %d' % n    # 文本颜色默认为白色,位于形状中心
    
    # 添加文本框
    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
prs.save('test.pptx')

效果:

 

 35 种不同 autoshape_type_id 参数对应的形状

1、autoshape_type_id= MSO_SHAPE.ARC

2、 autoshape_type_id= MSO_SHAPE.BALLOON

3、autoshape_type_id= MSO_SHAPE.BEVEL

4、autoshape_type_id= MSO_SHAPE.CAN

5、autoshape_type_id= MSO_SHAPE.CHEVRON

6、autoshape_type_id= MSO_SHAPE.CHORD

7、autoshape_type_id= MSO_SHAPE.CLOUD

8、autoshape_type_id= MSO_SHAPE.CLOUD_CALLOUT

 9、autoshape_type_id= MSO_SHAPE.CUBE

 10、 autoshape_type_id= MSO_SHAPE.CURVED_DOWN_RIBBON

  11、autoshape_type_id= MSO_SHAPE.CURVED_UP_RIBBON

12、autoshape_type_id= MSO_SHAPE.DECAGON

13、autoshape_type_id= MSO_SHAPE.DIAMOND

14、autoshape_type_id= MSO_SHAPE.DOUBLE_WAVE

  15、autoshape_type_id= MSO_SHAPE.DOWN_ARROW

 16、autoshape_type_id= MSO_SHAPE.EXPLOSION1

17、autoshape_type_id= MSO_SHAPE.FLOWCHART_CONNECTOR

18、autoshape_type_id= MSO_SHAPE.FLOWCHART_DECISION

 19、autoshape_type_id= MSO_SHAPE.FLOWCHART_DIRECT_ACCESS_STORAGE

 20、autoshape_type_id= MSO_SHAPE.FLOWCHART_DOCUMENT

 21、 autoshape_type_id= MSO_SHAPE.FLOWCHART_MAGNETIC_DISK

 22、autoshape_type_id= MSO_SHAPE.FLOWCHART_MULTIDOCUMENT

 23、autoshape_type_id= MSO_SHAPE.FLOWCHART_PUNCHED_TAPE

24、autoshape_type_id= MSO_SHAPE.FLOWCHART_STORED_DATA

 25、autoshape_type_id= MSO_SHAPE.FUNNEL

 26、autoshape_type_id= MSO_SHAPE.HEART

27、autoshape_type_id= MSO_SHAPE.MOON

28、autoshape_type_id= MSO_SHAPE.OVAL

 29、autoshape_type_id= MSO_SHAPE.OVAL_CALLOUT

30、autoshape_type_id= MSO_SHAPE.SMILEY_FACE

31、autoshape_type_id= MSO_SHAPE.STAR_5_POINT

 32、autoshape_type_id= MSO_SHAPE.STAR_6_POINT

33、autoshape_type_id= MSO_SHAPE.SUN

 34、 autoshape_type_id= MSO_SHAPE.TEAR

  35、autoshape_type_id= MSO_SHAPE.WAVE

 

posted @ 2020-02-14 22:16  赏尔  阅读(2853)  评论(0编辑  收藏  举报