在日常工作中,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:
关于Python学习指南
学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后给大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
包括:Python激活码+安装包、Python web开发,Python爬虫,Python数据分析,人工智能、自动化办公等学习教程。带你从零基础系统性的学好Python!
👉Python所有方向的学习路线👈
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。(全套教程文末领取)
👉Python学习视频600合集👈
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
温馨提示:篇幅有限,已打包文件夹,获取方式在:文末
👉Python70个实战练手案例&源码👈
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
👉Python大厂面试资料👈
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
👉Python副业兼职路线&方法👈
学好 Python 不论是就业还是做副业赚钱都不错,但要学会兼职接单还是要有一个学习规划。
👉 这份完整版的Python全套学习资料已经上传,朋友们如果需要可以扫描下方CSDN官方认证二维码或者点击链接免费领取【保证100%免费
】
![](https://img-blog.csdnimg.cn/img_convert/c683b2c8f75b0ae2f5e965822bc58077.png)