跳转到内容

PPT 演示文稿 (pptx)

pptx 是处理 PowerPoint 演示文稿的技能。它的核心是:创建、编辑和格式化 PPT 文件。

当用户提到以下内容时使用:

  • PPT
  • 演示文稿
  • Slides
  • Deck
  • .pptx 文件
from pptx import Presentation
prs = Presentation()
prs.save('presentation.pptx')
from pptx import Presentation
prs = Presentation()
# 添加空白幻灯片
slide = prs.slides.add_slide(prs.slide_layouts[1])
# 添加标题
title = slide.shapes.title
title.text = '演示标题'
# 添加内容
subtitle = slide.placeholders[1]
subtitle.text = '副标题内容'
prs.save('slides.pptx')
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[1])
# 添加文本框
left = Inches(1)
top = Inches(2)
width = Inches(8)
height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
tf.text = '这是文本框内容'
# 添加段落
p = tf.add_paragraph()
p.text = '第二段文字'
p.font.size = Pt(24)
p.font.bold = True
prs.save('textbox.pptx')
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5]) # 空白布局
# 添加表格
rows = 3
cols = 3
left = Inches(1)
top = Inches(2)
width = Inches(8)
height = Inches(2)
table = slide.shapes.add_table(rows, cols, left, top, width, height).table
# 设置表格内容
table.cell(0, 0).text = '姓名'
table.cell(0, 1).text = '年龄'
table.cell(0, 2).text = '城市'
table.cell(1, 0).text = '张三'
table.cell(1, 1).text = '25'
table.cell(1, 2).text = '北京'
prs.save('table.pptx')
from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
# 创建图表数据
chart_data = CategoryChartData()
chart_data.categories = ['Q1', 'Q2', 'Q3', 'Q4']
chart_data.add_series('销售额', (100, 150, 120, 180))
# 添加图表
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
chart = slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
).chart
prs.save('chart.pptx')
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[1])
# 添加图片
slide.shapes.add_picture(
'image.jpg',
Inches(1), Inches(1),
width=Inches(4)
)
prs.save('with_image.pptx')
需求:创建商业计划书 PPT
步骤:
1. 添加封面
2. 添加目录
3. 添加问题/解决方案
4. 添加市场分析
5. 添加财务预测
6. 添加团队介绍
需求:创建季度报告
步骤:
1. 添加数据图表
2. 添加表格
3. 添加分析文字
4. 统一格式

创建

Presentation() + slides.add_slide()

文本

add_textbox() + text_frame

表格

add_table() + cell

图表

CategoryChartData + add_chart()


查看源文件: GitHub原始文件