python操作PPT-----读取及写入PPT内容

# 安装 pip install python-pptx

from pptx import Presentation
from pptx.util import Cm, Pt


# 打开ppt
prs = Presentation('测试.pptx')
# 获取slide幻灯片
for slide in prs.slides:
    # 获取形状shape
    for shape in slide.shapes:
        if shape.has_text_frame:  # 判断是否有文字
            text_frame = shape.text_frame  # 获取文字框
            print(text_frame.text)
            # 从shape中找段落paragraphs
            for paragraph in text_frame.paragraphs:
                print(paragraph.text)


# 写入内容
prs = Presentation('空白演示.pptx')
slide = prs.slides.add_slide(prs.slide_layouts[0])  # 获取第一个母版
for shape in slide.placeholders:  # 获取这一页的所有占位符
    phf = shape.placeholder_format
    print(f'{phf.idx}--{shape.name}--{phf.type}')
    # 要写入的内容  shape.text=字符串
    shape.text = f'{phf.idx}--{phf.type}'
prs.save('test.pptx')  # 保存文件路径


# 根据占位符ID确定要填写哪里
prs = Presentation('空白演示.pptx')
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
# slide.placeholders[占位符ID]
center_title = slide.placeholders[0]  # 主标题
sub_title = slide.placeholders[1]  # 副标题
# 修改占位符里的内容
center_title.text = '欢迎你!'
sub_title.text = '热烈欢迎来到xxx'
prs.save('test.pptx')  # 保存文件路径


# 添加段落paragraph
prs = Presentation()
bullet_slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(bullet_slide_layout)
shapes = slide.shapes
title_shape = shapes.title
body_shape = shapes.placeholders[1]
title_shape.text = '添加项目符号列表页'
tf = body_shape.text_frame
tf.text = '带圆点的项目符号行1'
# 添加一个段落
p = tf.add_paragraph()
p.text = '带圆点的项目符号行2'
# 设定层级关系  0为最顶层
p.level = 1
p = tf.add_paragraph()
p.text = '带圆点的项目符号行2'
p.level = 2
prs.save('test2.pptx')


# 添加文本框
prs = Presentation()
blank_slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = width = height = Cm(3)
# slide.shapes.add_textbox(距离左边,距离顶端,宽度,高度)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
tf.text = '这是一段文本框里的文字'
p = tf.add_paragraph()
p.text = '这是第二段文字,加粗,字号20'
p.font.bold = True
p.font.size = Pt(20)
prs.save('test3.pptx')


# 添加图片
prs = Presentation()
blank_slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = Cm(3)
# slide.shapes.add_picture(图片路径,距离左边,距离顶端,宽度,高度)
pic = slide.shapes.add_picture('a.png', left, top)  # 原图片大小
width = Cm(5)
height = Cm(5.5)
pic = slide.shapes.add_picture('a.png', left, top, width=width, height=height)  # 指定图片大小
prs.save('test4.pptx')


# 添加表格
prs = Presentation()
blank_slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(blank_slide_layout)
rows, cols = 3, 2  # 要添加的表格的行数、列数
left = top = Cm(3)
width = Cm(10)
height = Cm(6)
table = slide.shapes.add_table(rows, cols, left, top, width, height).table
table.columns[0].width = Cm(4)  # 第一列宽
table.columns[1].width = Cm(3)  # 第二列宽
table.rows[0].height = Cm(2)  # 第一行高
data = [
    ['a', 1],
    ['b', 2],
    ['c', 3],
]
for row in range(rows):
    for col in range(cols):
        table.cell(row, col).text = str(data[row][col])
prs.save('test5.pptx')

 

posted @ 2020-09-04 19:07  不会飞的鲨鱼  阅读(5486)  评论(0编辑  收藏  举报