5. ppt 基础操作

# PPT 基本操作
"""
slide(幻灯片): 一个ppt由一系列的slide构成
slide_master(幻灯片母版): 母版可定义主题样式基准
slide_layouts(模板): 创建幻灯片时可选择的模板
shape(形状): 包含一切可视元素,包含 slide,shapes 可访问 slide 内元素
placeholder(占位符): 在模板中占据位置,如图片、文字等
paragraph(段落): 文本段,可以直接设置整段文本样式
"""

# pip install python-pptx

def base_ppt(path):
    from pptx import Presentation

    # 创建ppt
    ppt = Presentation()

    # Offfice Theme 新建幻灯片的主题模板第1个
    slide = ppt.slides.add_slide(ppt.slide_layouts[0])
    slide = ppt.slides.add_slide(ppt.slide_layouts[1])
    slide = ppt.slides.add_slide(ppt.slide_layouts[2])
    slide = ppt.slides.add_slide(ppt.slide_layouts[3])
    # Offfice Theme 新建幻灯片的主题模板第五个
    slide = ppt.slides.add_slide(ppt.slide_layouts[4])

    ppt.save(path)

def create_ppt(path):
    from pptx import Presentation
    from pptx.util import Pt

    # 创建ppt
    ppt = Presentation()

    slide = ppt.slides.add_slide(ppt.slide_layouts[1])
    # 获取内容框
    shapes = slide.shapes

    title = shapes.title
    title.text = 'This is Python-pptx Title !'

    content = shapes.placeholders[1]
    content.text = 'Content Info'

    slide2 = ppt.slides.add_slide(ppt.slide_layouts[1])
    shapes2 = slide2.shapes
    content = shapes2.placeholders[1]
    # tf = content.text_frame
    # p = tf.add_paragraph() # 首行会增加一个空行
    # p.text = "这是内容1"
    content.text_frame.text = "这是内容1"
    content.text_frame.level = 1  # 层级关系设置

    tf = content.text_frame

    p1 = tf.add_paragraph()
    p1.text = "这是内容2"
    p1.level = 2

    p2 = tf.add_paragraph()
    p2.text = "这是内容3"
    p2.level = 3

    p3 = tf.add_paragraph()
    p3.text = "这是内容4"
    p3.font.bold = True
    p3.font.size = Pt(30)

    slide3 = ppt.slides.add_slide(ppt.slide_layouts[6])  # 自由模板
    left = top = width = height = Pt(200)
    text_box = slide3.shapes.add_textbox(left, top, width, height)
    tf3 = text_box.text_frame
    tf3.text = '这是文本框内容!'

    p4 = tf3.add_paragraph()
    p4.text = "这是内容5"
    p4.font.bold = True
    p4.font.size = Pt(30)

    ppt.save(path)

# ppt 增加图片
def add_pic(path):
    from pptx import Presentation
    from pptx.util import Pt

    ppt = Presentation()
    slide = ppt.slides.add_slide(ppt.slide_layouts[6])
    shapes = slide.shapes
    left = top = Pt(50)
    height = Pt(300)
    shapes.add_picture(
        'D:\\git-python\\前端\\static\\img\\haha.png', left, top, height)

    ppt.save(path)

# 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)

def get_ppt(path):
    from pptx import Presentation
    from pptx.enum.shapes import MSO_SHAPE_TYPE

    # 打开待读取的ppt文件
    ppt = Presentation(path)

    # 获取第0张
    slide0 = ppt.slides[0]

    # 遍历所有内容
    for shape in slide0.shapes:
        # 打印shape名称
        print(shape.shape_type)
        # 判断是否为表格
        if shape.shape_type == MSO_SHAPE_TYPE.TABLE:
            #获取表格行
            for row in shape.table.rows:
                for cell in row.cells:
                    print(cell.text_frame.text)

if __name__ == '__main__':
    # base_ppt('./办公自动化/files/base_ppt.pptx')
    # create_ppt('./办公自动化/files/create_ppt.pptx')
    # add_pic('./办公自动化/files/ppt_test.pptx')
    # add_flow_chart('./办公自动化/files/ppt_flow_chart.pptx')
    get_ppt('./办公自动化/files/ppt_flow_chart.pptx')