python-pptx 是一个用于创建和更新PowerPoint(.pptx)文件的Python库。
典型的用途是从数据库内容生成自定义的PowerPoint演示文稿,可通过单击Web应用程序中的链接进行下载。
一些开发人员使用它根据工作管理系统中保存的信息自动生成可立即呈现的工程状态报告。
它还可以用于对演示文稿库进行批量更新,或者仅仅是为了自动生成一个或两个幻灯片,这对于手动操作来说是繁琐的。
安装
pip install python-pptx
PPT 文件内容结构
Presentations
Slides
Shapes
Placeholders
able-related objects
ChartData objects
Charts
Text-related objects
Click Action-related objects
DrawingML objects
Image
读取 PPT 文件
打开 ppt 文件,方式1
from pptx import Presentation
# prs = Presentation()
prs = Presentation('existing-prs-file.pptx')
prs.save('test.pptx')
打开 ppt 文件,方式2
from pptx import Presentation
f = open('foobar.pptx')
prs = Presentation(f)
f.close()
打开 ppt 文件,方式3
with open('foobar.pptx') as f:
source_stream = StringIO(f.read())
prs = Presentation(source_stream)
source_stream.close()
target_stream = StringIO()
prs.save(target_stream)
演示文稿中的每张幻灯片都基于幻灯片布局。因此,您必须指定在创建新幻灯片时使用哪种幻灯片布局,这并不奇怪。让我们花点时间了解一些我们需要的幻灯片布局,以便我们添加的幻灯片看起来像我们想要的那样。
写入 PPT 文件
最简单的示例
from pptx import Presentation
prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "标题"
subtitle.text = "正文"
prs.save('test.pptx')
示例2
from pptx import Presentation
prs = Presentation()
bullet_slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(bullet_slide_layout)
shapes = slide.shapes
title_shape = shapes.title
body_shape = shapes.placeholders[1]
title_shape.text = 'Adding a Bullet Slide'
tf = body_shape.text_frame
tf.text = 'Find the bullet slide layout'
p = tf.add_paragraph()
p.text = 'Use _TextFrame.text for first bullet'
p.level = 1
p = tf.add_paragraph()
p.text = 'Use _TextFrame.add_paragraph() for subsequent bullets'
p.level = 2
prs.save('test.pptx')
add_textbox
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = width = height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
tf.text = "This is text inside a textbox"
p = tf.add_paragraph()
p.text = "This is a second paragraph that's bold"
p.font.bold = True
p = tf.add_paragraph()
p.text = "This is a third paragraph that's big"
p.font.size = Pt(40)
prs.save('test.pptx')
add_picture
from pptx import Presentation
from pptx.util import Inches
img_path = 'monty-truth.png'
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = Inches(1)
pic = slide.shapes.add_picture(img_path, left, top)
left = Inches(5)
height = Inches(5.5)
pic = slide.shapes.add_picture(img_path, left, top, height=height)
prs.save('test.pptx')
add_shape
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.util import Inches
prs = Presentation()
title_only_slide_layout = prs.slide_layouts[5]
slide = prs.slides.add_slide(title_only_slide_layout)
shapes = slide.shapes
shapes.title.text = 'Adding an AutoShape'
left = Inches(0.93) # 0.93" centers this overall set of shapes
top = Inches(3.0)
width = Inches(1.75)
height = Inches(1.0)
shape = shapes.add_shape(MSO_SHAPE.PENTAGON, left, top, width, height)
shape.text = 'Step 1'
left = left + width - Inches(0.4)
width = Inches(2.0) # chevrons need more width for visual balance
for n in range(2, 6):
shape = shapes.add_shape(MSO_SHAPE.CHEVRON, left, top, width, height) shape.text = 'Step %d' % n
left = left + width - Inches(0.4)
prs.save('test.pptx')
## add_table
```python
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
title_only_slide_layout = prs.slide_layouts[5]
slide = prs.slides.add_slide(title_only_slide_layout)
shapes = slide.shapes
shapes.title.text = 'Adding a Table'
rows = cols = 2
left = top = Inches(2.0)
width = Inches(6.0)
height = Inches(0.8)
table = shapes.add_table(rows, cols, left, top, width, height).table
# set column widths
table.columns[0].width = Inches(2.0)
table.columns[1].width = Inches(4.0)
# write column headings
table.cell(0, 0).text = 'Foo'
table.cell(0, 1).text = 'Bar'
# write body cells
table.cell(1, 0).text = 'Baz'
table.cell(1, 1).text = 'Qux'
prs.save('test.pptx')
Extract all text from slides in presentation
from pptx import Presentation
prs = Presentation(path_to_presentation)
# text_runs will be populated with a list of strings, # one for each text run in presentation
text_runs = []
for slide in prs.slides:
for shape in slide.shapes:
if not shape.has_text_frame: continue
for paragraph in shape.text_frame.paragraphs: for run in paragraph.runs:
text_runs.append(run.text)
添加一个 slide
SLD_LAYOUT_TITLE_AND_CONTENT = 1
prs = Presentation()
slide_layout = prs.slide_layouts[SLD_LAYOUT_TITLE_AND_CONTENT]
slide = prs.slides.add_slide(slide_layout)
访问幻灯片中的图形
shapes = slide.shapes
自动形状
from pptx.enum.shapes import MSO_SHAPE
shapes = slide.shapes
left = top = width = height = Inches(1.0)
shape = shapes.add_shape(
MSO_SHAPE.ROUNDED_RECTANGLE, left, top, width, height
)
长度单位
from pptx.util import Inches, Pt
length = Inches(1)
length.inches
length.cm
length.pt
length = Pt(72)
形状位置和尺寸
填充
线
调整自动形状
占位符
一个占位符也是一个形状
占位符类型
访问占位符
识别和表征占位符
插入占位符
设置幻灯片标题
文本
文本框
段落
添加文本
应用文本框架级格式
应用段落样式
应用字符样式
图表
添加图表
轴
数据标签
传说
线图
饼图
表格
内容
表格操作
notes slide
Concepts