利用Python自动生成PPT的示例详解
作者:阿涛的一天 时间:2021-10-16 18:25:08
在日常工作中,PPT制作是常见的工作,如果制作创意类PPT,则无法通过自动化的形式生成,因为创意本身具有随机性,而自动化解决的是重复性工作,两者有所冲突。
python-pptx是python处理PPT的一个库,注重的是读和写,无法导出,没有渲染功能。
废话不多说,第一步,安装python-pptx库:
pip3 install -i https://pypi.doubanio.com/simple/ python-pptx
ppt里面处理的主要对象一般为文本框,表格,图片。
每一页的ppt为一个slide
from pptx import Presentation, util
from pptx.util import Pt,Cm
from pptx.shapes.picture import Picture
#实例化一个ppt对象
ppt = Presentation("./test.pptx")
slide = ppt.slides[0] #第几页
然后遍历查看这一页ppt中都包含哪些对象:
def rander_template(slide):
for shape in slide.shapes:
if shape.has_text_frame == True:
print("==========================文本框=============================")
print("段落长度:",len(shape.text_frame.paragraphs))
for paragraph in shape.text_frame.paragraphs:
# 拼接文字
print("段落包含字段:",len(paragraph.runs))
print(''.join(run.text for run in paragraph.runs))
for i in range(len(paragraph.runs)):
print("run"+str(i)+":"+paragraph.runs[i].text)
print(shape.text_frame.paragraphs[0].runs[0].text)
shape.text_frame.paragraphs[0].runs[0].text = "规则是自由的第一要义"
elif shape.has_table == True:
print("==========================表格==============================")
one_table_data = []
for row in shape.table.rows: # 读每行
row_data = []
for cell in row.cells: # 读一行中的所有单元格
cell.text = cell.text if cell.text != "" else "未填写"
c = cell.text
row_data.append(c)
one_table_data.append(row_data) # 把每一行存入表
# 用二维列表输出表格行和列的数据
print(one_table_data)
print("第一个单元格内容:",shape.table.rows[0].cells[0].text)
elif isinstance(shape,Picture):
print("==========================图片==============================")
index = 0
with open(f'{index}.jpg','wb') as f:
f.write(shape.image.blob)
index += 1
文本框对象【text_frame】:
shape.has_text_frame查看是否有文本框对象,有的话查看具体有几个段落【len(shape.text_frame.paragraphs)】,每个段落又有多少个run对象【len(paragraph.runs)】
注意:修改run对象的时候,修改run[0],后面的值都会被覆盖。
表格对象【table】:
table对象还是按照行列值来定位划分的,eg:table.rows[2]cells[3].text代表第三行第四列的值
图片对象【Picture】:
插入图片需要固定图片的位置,比如:
def insert_pic(slide):
#需要用到pptx库的util方法
img_path = './blue.png' # 图片路径
# 设置图片的位置和大小
left = util.Cm(8.04)
top = util.Cm(9.93)
width = util.Cm(15.07)
height = util.Cm(4.06)
# 在页面中插入图片
slide.shapes.add_picture(img_path, left, top, width, height)
全部代码:
from pptx import Presentation, util
from pptx.util import Pt,Cm
from pptx.shapes.picture import Picture
ppt = Presentation("./test.pptx")
def rander_template(slide):
for shape in slide.shapes:
if shape.has_text_frame == True:
print("==========================文本框=============================")
print("段落长度:",len(shape.text_frame.paragraphs))
for paragraph in shape.text_frame.paragraphs:
# 拼接文字
print("段落包含字段:",len(paragraph.runs))
print(''.join(run.text for run in paragraph.runs))
for i in range(len(paragraph.runs)):
print("run"+str(i)+":"+paragraph.runs[i].text)
print(shape.text_frame.paragraphs[0].runs[0].text)
shape.text_frame.paragraphs[0].runs[0].text = "规则是自由的第一要义"
elif shape.has_table == True:
print("==========================表格==============================")
one_table_data = []
for row in shape.table.rows: # 读每行
row_data = []
for cell in row.cells: # 读一行中的所有单元格
cell.text = cell.text if cell.text != "" else "未填写"
c = cell.text
row_data.append(c)
one_table_data.append(row_data) # 把每一行存入表
# 用二维列表输出表格行和列的数据
print(one_table_data)
print("第一个单元格内容:",shape.table.rows[0].cells[0].text)
elif isinstance(shape,Picture):
print("==========================图片==============================")
index = 0
with open(f'{index}.jpg','wb') as f:
f.write(shape.image.blob)
index += 1
def insert_pic(slide):
img_path = './blue.png' # 图片路径
# 设置图片的位置和大小
left = util.Cm(8.04)
top = util.Cm(9.93)
width = util.Cm(15.07)
height = util.Cm(4.06)
# 在页面中插入图片
slide.shapes.add_picture(img_path, left, top, width, height)
if __name__ == "__main__":
slide = ppt.slides[0] #第几页
rander_template(slide)
insert_pic(slide)
ppt.save('new.pptx') # 保存为文件
初始ppt:
生成ppt:
来源:https://blog.csdn.net/weixin_44784088/article/details/124277314
标签:Python,自动,PPT
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
教你用python控制安卓手机
2023-11-10 10:13:39
![](https://img.aspxhome.com/file/2023/4/69554_0s.png)
Python对口红进行数据分析来选定情人节礼物
2022-01-23 07:38:08
![](https://img.aspxhome.com/file/2023/5/103365_0s.jpg)
Keras 多次加载model出错的解决方案
2023-11-01 20:52:35
python实现音乐播放和下载小程序功能
2023-07-03 17:59:03
![](https://img.aspxhome.com/file/2023/5/105855_0s.jpg)
浅谈keras中的后端backend及其相关函数(K.prod,K.cast)
2021-07-04 08:53:54
![](https://img.aspxhome.com/file/2023/7/108007_0s.jpg)
php下关于Cannot use a scalar value as an array的解决办法
2023-10-29 22:29:23
Tensorflow中使用cpu和gpu有什么区别
2021-10-15 15:48:45
![](https://img.aspxhome.com/file/2023/9/82769_0s.png)
基于Python计算圆周率pi代码实例
2021-03-06 05:13:17
![](https://img.aspxhome.com/file/2023/4/70244_0s.png)
phpmyadmin中禁止外网使用的方法
2023-09-12 01:10:22
数据库的选择原则是什么?
2010-07-14 21:05:00
php filter协议使用方法
2023-05-29 19:36:40
![](https://img.aspxhome.com/file/2023/5/55335_0s.png)
Python批量修改文件名实例操作分享
2022-04-11 06:14:47
![](https://img.aspxhome.com/file/2023/4/80674_0s.png)
基于Django框架的权限组件rbac实例讲解
2022-09-27 17:11:51
![](https://img.aspxhome.com/file/2023/2/72032_0s.jpg)
Django通过自定义认证后端实现多种登录方式验证
2022-09-21 20:15:40
python 子类调用父类的构造函数实例
2023-05-27 22:33:37
![](https://img.aspxhome.com/file/2023/5/70135_0s.jpg)
php简单定时执行任务的实现方法
2023-08-19 21:34:09
python+requests接口压力测试500次,查看响应时间的实例
2021-09-29 08:27:56
![](https://img.aspxhome.com/file/2023/3/68373_0s.jpg)
Python基于回溯法子集树模板解决马踏棋盘问题示例
2021-08-01 15:45:43
![](https://img.aspxhome.com/file/2023/6/105416_0s.jpg)
详解Python流程控制语句
2021-05-15 08:25:45
python多线程用法实例详解
2023-02-08 11:24:03