Python自动化办公PPT-写入内容

1. 安装模块

Windows用户打开命令行输入:pip install python-pptx

Mac用户打开终端/Terminal输入:pip3 install python-pptx

使用windows系统,如果出现无法安装情况,可以在cmd模式下输入网址选择国内清华镜像。

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple python-pptx

导入模块:import pptx

2. 读取PPT文档内容

1)新建文档,确认占位符类型

from pptx import Presentation

prs = Presentation() #初始化一个空pptx文档
slide = prs.slides.add_slide(prs.slide_layouts[0])  # 用第一个母版生成一页ppt
for shape in slide.placeholders:         # 获取这一页所有的占位符
    phf = shape.placeholder_format
    print(f'{phf.idx}--{shape.name}--{phf.type}')  # id号--占位符形状名称-占位符的类型
    shape.text = f'{phf.idx}--{shape.name}--{phf.type}'
prs.save("写入内容.pptx")

输出结果:

0–Title 1–CENTER_TITLE (3)
1–Subtitle 2–SUBTITLE (4)

加入while循环,可以一次生成所有版式:

from pptx import Presentation

prs = Presentation() #初始化一个空pptx文档
i = 0
while i <= 10:
    slide = prs.slides.add_slide(prs.slide_layouts[i])  
    i = i+1
    for shape in slide.placeholders:         # 获取这一页所有的占位符
        phf = shape.placeholder_format
        print(f'{phf.idx}--{shape.name}--{phf.type}')  # id号--占位符形状名称-占位符的类型
        shape.text = f'{phf.idx}--{shape.name}--{phf.type}'
prs.save("写入内容.pptx")

得到占位符类型如下:

Title 标题
Subtitle 副标题
Body 正文
Content 文本
Text 文本
Picture 图片
Vertical Text 竖排文本

在这里插入图片描述
2)向占位符中添加内容

实际的写入操作中,占位符类型不一定严格来写。只是用于同时写入较多的内容时便于区分。
① 添加文字
通用模式:body_shape

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

prs = Presentation()
# 插入幻灯片,布局slide_layout为母版的第二个版式
slide = prs.slides.add_slide(prs.slide_layouts[10])

# 向占位符中添加文本,前提是占位符必须存在。
body_shape = slide.shapes.placeholders
body_shape[0].text = '这是占位符【0】'
body_shape[1].text = '这是占位符【1】'

prs.save("写入内容.pptx")

模式一:title

from pptx import Presentation

prs = Presentation()
# 插入幻灯片,布局slide_layout为母版的第二个版式
slide = prs.slides.add_slide(prs.slide_layouts[1])
body_shape = slide.shapes.placeholders


title_shape = slide.shapes.title
title_shape.text = '这是一个标题'
subtitle = slide.shapes.placeholders[1]
subtitle.text = '这是一个副标题'
prs.save("写入内容.pptx")

② 添加新段落

from pptx import Presentation

prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[1])
title_shape = slide.shapes.title
title_shape.text = '这是一个标题'
subtitle = slide.shapes.placeholders[1]
subtitle.text = '这是一个文本框'
shapes = slide.shapes

# 添加新段落
new_paragraph = subtitle.text_frame.add_paragraph()
new_paragraph.text = '新段落'

prs.save("写入内容.pptx")

③ 添加新的文本框

from pptx import Presentation
from pptx.util import Inches,Pt,Cm
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[1])

# 添加新文本框
left = top = width = height =Cm(10)
text_box = slide.shapes.add_textbox(left,top,width,height)
text_box.text = '这是一个新文本框'

prs.save("写入内容.pptx")

参考:
Python自动化办公 - 对PPT的操作(Python-pptx的基本使用
Python从菜鸟到高手(10):循环

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值