利用Python自动生成PPT的示例详解
作者:阿涛的一天 发布时间:2021-10-16 18:25:08
标签:Python,自动,PPT
在日常工作中,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


猜你喜欢
- Mac安装软件时提示已损坏的解决方法从网上下载的SecureCRT、Principle等设计软件,以及输入法等常用软件,安装时可能会提示&a
- 背景Python 作为一门成熟的编程语言,拥有无数优秀的第三方包以方便开发者能够快速地构建应用。一般来说,如果你开发了一个 Python 软
- <script type="text/javascript"> // Close HTML Tags ---
- 在我们建立一个数据库时,并且想将分散在各处的不同类型的数据库分类汇总在这个新建的数据库中时,尤其是在进行数据检验、净化和转换时,将会面临很大
- 占位符,顾名思义就是插在输出里站位的符号。占位符是绝大部分编程语言都存在的语法, 而且大部分都是相通的, 它是一种非常常用的字符串格式化的方
- 概述全链接层 (Fully Connected Layer) 会把一个特质空间线性变换到另一个特质空间, 在整个网络中起到分类器的作用.ke
- 进程什么是进程进程指的是一个程序的运行过程,或者说一个正在执行的程序所以说进程一种虚拟的概念,该虚拟概念起源操作系统一个CPU 同一时刻只能
- 小编使用python中的django框架来完成!1,首先用pycharm创建django项目并配置相关环境这里小编默认项目都会创建setti
- cursor就是一个Cursor对象,这个cursor是一个实现了迭代器(def__iter__())和生成器(yield)的MySQLdb
- 本文研究的主要是python通过elixir包操作mysql数据库的相关实例,具体如下。python操作数据库有很多方法,下面介绍elixi
- 前言因为Python的水平目前一直是处于能用阶段,平时写的脚本使用的Python的写法也比较的简单,没有写过稍微大一点的项目。对Python
- 在FF下,如果内容含有FLASH,FLASH不会被隐藏掉,做为变通,我在收缩完成之后,将生成的内容容器隐藏掉了。<!DOCTYPE h
- Transact-SQL(又称T-SQL),是在Microsoft SQL Server和Sybase SQL
- 概述在我们使用内置打印函数print时,打印出的Python数据结构对象总是一行的输出的方式,这样对数据结构较复杂或数据较多的对象的显示并不
- 运行下面的代码你就可以清楚的认识到这两个参数的用法,innerText只能动态的改变指定元素内的文本内容,而innerHTML则不仅仅可以改
- 操作说明:选择多个PDF文件,执行完合并后会生成一个新的PDF文件,这个新的PDF文件包含所有源PDF文件的页面。将相关的三方模块导入到代码
- 1. 同线性代数中矩阵乘法的定义: np.dot()np.dot(A, B):对于二维矩阵,计算真正意义上的矩阵乘积,同线性代数中矩阵乘法的
- 前几天,使用python时遇到这么一个需求,删除一个列表中值为1的元素。我寻思着使用remove方法,但是remove方法只会删除第一个,于
- mysql 索引详解:在mysql 中,索引可以分为两种类型 hash索引和 btree索引。 什么情况下可以用到B树索引?&nb
- 项目是基于vue2 的移动端项目,供大家参考,具体内容如下1、实际效果地址 * 联动 mint-ui picker.png2、首先你需要去下载