用Python自动生成PPT的步骤操作是什么
Admin 2022-09-06 群英技术资讯 519 次浏览
在日常工作中,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:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
pandas里怎样处理缺失值为NaN的情况?Python中我们用pandas处理数据非常的方便,但是也常会遇到一些问题。例如andas中NaN缺失值的情况,一些新手朋友可能不太清楚怎样解决,下面给大家分享两种处理pandas中NaN缺失值的方法,有需要的朋友可以参考。
这篇文章主要为大家介绍了python密码学列置换密码学习的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
Python文件路径乱码的解决方法是怎么,代码怎么写
用python处理中文,读取文件或消息时,如果发现乱码(字符串处理,读写文件,print),大多数人的做法是,调用encode decode进行调试,并没有
Python函数-complex()。complex() 函数用于创建一个值为 real + imag * j 的复数或者转化一个字符串或数为复数。如果第一个参数为字符串,则不需要指定第二个参数。。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008