pptx:Python的PPT操作库

1. 安装模块

Windows用户打开命令行输入:pip install python-pptx

Mac用户打开终端/Terminal输入:pip3 install python-pptx

使用windows系统,如果出现无法安装情况,可以在cmd模式下输入网址选择国内清华镜像。

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

导入模块:import pptx

2. 读取PPT文档内容

先了解下PPT基本结构在python分别是什么含义:

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

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

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

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

2.1 幻灯片 slide

1)获取slide

.slides-> 得到一个列表,包含了每个slide

from pptx import Presentation

prs = Presentation('示例文件.pptx')
for slide in prs.slides:
    print(slide)

输出结果:

<pptx.slide.Slide object at 0x0000000003737318>
<pptx.slide.Slide object at 0x0000000003737228>
<pptx.slide.Slide object at 0x0000000003737818>
<pptx.slide.Slide object at 0x0000000003737408>
<pptx.slide.Slide object at 0x00000000037377C8>
<pptx.slide.Slide object at 0x00000000037376D8>
<pptx.slide.Slide object at 0x0000000003737F98>
<pptx.slide.Slide object at 0x00000000037372C8>
<pptx.slide.Slide object at 0x00000000037373B8>

2.2 形状 shape

1) 获取形状 shape

from pptx import Presentation

prs = Presentation('示例文件.pptx')
for slide in prs.slides:
    for shape in slide.shapes:
        print(shape)

输出结果:

<pptx.shapes.autoshape.Shape object at 0x000000000379C390>
<pptx.shapes.picture.Picture object at 0x000000000379C4E0>
<pptx.shapes.picture.Picture object at 0x000000000379C0F0>
<pptx.shapes.placeholder.SlidePlaceholder object at 0x000000000379C080>
<pptx.shapes.placeholder.SlidePlaceholder object at 0x000000000379C400>
<pptx.shapes.placeholder.SlidePlaceholder object at 0x000000000379C390>
<pptx.shapes.graphfrm.GraphicFrame object at 0x000000000379C080>
<pptx.shapes.placeholder.SlidePlaceholder object at 0x000000000379C400>

2) 输出shape中的文字

shape.has_text_frame->是否有文字
shape.text_frame->获取文字框

# 获取所有Slide中的内容
from pptx import Presentation

prs = Presentation('示例文件.pptx')
for slide in prs.slides:
    for shape in slide.shapes:
        if shape.has_text_frame:
            text_frame = shape.text_frame
            print(text_frame.text)
# 获取某一页Slide中的内容
from pptx import Presentation

prs = Presentation('示例文件.pptx')
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 段落 paragraph

1)输出shape中的某个paragraph

从shape中找paragraphs-> 获取shpae中的段落
for paragragh in text_frame.paragraphs:
print(paragragh.text)

from pptx import Presentation

prs = Presentation('示例文件.pptx')
for slide in prs.slides:
    for shape in slide.shapes:
        if shape.has_text_frame:
            text_frame = shape.text_frame
            for paragragh in text_frame.paragraphs:
                print(paragragh.text)

"""
注意:
该方法同样也直接获取Shpae中的文字内容;
但是这个更灵活,先获取每个Shape,然后在获取每个Shape中的paragraph;
我们可以针对paragraph,写一个判断条件,只获取第几个paragraph;
""

3. 向PPT文档写入内容

幻灯片母版及占位符

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

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

3.1 添加slide和内容

1)选择PPT模板

a、使用ppt自带的模板

prs= Presentation()
prs.slide_layouts[index]

ppt自带了常用的1-48种模板通过index选择对应的模板

b、使用自定义ppt模板

prs= Presentation('template.pptx')

2)确认占位符id

prs.slides_layouts[0] # 获取第一套母版的第一个版式

from pptx import Presentation

prs = Presentation("示例文件2.pptx")
slide = prs.slides.add_slide(prs.slide_layouts[0])  # 用第一个母版生成一页ppt
for shape in slide.placeholders:         # 获取这一页所有的占位符
    phf = shape.placeholder_format
    print(f'{phf.idx}--{shape.name}--{phf.type}')  # id号--占位符形状名称-占位符的类型

输出结果:

0–Title 1–TITLE (1)
14–Text Placeholder 2–BODY (2)
15–Text Placeholder 3–BODY (2)
16–Text Placeholder 4–BODY (2)
17–Text Placeholder 5–BODY (2)
18–Text Placeholder 6–BODY (2)

title 标题类型的占位符
subtitle 副标题类型的占位符
body 正文类型的占位符

3)向占位符内填写内容

shape.text = 字符串
prs.save(文件路径.文件名) #同文件夹内可以省略文件路径

绝对文件路径如:C:/Users/Administrator/Desktop/test.pptx

from pptx import Presentation
from datetime import datetime

prs = Presentation("示例文件2.pptx")
slide = prs.slides.add_slide(prs.slide_layouts[0])  # 用第一个母版生成一页ppt
for shape in slide.placeholders:         # 获取这一页所有的占位符
    phf = shape.placeholder_format
    print(f'{phf.idx}--{shape.name}--{phf.type}')
    shape.text = f'{phf.idx}--{phf.type}'  #在占位符中填写“占位符id号--占位符类型”
prs.save('向占位符内填写内容.pptx')

写入前:

写入后:

4)修改占位符的内容

slide.placeholders[占位符ID]

[变量名].text = 字符串

from pptx import Presentation
from datetime import datetime
prs = Presentation("示例文件2.pptx")
slide = prs.slides.add_slide(prs.slide_layouts[0])  # 用第一个母版生成一页ppt
# 确定填写位置
winner_name = slide.placeholders[18]
certificate_type =slide.placeholders[0]
this_is_to_certify_that = slide.placeholders[14]
winning_reason =slide.placeholders[15]
award_presenter = slide.placeholders[16]
award_date = slide.placeholders[17]
# 修改占位符里的填写内容
winner_name.text = "韩梅梅"
certificate_type.text = "三好学生奖状"
this_is_to_certify_that.text = "兹证明"
winning_reason.text = "德学体美劳全面发展"
award_presenter.text = "xx校长"
award_date.text = str(datetime.now().date())

prs.save('修改占位符的内容.pptx')

3.2 添加段落

from pptx import Presentation

prs = Presentation()
bullet_slide_layout = prs.slide_layouts[1] # 使用第2个版式新建幻灯片
slide = prs.slides.add_slide(bullet_slide_layout)
shapes = slide.shapes
title_shape = shapes.title
body_shape =shapes.placeholders[1]

title_shape.text = "添加项目符号列表页"  # 给title占位符添加内容

tf = body_shape.text_frame
tf.text = "带圆点的项目符号行1"   # 给body占位符添加内容

p = tf.add_paragraph()
p.text = "带圆点的项目符号行2"   # 在原来的基础上,添加第一个段落

p = tf.add_paragraph()
p.text = "带圆点的项目符号行3"  # 在原来的基础上,添加第个段落
prs.save('添加段落paragraph.pptx')

给段落设定层级关系

paragraph.level = 层级数【0是最顶层】

p = tf.add_paragraph()
p.text = "带圆点的项目符号行2"
p.level = 1

p = tf.add_paragraph()
p.text = "带圆点的项目符号行3"
p.level = 2

3.3 添加文本框

slide.shapes.add_textbox(left,top.width,height)

  • 厘米写法
from pptx import Presentation
from pptx.util import Cm,Pt

prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)

left = top = width = height =Cm(3)  # # left,top为相对位置,width,height为文本框大小。满足条件顺序是左>上>右>下
text_box = slide.shapes.add_textbox(left,top,width,height)
tf = text_box.text_frame
tf.text = "这是一段文本框里的文字"

p = tf.add_paragraph()
p.text = "这是第二段文字,加粗,字号40"
p.font.bold = True
p.font.size = Pt(40)

prs.save('添加文本框.pptx')
  • 英尺写法
from pptx import Presentation
from pptx.util import Inches
left= top= width= height= Inches(5)

textbox= slide.shapes.add_textbox(left, top, width, height)
textbox.text= '这是一段文本框里的文字'

new_para= textbox.text_frame.add_paragraph()
new_para.text= '这是第二段文字'

prs.save('添加文本框.pptx')

3.4 添加形状

slide_layout = prs.slide_layouts[SLD_LAYOUT_TITLE_AND_CONTENT]

from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.util import Inches

SLD_LAYOUT_TITLE_AND_CONTENT = 6
prs = Presentation()
slide_layout = prs.slide_layouts[SLD_LAYOUT_TITLE_AND_CONTENT]
slide = prs.slides.add_slide(slide_layout)

# 支持的形状可以看:
# https://docs.microsoft.com/zh-cn/office/vba/api/Office.MsoAutoShapeType
shapes = slide.shapes
left, top, width, height = Inches(1), Inches(3), Inches(1.8), Inches(1)
shape = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, left, top, width, height)
shape.text ='第一步'

for n in range(2,6):
    left=left+width-Inches(-0.8)
    shape = slide.shapes.add_shape(MSO_SHAPE.CHEVRON,left,top,width,height)
    shape.text = f'第{n}步'
prs.save('添加形状.pptx')

3.5 添加图片

slide.shapes.add_picture(图片路径,距离左边,距离顶端,宽度,高度)

from pptx import Presentation
from pptx.util import Cm

prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)

left = top = Cm(3)
pic = slide.shapes.add_picture('butterfly.png',left,top)

left = height = Cm(5)
pic = slide.shapes.add_picture('butterfly.png',left,top, height=height)

prs.save('添加图片.pptx')

3.6 添加表格

shapes.add_table(rows,cols,left,top,width,height).table

from pptx import Presentation
from pptx.util import Cm

prs = Presentation()
blank_slide_layout = prs.slide_layouts[6] #使用第7个空白版式创建新的幻灯片
slide = prs.slides.add_slide(blank_slide_layout)

rows,cols = 4,2
left = top =Cm(5)
width = Cm(18)
height = Cm(3)

table = slide.shapes.add_table(rows,cols,left,top,width,height).table
# 可以修改列宽、行高
table.columns[0].width = Cm(6)
table.columns[1].width = Cm(4)
table.rows[0].height =Cm(2)

data = [
    ['姓名','成绩'],
    ['李雷',99],
    ['韩梅梅', 92],
    ['马东梅', 92],
]
for row in range(rows):
    for col in range(cols):
        table.cell(row,col).text =str(data[row][col])

prs.save('添加表格.pptx')

3.7 PPT文档内容样式批量调整

1)文本框位置调整

对齐文本方式:

顶端对齐 .TOP

底端对齐 .BOTTOM

中间对齐 .MIDDLE

from pptx import Presentation
from pptx.util import Cm
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE

prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)

left = top = width = height =Cm(3)
text_box = slide.shapes.add_textbox(left,top,width,height)
tf = text_box.text_frame
tf.text = "这是一段文本框里的文字"

tf.margin_bottom = Cm(0.1) #下边距
tf.margin_left = 0 #左边距
tf.vertical_anchor = MSO_ANCHOR.BOTTOM # 对齐文本方式:底端对齐
tf.word_wrap = True # 框中的文字自动换行
prs.save('文本框样式的调整.pptx')

2)文本框背景颜色调整

from pptx.dml.color import RGBColor

fill = text_box.fill
text_box = slide.shapes.add_textbox(left, top, width, height)
fill.solid()  # 纯色填充
fill.fore_color.rgb = RGBColor(247, 150, 70)

from pptx import Presentation
from pptx.util import Cm, Pt
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE
from pptx.dml.color import RGBColor

prs = Presentation()
# 使用第七个版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)

left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
tf.text = "这是一段文本框里面的文字"
# -------------------------------------- #
tf.margin_bottom = Cm(0.1) # 下边距
tf.margin_left = 0 # 下边距
tf.vertical_anchor = MSO_ANCHOR.BOTTOM
tf.word_wrap = True # 框中的文字自动换行
# -------------------------------------- #
fill = text_box.fill
fill.solid()
# 使用之前一定要导入RGBColor这个库
fill.fore_color.rgb = RGBColor(100, 149, 237)

prs.save("文本框背景色的调整.pptx")

3)文本框边框样式调整

line = text_box.line

from pptx import Presentation
from pptx.util import Cm, Pt
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE
from pptx.dml.color import RGBColor

prs = Presentation()
# 使用第七个版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)

left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
tf.text = "这是一段文本框里面的文字"
# -------------------------------------- #
tf.margin_bottom = Cm(0.1) # 下边距
tf.margin_left = 0 # 下边距
tf.vertical_anchor = MSO_ANCHOR.BOTTOM
tf.word_wrap = True # 框中的文字自动换行
# -------------------------------------- #
fill = text_box.fill
fill.solid()
# 使用之前一定要导入RGBColor这个库
fill.fore_color.rgb = RGBColor(247, 150, 70)
# -------------------------------------- #
line = text_box.line
line.color.rgb = RGBColor(255, 0, 0)
line.width = Cm(0.3)

prs.save("文本框边框样式调整.pptx")

4)段落及字体样式调整

段落对齐调整

from pptx.enum.text import PP_ALIGN

p.alignment = 对齐方式

PP_ALIGN.LEFTPP_ALIGN.CENTERPP_ALIGN.RIGHT

from pptx import Presentation
from pptx.enum.text import PP_ALIGN
from pptx.util import Cm

prs = Presentation()
# 使用第七个版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)

left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
# ---------------------------- #
p = tf.add_paragraph()
p.text = "这是第二段文字"
p.alignment = PP_ALIGN.LEFT  # 左对齐

prs.save("段落对其调整.pptx")
段落其他样式调整

与word中的使用非常相似:

.add_run() 添加新的文字块
.level 段落缩进层级
.line_spacing 段落行间距
.runs 段落内的文字块
.space_after 段后距
.space_before 段前距

字体样式调整

.font.name 字体名称(可以直接设定为中文字体)

.font.bold 是否加粗

.font.italic 是否斜体

.font.color 字体颜色

.font.size 字体大小

from pptx import Presentation
from pptx.util import Cm, Pt
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN

prs = Presentation()
# 使用第七个版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)

left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
# ---------------------------- #
p = tf.add_paragraph()
p.text = "这是第二段文字"
p.alignment = PP_ALIGN.LEFT
# ------------------------------------- #
p.font.bold = True
p.font.name = "宋体"
p.font.color.rgb = RGBColor(247, 150, 70)
p.font.size = Pt(30)

prs.save("字体样式调整.pptx")
posted @ 2023-09-28 08:39  luckzack  阅读(383)  评论(0编辑  收藏  举报