【笔记】Python3操作pptx

前言

Python3通过python-docx操作pptx实现办公自动化

下载依赖

1
pip3 install python-pptx

引入依赖

1
from pptx import Presentation

打开一个已有的pptx文档

<filename>:指定已存在的文件名

1
presentation = Presentation("<filename>.pptx")

通过文件对象获取帧对象

获取所有帧列表

1
slide_list = presentation.slides

获取帧的个数

1
len(slide_list)

获取指定帧

<index>:帧下标

1
slide_list[<index>]

遍历所有帧

1
2
for slide in slide_list:
pass

通过帧对象获取样式对象

获取所有样式列表

1
shape_list = slide.shapes

获取样式个数

1
len(shape_list)

获取指定样式

1
shape_list[<index>]

遍历所有样式

1
2
for shape in shape_list:
pass

通过样式对象填充背景色

1
2
3
4
5
from pptx.dml.color import RGBColor

fill = shape.fill
fill.solid()
fill.fore_color.rgb = RGBColor(255, 255, 255)

通过样式对象绘制文本框

  • 设置文本框颜色和文本框粗细
1
2
3
4
5
6
from pptx.util import Cm
from pptx.dml.color import RGBColor

line = shape.line
line.color.rgb = RGBColor(255, 255, 255)
line.width = Cm(1)

通过样式对象获取文本框对象

判断是否存在文本框

1
exist = shape.has_text_frame

获取文本框

1
text_frame = shape.text_frame

通过文本框对象获取文本内容

1
text_frame.text

通过文本框对象获取段落对象

获取所有的段落列表

1
paragraph_list = shape.text_frame.paragraphs

获取段落个数

1
len(paragraph_list)

遍历所有段落

1
2
for paragraph in paragraph_list:
pass

通过段落对象获取文本对象

获取文本内容

1
paragraph.text

修改文本内容

1
paragraph.text = ""

设置文字属性

字体

  • 通过字体名定义字体
1
paragraph.font.name = ""

字号

1
2
3
from pptx.util import Pt

paragraph.font.size = Pt(1)

文字颜色

RGBColor(<red>, <green>, <blue>):设置RGB颜色,每个颜色的取值范围为0~255

1
2
3
from pptx.dml.color import RGBColor

paragraph.font.color.rgb = RGBColor(255, 255, 255)

加粗

1
paragraph.font.bold = True

斜体

1
paragraph.font.italic = True

下划线

1
paragraph.font.underline = True

设置段落属性

行间距

1
2
3
from pptx.util import Cm

paragraph.line_space = Cm(1)

段落前面的空白

1
2
3
from pptx.util import Cm

paragraph.space_before = Cm(1)

段落前面的空白

1
2
3
from pptx.util import Cm

paragraph.space_after = Cm(1)

段落对齐方式

1
2
3
4
5
from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT

paragraph.alignment = PP_PARAGRAPH_ALIGNMENT.LEFT
paragraph.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER
paragraph.alignment = PP_PARAGRAPH_ALIGNMENT.RIGHT

通过段落对象获取文本块对象

获取所有文本块列表

1
run_list = paragraph.runs

获取文本块个数

1
len(run_list)

遍历所有文本块

1
2
for run in run_list:
pass

获取文本内容

1
run.text

修改文本内容

1
run.text = ""

设置文字属性

字体

  • 通过字体名定义字体
1
run.font.name = ""

字号

1
2
3
from pptx.util import Pt

run.font.size = Pt(1)

文字颜色

RGBColor(<red>, <green>, <blue>):设置RGB颜色,每个颜色的取值范围为0~255

1
2
3
from pptx.dml.color import RGBColor

run.font.color.rgb = RGBColor(255, 255, 255)

加粗

1
run.font.bold = True

斜体

1
run.font.italic = True

下划线

1
run.font.underline = True

通过文本框对象获取图片对象

判断是否是图片

  • 通过捕获异常来判断文本框内是否是图片,如果不是图片会抛出异常
1
2
3
4
try:
image = shape.image
except:
pass

获取图片二进制

  • 用于保存图片为文件
1
blob = image.blob

保存图片为文件

  • 如果图片文件名相同会被覆盖
1
2
3
blob = image.blob
with open(f"img.{image.ext}", "wb") as file:
file.write(blob)

获取图片扩展名

1
ext = image.ext

获取图片大小

1
size = image.size

通过段落对象获取表格对象

判断是否是表格

1
shape.has_table

获取表格对象

1
table = shape.table

通过表格对象获取所有行对象

1
row_list = table.rows

获取行对象个数

1
len(row_list)

遍历所有行对象

1
2
for row in row_list:
pass

通过行对象获取所有单元格对象

1
cell_list = row.cells

获取所有单元格对象个数

1
len(cell_list)

遍历所有单元格对象

1
2
for cell in cell_list:
pass

通过单元格对象获取文本

1
cell.text

通过文件对象获取模板样式对象

获取所有模板样式对象

1
slide_layout_list = presentation.slide_layouts

获取模版样式对象个数

1
len(slide_layout_list)

遍历模版样式对象

  • 内置模板样式共11个
1
2
for index, slide_layout in enumerate(slide_layout_list):
pass

获取模版样式名称

1
slide_layout.name

通过帧对象获取占位符对象

获取所有占位符对象

1
placeholder_list = slide.placeholders

获取占位符对象个数

1
len(placeholder_list)

遍历所有占位符对象

1
2
for placeholder of placeholder_list:
pass

通过占位符对象获取占位符名

1
placeholder.name

获取占位符索引

1
placeholder.placeholder_format.idx

获取占位符类型

1
placeholder.placeholder_format.type

幻灯片文本内容的写入

  • 所有添加内容操作完成后,都需要保存操作

通过占位符对象写入文本数据

1
placeholder.text = ""

通过文本框对象写入文本数据

  • 向文本框对象内添加一个段落对象并添加文本内容
1
2
paragraph = text_frame.add_paragraph()
paragraph.text = ""

通过样式对象写入表格数据

rows:定义行数
cols:定义列数
left:定义左外边距
top:定义上外边距
width:定义表格宽
height:定义表格高

1
2
3
from pptx.util import Cm

table = shape.add_table(rows=1, cols=1, left=Cm(1), top=Cm(1), width=Cm(1), height=Cm(1))

通过表格对象修改数据

<row>:行号,从0开始
<col>:列号,从0开始

1
table.table.cell(<row>, <col>).text = ""

幻灯片图片内容的写入

通过样式对象写入图片数据

<src>:图片文件路径
left:定义左边距
top:定义上边距

1
picture = shape.add_picture("<src>", left=1, top=1)

width:定义图片宽
height:定义图片高

1
2
3
from pptx.util import Cm

picture = shape.add_picture("<src>", left=1, top=1, width=Cm(1), height=Cm(1))

幻灯片图表内容的写入

通过样式对象写入图片数据

饼图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from pptx.util import Cm

# 准备数据
chart_data = CharData()
chart_data.categories = ["分类1", "分类2"]
chart_data.add_series("主标题", (0.1, 0.9))

# 添加图表
chart = shape.add_chart(XL_CHART_TYPE.PIE, x=Cm(1), y=Cm(1), cx=Cm(1), cy=Cm(1), chart_data=chart_data).chart

# 配置图例
chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
chart.legend.include_in_layout = False

## 配置图标格式
chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
data_labels.number_format = "0%"
data_labels.position = XL_DATA_LABEL_POSITION.OUTSIDE_END

通过文件对象保存为文件

<filename>.pptx:文件名

1
presentation.save("<filename>.pptx")

完成

参考文献

哔哩哔哩——千锋教育