使用python-pptx操作PPT的示例详解

作者:肖永威 时间:2022-06-01 21:49:20 

python对PPT演示文档读写,是通过第三方库python-pptx实现的,python-pptx是用于创建和更新 PowerPoint(.pptx)文件的 Python 库。

关于PPT演示文档与幻灯片模板的内容不是本文的重点,在此略过。

1. PPT基本结构在python-pptx下定义

1.1. 演示文档结构定义

python-pptx对ppt结构的描述如下图所示,演示文档由多个幻灯片(slide)构成,每个幻灯片由众多各种形状(shape)组成。

  • Slide:幻灯片,就是演示文稿中每一页的页面。

  • Shape:形状,在每页幻灯片内插入的方框,可以是形状,也可以是文本框、图片、表格等等。

  • Run:文字块,一般为较少字符。

  • Paragraph:段落,通常有序号ㆍ、1.等。

使用python-pptx操作PPT的示例详解

图示演示文档,如下图所示,由3页幻灯片(slide)构成,其中,第三页幻灯片中的形状(shape)分别是“标题 1”(Title 1)和“图片”(Picture Placeholder 2)组成。

使用python-pptx操作PPT的示例详解

1.2. 自定义幻灯片母版

使用程序生成演示文档,最好先自定义幻灯片母版,如下图所示,定义4页模板(slide_layouts)。

使用python-pptx操作PPT的示例详解

注意:shape名称在office软件下,是中文,而程序读出来的可能是英文!

使用python-pptx操作PPT的示例详解

对于母版内容:

Slides_layouts:版式,一个幻灯片母版由多个版式组成,索引从0开始。

slide_layouts[]传入0表示获取的是第一个版式,传入1表示获取的是第二个版式

Placeholder:占位符:存在PPT母版里面的幻灯片的某一部件:Placeholder

2. python-pptx操作PPT实践

2.1. 安装python-pptx

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

2.2. 读取PPT演示文档

from pptx import Presentation

# 打开演示文档
prs = Presentation('加油站全流程诊断大数据系统.pptx')
for slide in prs.slides:
   print(slide)

# 写入新文件中
prs.save('test.pptx')

# 获取某一页Slide中的内容

for i, slide in enumerate(prs.slides):
   if i == 3:
       for shape in slide.shapes:
           if shape.has_text_frame:
               text_frame = shape.text_frame
               print(text_frame.text)

2.3. 基于模板创建新的演示文档

这里所说模板仍然是pptx文件,不是PPT的模板(potx),python-pptx无法读取potx模板。也是说,使用仅有一页,并且无内容的空演示文档,内含自定义幻灯片母版,如前面所述的母版样例。

创建新演示文档过程如下:

  • 首先,修改首页内容,例如主题和副主题

  • 接着,按实际要求,使用具体模板,也就是slide_layouts

代码过程如下:

from pptx import Presentation
from PIL import Image

im=Image.open('d:\\02资料\\AI无感加油.png')

# 修改首页
prs= Presentation('template_1.pptx')
slide = prs.slides[0]
title = ['油站全流程诊断输出测试页',
       '测试页副标题']
for i, shape in enumerate(slide.shapes):
   if shape.has_text_frame:
       text_frame = shape.text_frame
       text_frame.text = title[i]

# 插入内容测试
slide = prs.slides.add_slide(prs.slide_layouts[1])  # 用第一个母版生成一页ppt
for shape in slide.placeholders:         # 获取这一页所有的占位符
   phf = shape.placeholder_format
   print(f'{phf.idx}--{shape.name}--{phf.type}')
   print('shape name ', shape.name)
   if shape.name ==  'Title 1':
       shape.text = f'目标'  #在标题占位符中填写“目标”
   else:
       shape.text = f'内容'  #在其他占位符中填写“内容”        

# 插入图片测试
im.save('tmp.png') #从外部(数据库)读取的图片,临时存储
slide = prs.slides.add_slide(prs.slide_layouts[2])  # 用第一个母版生成一页ppt
for shape in slide.placeholders:         # 获取这一页所有的占位符
   phf = shape.placeholder_format
   print(f'{phf.idx}--{shape.name}--{phf.type}')
   print('shape name ', shape.name)
   if shape.name ==  'Title 1':
       shape.text = f'插入图片测试'  #在标题占位符中填写“目标”
   else:
       shape.insert_picture('tmp.png')  #在其他占位符中填写“图片”

prs.save('向占位符内填写内容_1.pptx')

附加内容:列出对象属性。

for i, shape in enumerate(slide.shapes):
   print('对象类型', shape.shape_type)
   print('对象属性列表', dir(shape))

shape部分属性列表:

  • ‘has_chart’,

  • ‘has_table’,

  • ‘has_text_frame’,

  • ‘height’,

  • ‘is_placeholder’,

  • ‘name’,

  • ‘part’,

  • ‘placeholder_format’,

  • ‘shape_id’,

  • ‘shape_type’,

  • ‘text’,

  • ‘text_frame’,

  • ‘top’,

  • ‘width’

3. 小结

对于数据分析结果规范化输出,python-pptx功能基本满足,使用条件是精通PPT,设计出合适的母版,供程序交互使用。

来源:https://blog.csdn.net/xiaoyw71/article/details/128639919

标签:python-pptx,PPT
0
投稿

猜你喜欢

  • sqlserver 日期比较、日期查询常用语句:月的第一天,季度的第一天等

    2010-08-01 18:58:00
  • Python 2种方法求某个范围内的所有素数(质数)

    2022-09-07 08:43:40
  • Python如何破解压缩包密码

    2023-09-01 19:15:41
  • Python2和Python3的共存和切换使用

    2022-12-26 13:05:20
  • 细说Go语言中空结构体的奇妙用途

    2024-04-23 09:46:09
  • 分享216色网页拾色器(调色板)

    2007-09-27 12:33:00
  • Python random库使用方法及异常处理方案

    2023-10-07 13:43:46
  • Sql Server datetime问题

    2024-01-27 03:06:25
  • Django视图、传参和forms验证操作

    2023-03-04 09:17:03
  • ASP无组件汉字验证码

    2008-05-08 13:19:00
  • Python中的pathlib库使用详解

    2023-07-22 02:09:24
  • js中跨域方法原理详解

    2024-06-13 20:33:00
  • 互联网产品交互事件分析

    2009-10-06 15:23:00
  • Python 函数绘图及函数图像微分与积分

    2021-07-13 22:53:14
  • 用Python遍历C盘dll文件的方法

    2023-04-27 20:15:27
  • python适合做数据挖掘吗

    2021-03-25 00:42:58
  • JS鼠标事件大全 推荐收藏

    2024-05-28 15:41:00
  • python游戏库pygame经典教程(推荐!)

    2022-10-02 06:26:11
  • Python中functools模块函数解析

    2021-03-11 10:15:40
  • 基于Python socket实现简易网络聊天室

    2021-10-19 09:10:30
  • asp之家 网络编程 m.aspxhome.com