一、python-pptx简介 #
python-pptx 是Python中用于创建和操作PowerPoint(.pptx)文件的强大库,支持文本、图片、表格、图表等多种元素操作。
二、安装方法 #
介绍如何安装python-pptx及其依赖。
# 安装python-pptx主库
pip install python-pptx三、基本用法 #
本节介绍如何用python-pptx创建PPT、添加幻灯片、插入文本和图片。
1. 创建PPT并添加幻灯片 #
展示如何新建PPT文件并添加幻灯片。
`python
导入Presentation类 #
from pptx import Presentation
创建一个新的PPT文档 #
prs = Presentation()
添加标题幻灯片(第0种布局) #
slide = prs.slides.add_slide(prs.slide_layouts[0])
保存PPT文件 #
prs.save('demo.pptx')
### 2. 插入文本框和文本
> 展示如何在幻灯片中插入文本框和文本内容。
```python
# 导入Presentation类
from pptx import Presentation
# 创建PPT文档
prs = Presentation()
# 添加空白幻灯片
slide = prs.slides.add_slide(prs.slide_layouts[5])
# 添加文本框,设置位置和大小(左,上,宽,高,单位为英寸)
from pptx.util import Inches
left = Inches(2)
top = Inches(2)
width = Inches(4)
height = Inches(1)
textbox = slide.shapes.add_textbox(left, top, width, height)
# 获取文本框中的文本框架
text_frame = textbox.text_frame
# 添加文本内容
text_frame.text = "Hello, python-pptx!"
# 保存PPT文件
prs.save('text_demo.pptx')3. 插入图片 #
展示如何在幻灯片中插入图片。
`python
导入Presentation类 #
from pptx import Presentation
创建PPT文档 #
prs = Presentation()
添加空白幻灯片 #
slide = prs.slides.add_slide(prs.slide_layouts[5])
插入图片,设置位置和大小 #
from pptx.util import Inches img_path = 'example.png' # 替换为你的图片路径 left = Inches(1) top = Inches(1) width = Inches(4) height = Inches(3) slide.shapes.add_picture(img_path, left, top, width, height)
保存PPT文件 #
prs.save('image_demo.pptx')
## 四、进阶用法
> 本节介绍表格、图表、母版和样式等高级用法。
### 1. 添加表格
> 展示如何在幻灯片中插入表格。
```python
# 导入Presentation类
from pptx import Presentation
# 创建PPT文档
prs = Presentation()
# 添加空白幻灯片
slide = prs.slides.add_slide(prs.slide_layouts[5])
# 设置表格位置和大小
from pptx.util import Inches
rows, cols = 3, 2
left = Inches(1)
top = Inches(1)
width = Inches(4)
height = Inches(1.5)
# 添加表格
table = slide.shapes.add_table(rows, cols, left, top, width, height).table
# 填充表头
table.cell(0, 0).text = '姓名'
table.cell(0, 1).text = '年龄'
# 填充数据
table.cell(1, 0).text = '张三'
table.cell(1, 1).text = '23'
table.cell(2, 0).text = '李四'
table.cell(2, 1).text = '25'
# 保存PPT文件
prs.save('table_demo.pptx')2. 添加图表(柱状图) #
展示如何在幻灯片中插入柱状图。
`python
导入Presentation类 #
from pptx import Presentation
创建PPT文档 #
prs = Presentation()
添加空白幻灯片 #
slide = prs.slides.add_slide(prs.slide_layouts[5])
设置图表位置和大小 #
from pptx.util import Inches left = Inches(1) top = Inches(1) width = Inches(5) height = Inches(3)
导入图表相关模块 #
from pptx.chart.data import CategoryChartData from pptx.enum.chart import XL_CHART_TYPE
构造图表数据 #
chart_data = CategoryChartData() chart_data.categories = ['A', 'B', 'C'] chart_data.add_series('销量', (10, 20, 15))
添加柱状图 #
chart = slide.shapes.add_chart( XL_CHART_TYPE.COLUMN_CLUSTERED, left, top, width, height, chart_data ).chart
保存PPT文件 #
prs.save('chart_demo.pptx')
### 3. 设置幻灯片母版和样式
> 展示如何使用母版和设置字体样式。
```python
# 导入Presentation类
from pptx import Presentation
# 创建PPT文档
prs = Presentation()
# 获取第一个幻灯片母版
slide_master = prs.slide_master
# 添加标题幻灯片
slide = prs.slides.add_slide(prs.slide_layouts[0])
# 设置标题文本和字体样式
title = slide.shapes.title
title.text = "python-pptx演示"
# 设置字体大小和颜色
from pptx.util import Pt
from pptx.dml.color import RGBColor
run = title.text_frame.paragraphs[0].runs[0]
run.font.size = Pt(32)
run.font.bold = True
run.font.color.rgb = RGBColor(0, 102, 204)
# 保存PPT文件
prs.save('style_demo.pptx')五、常见应用场景 #
python-pptx 常用于以下场景:
- 自动化批量生成PPT报告
- 数据可视化与演示文档生成
- 教学课件、会议资料自动化制作
- 企业报表、产品宣传等PPT自动化
六、参考资料 #
推荐学习和查阅的python-pptx相关资料。