pptx-manipulation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePPTX Manipulation Skill
PPTX 操作技能
Overview
概述
This skill enables programmatic creation, editing, and manipulation of Microsoft PowerPoint (.pptx) presentations using the python-pptx library. Create professional slides with text, shapes, images, charts, and tables without manual editing.
本技能支持使用python-pptx库以编程方式创建、编辑和处理Microsoft PowerPoint(.pptx)演示文稿。无需手动编辑即可创建包含文本、形状、图片、图表和表格的专业幻灯片。
How to Use
使用方法
- Describe the presentation you want to create or modify
- Provide content, data, or images to include
- I'll generate python-pptx code and execute it
Example prompts:
- "Create a 10-slide pitch deck from this outline"
- "Add a chart to slide 3 with this data"
- "Extract all text from this presentation"
- "Generate slides from this markdown content"
- 描述你想要创建或修改的演示文稿
- 提供要包含的内容、数据或图片
- 我将生成python-pptx代码并执行
示例提示:
- "根据此大纲创建一个10页的推介演示文稿"
- "在第3页幻灯片中添加包含此数据的图表"
- "提取此演示文稿中的所有文本"
- "根据此Markdown内容生成幻灯片"
Domain Knowledge
领域知识
python-pptx Fundamentals
python-pptx 基础
python
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.enum.shapes import MSO_SHAPE
from pptx.enum.text import PP_ALIGNpython
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.enum.shapes import MSO_SHAPE
from pptx.enum.text import PP_ALIGNCreate new presentation
创建新演示文稿
prs = Presentation()
prs = Presentation()
Or open existing
或打开现有演示文稿
prs = Presentation('existing.pptx')
undefinedprs = Presentation('existing.pptx')
undefinedPresentation Structure
演示文稿结构
Presentation
├── slide_layouts (predefined layouts)
├── slides (individual slides)
│ ├── shapes (text, images, charts)
│ │ ├── text_frame (paragraphs)
│ │ └── table (rows, cells)
│ └── placeholders (title, content)
└── slide_masters (templates)Presentation
├── slide_layouts (预定义版式)
├── slides (单个幻灯片)
│ ├── shapes (文本、图片、图表)
│ │ ├── text_frame (段落)
│ │ └── table (行、单元格)
│ └── placeholders (标题、内容占位符)
└── slide_masters (模板)Slide Layouts
幻灯片版式
python
undefinedpython
undefinedCommon layout indices (may vary by template)
常见版式索引(可能因模板而异)
TITLE_SLIDE = 0
TITLE_CONTENT = 1
SECTION_HEADER = 2
TWO_CONTENT = 3
COMPARISON = 4
TITLE_ONLY = 5
BLANK = 6
TITLE_SLIDE = 0
TITLE_CONTENT = 1
SECTION_HEADER = 2
TWO_CONTENT = 3
COMPARISON = 4
TITLE_ONLY = 5
BLANK = 6
Add slide with layout
添加带指定版式的幻灯片
slide_layout = prs.slide_layouts[TITLE_CONTENT]
slide = prs.slides.add_slide(slide_layout)
undefinedslide_layout = prs.slide_layouts[TITLE_CONTENT]
slide = prs.slides.add_slide(slide_layout)
undefinedAdding Content
添加内容
Title Slide
标题幻灯片
python
slide_layout = prs.slide_layouts[0] # Title slide
slide = prs.slides.add_slide(slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Quarterly Report"
subtitle.text = "Q4 2024 Performance Review"python
slide_layout = prs.slide_layouts[0] # 标题幻灯片版式
slide = prs.slides.add_slide(slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "季度报告"
subtitle.text = "2024年第四季度业绩回顾"Text Content
文本内容
python
undefinedpython
undefinedUsing placeholder
使用占位符
body = slide.placeholders[1]
tf = body.text_frame
tf.text = "First bullet point"
body = slide.placeholders[1]
tf = body.text_frame
tf.text = "第一个项目符号"
Add more paragraphs
添加更多段落
p = tf.add_paragraph()
p.text = "Second bullet point"
p.level = 0
p = tf.add_paragraph()
p.text = "Sub-bullet"
p.level = 1
undefinedp = tf.add_paragraph()
p.text = "第二个项目符号"
p.level = 0
p = tf.add_paragraph()
p.text = "子项目符号"
p.level = 1
undefinedText Box
文本框
python
from pptx.util import Inches, Pt
left = Inches(1)
top = Inches(2)
width = Inches(4)
height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
p = tf.paragraphs[0]
p.text = "Custom text box"
p.font.bold = True
p.font.size = Pt(18)python
from pptx.util import Inches, Pt
left = Inches(1)
top = Inches(2)
width = Inches(4)
height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
p = tf.paragraphs[0]
p.text = "自定义文本框"
p.font.bold = True
p.font.size = Pt(18)Shapes
形状
python
from pptx.enum.shapes import MSO_SHAPEpython
from pptx.enum.shapes import MSO_SHAPERectangle
矩形
shape = slide.shapes.add_shape(
MSO_SHAPE.RECTANGLE,
Inches(1), Inches(2), # left, top
Inches(3), Inches(1.5) # width, height
)
shape.text = "Rectangle with text"
shape = slide.shapes.add_shape(
MSO_SHAPE.RECTANGLE,
Inches(1), Inches(2), # 左、上位置
Inches(3), Inches(1.5) # 宽、高
)
shape.text = "带文本的矩形"
Common shapes:
常见形状:
MSO_SHAPE.RECTANGLE, ROUNDED_RECTANGLE
MSO_SHAPE.RECTANGLE, ROUNDED_RECTANGLE
MSO_SHAPE.OVAL, CHEVRON, ARROW_RIGHT
MSO_SHAPE.OVAL, CHEVRON, ARROW_RIGHT
MSO_SHAPE.CALLOUT_ROUNDED_RECTANGLE
MSO_SHAPE.CALLOUT_ROUNDED_RECTANGLE
undefinedundefinedImages
图片
python
undefinedpython
undefinedAdd image
添加图片
slide.shapes.add_picture(
'image.png',
Inches(1), Inches(2), # position
width=Inches(4) # auto height
)
slide.shapes.add_picture(
'image.png',
Inches(1), Inches(2), # 位置
width=Inches(4) # 自动高度
)
Or specify both dimensions
或指定宽高
slide.shapes.add_picture(
'logo.png',
Inches(8), Inches(0.5),
Inches(1.5), Inches(0.75)
)
undefinedslide.shapes.add_picture(
'logo.png',
Inches(8), Inches(0.5),
Inches(1.5), Inches(0.75)
)
undefinedTables
表格
python
undefinedpython
undefinedCreate table
创建表格
rows, cols = 4, 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
rows, cols = 4, 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
Set column widths
设置列宽
table.columns[0].width = Inches(2)
table.columns[1].width = Inches(3)
table.columns[2].width = Inches(3)
table.columns[0].width = Inches(2)
table.columns[1].width = Inches(3)
table.columns[2].width = Inches(3)
Add headers
添加表头
headers = ['Product', 'Q3 Sales', 'Q4 Sales']
for i, header in enumerate(headers):
cell = table.cell(0, i)
cell.text = header
cell.text_frame.paragraphs[0].font.bold = True
headers = ['产品', 'Q3销售额', 'Q4销售额']
for i, header in enumerate(headers):
cell = table.cell(0, i)
cell.text = header
cell.text_frame.paragraphs[0].font.bold = True
Add data
添加数据
data = [
['Widget A', '$10,000', '$12,500'],
['Widget B', '$8,000', '$9,200'],
['Widget C', '$15,000', '$18,000'],
]
for row_idx, row_data in enumerate(data, 1):
for col_idx, value in enumerate(row_data):
table.cell(row_idx, col_idx).text = value
undefineddata = [
['Widget A', '$10,000', '$12,500'],
['Widget B', '$8,000', '$9,200'],
['Widget C', '$15,000', '$18,000'],
]
for row_idx, row_data in enumerate(data, 1):
for col_idx, value in enumerate(row_data):
table.cell(row_idx, col_idx).text = value
undefinedCharts
图表
python
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPEpython
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPEChart data
图表数据
chart_data = CategoryChartData()
chart_data.categories = ['Q1', 'Q2', 'Q3', 'Q4']
chart_data.add_series('Sales', (19.2, 21.4, 16.7, 23.8))
chart_data.add_series('Expenses', (12.1, 15.3, 14.2, 18.1))
chart_data = CategoryChartData()
chart_data.categories = ['Q1', 'Q2', 'Q3', 'Q4']
chart_data.add_series('销售额', (19.2, 21.4, 16.7, 23.8))
chart_data.add_series('支出', (12.1, 15.3, 14.2, 18.1))
Add chart
添加图表
x, y, cx, cy = Inches(1), Inches(2), Inches(8), Inches(4)
chart = slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED,
x, y, cx, cy, chart_data
).chart
x, y, cx, cy = Inches(1), Inches(2), Inches(8), Inches(4)
chart = slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED,
x, y, cx, cy, chart_data
).chart
Customize
自定义设置
chart.has_legend = True
chart.legend.include_in_layout = False
undefinedchart.has_legend = True
chart.legend.include_in_layout = False
undefinedFormatting
格式设置
Text Formatting
文本格式
python
from pptx.dml.color import RGBColor
run = p.runs[0]
run.font.name = 'Arial'
run.font.size = Pt(24)
run.font.bold = True
run.font.italic = True
run.font.color.rgb = RGBColor(0x00, 0x66, 0xCC)python
from pptx.dml.color import RGBColor
run = p.runs[0]
run.font.name = 'Arial'
run.font.size = Pt(24)
run.font.bold = True
run.font.italic = True
run.font.color.rgb = RGBColor(0x00, 0x66, 0xCC)Shape Fill & Line
形状填充与线条
python
from pptx.dml.color import RGBColor
shape.fill.solid()
shape.fill.fore_color.rgb = RGBColor(0x00, 0x80, 0x00)
shape.line.color.rgb = RGBColor(0x00, 0x00, 0x00)
shape.line.width = Pt(2)python
from pptx.dml.color import RGBColor
shape.fill.solid()
shape.fill.fore_color.rgb = RGBColor(0x00, 0x80, 0x00)
shape.line.color.rgb = RGBColor(0x00, 0x00, 0x00)
shape.line.width = Pt(2)Paragraph Alignment
段落对齐
python
from pptx.enum.text import PP_ALIGN
p.alignment = PP_ALIGN.CENTER # LEFT, RIGHT, JUSTIFYpython
from pptx.enum.text import PP_ALIGN
p.alignment = PP_ALIGN.CENTER # LEFT, RIGHT, JUSTIFYBest Practices
最佳实践
- Use Templates: Start with a .pptx template for consistent branding
- Layout First: Plan slide structure before coding
- Reuse Slide Masters: Maintain consistency across presentations
- Optimize Images: Compress images before adding
- Test Output: Always verify generated presentations
- 使用模板:从.pptx模板开始,确保品牌一致性
- 先规划版式:编码前先规划幻灯片结构
- 复用幻灯片母版:在所有演示文稿中保持一致性
- 优化图片:添加前先压缩图片
- 测试输出:始终验证生成的演示文稿
Common Patterns
常见模式
Slide Deck Generator
演示文稿生成器
python
def create_deck(title, slides_content):
prs = Presentation()
# Title slide
slide = prs.slides.add_slide(prs.slide_layouts[0])
slide.shapes.title.text = title
# Content slides
for slide_data in slides_content:
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = slide_data['title']
body = slide.placeholders[1]
tf = body.text_frame
for i, point in enumerate(slide_data['points']):
if i == 0:
tf.text = point
else:
p = tf.add_paragraph()
p.text = point
return prspython
def create_deck(title, slides_content):
prs = Presentation()
# 标题幻灯片
slide = prs.slides.add_slide(prs.slide_layouts[0])
slide.shapes.title.text = title
# 内容幻灯片
for slide_data in slides_content:
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = slide_data['title']
body = slide.placeholders[1]
tf = body.text_frame
for i, point in enumerate(slide_data['points']):
if i == 0:
tf.text = point
else:
p = tf.add_paragraph()
p.text = point
return prsData-Driven Charts
数据驱动图表
python
def add_bar_chart(slide, title, categories, values):
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
chart_data = CategoryChartData()
chart_data.categories = categories
chart_data.add_series('Values', values)
chart = slide.shapes.add_chart(
XL_CHART_TYPE.BAR_CLUSTERED,
Inches(1), Inches(2),
Inches(8), Inches(4),
chart_data
).chart
chart.chart_title.text_frame.text = title
return chartpython
def add_bar_chart(slide, title, categories, values):
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
chart_data = CategoryChartData()
chart_data.categories = categories
chart_data.add_series('数值', values)
chart = slide.shapes.add_chart(
XL_CHART_TYPE.BAR_CLUSTERED,
Inches(1), Inches(2),
Inches(8), Inches(4),
chart_data
).chart
chart.chart_title.text_frame.text = title
return chartExamples
示例
Example 1: Create a Pitch Deck
示例1:创建推介演示文稿
python
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()python
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()Slide 1: Title
幻灯片1:标题
slide = prs.slides.add_slide(prs.slide_layouts[0])
slide.shapes.title.text = "StartupX"
slide.placeholders[1].text = "Revolutionizing Document Processing"
slide = prs.slides.add_slide(prs.slide_layouts[0])
slide.shapes.title.text = "StartupX"
slide.placeholders[1].text = "革新文档处理"
Slide 2: Problem
幻灯片2:问题
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "The Problem"
body = slide.placeholders[1].text_frame
body.text = "Manual document processing costs businesses $1T annually"
p = body.add_paragraph()
p.text = "Average worker spends 20% of time on document tasks"
p.level = 1
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "存在的问题"
body = slide.placeholders[1].text_frame
body.text = "手动文档处理每年给企业造成1万亿美元的损失"
p = body.add_paragraph()
p.text = "员工平均将20%的时间花费在文档任务上"
p.level = 1
Slide 3: Solution
幻灯片3:解决方案
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "Our Solution"
body = slide.placeholders[1].text_frame
body.text = "AI-powered document automation"
body.add_paragraph().text = "90% faster processing"
body.add_paragraph().text = "99.5% accuracy"
body.add_paragraph().text = "Works with existing tools"
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "我们的解决方案"
body = slide.placeholders[1].text_frame
body.text = "AI驱动的文档自动化"
body.add_paragraph().text = "处理速度提升90%"
body.add_paragraph().text = "准确率达99.5%"
body.add_paragraph().text = "可与现有工具兼容"
Slide 4: Market
幻灯片4:市场
slide = prs.slides.add_slide(prs.slide_layouts[5]) # Title only
slide.shapes.title.text = "Market Opportunity: $50B by 2028"
slide = prs.slides.add_slide(prs.slide_layouts[5]) # 仅标题版式
slide.shapes.title.text = "市场机遇:到2028年达500亿美元"
Add chart
添加图表
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
data = CategoryChartData()
data.categories = ['2024', '2025', '2026', '2027', '2028']
data.add_series('Market Size ($B)', [30, 35, 40, 45, 50])
slide.shapes.add_chart(
XL_CHART_TYPE.LINE,
Inches(1), Inches(1.5),
Inches(8), Inches(5),
data
)
prs.save('pitch_deck.pptx')
undefinedfrom pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
data = CategoryChartData()
data.categories = ['2024', '2025', '2026', '2027', '2028']
data.add_series('市场规模(十亿美元)', [30, 35, 40, 45, 50])
slide.shapes.add_chart(
XL_CHART_TYPE.LINE,
Inches(1), Inches(1.5),
Inches(8), Inches(5),
data
)
prs.save('pitch_deck.pptx')
undefinedExample 2: Report with Data Table
示例2:带数据表格的报告
python
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()python
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()Title slide
标题幻灯片
slide = prs.slides.add_slide(prs.slide_layouts[0])
slide.shapes.title.text = "Sales Performance Report"
slide.placeholders[1].text = "Q4 2024"
slide = prs.slides.add_slide(prs.slide_layouts[0])
slide.shapes.title.text = "销售业绩报告"
slide.placeholders[1].text = "2024年第四季度"
Data slide
数据幻灯片
slide = prs.slides.add_slide(prs.slide_layouts[5])
slide.shapes.title.text = "Regional Performance"
slide = prs.slides.add_slide(prs.slide_layouts[5])
slide.shapes.title.text = "区域业绩"
Create table
创建表格
table = slide.shapes.add_table(5, 4, Inches(0.5), Inches(1.5), Inches(9), Inches(4)).table
table = slide.shapes.add_table(5, 4, Inches(0.5), Inches(1.5), Inches(9), Inches(4)).table
Headers
表头
headers = ['Region', 'Revenue', 'Growth', 'Target']
for i, h in enumerate(headers):
table.cell(0, i).text = h
table.cell(0, i).text_frame.paragraphs[0].font.bold = True
headers = ['区域', '收入', '增长率', '目标完成情况']
for i, h in enumerate(headers):
table.cell(0, i).text = h
table.cell(0, i).text_frame.paragraphs[0].font.bold = True
Data
数据
data = [
['North America', '$5.2M', '+15%', 'Met'],
['Europe', '$3.8M', '+12%', 'Met'],
['Asia Pacific', '$2.9M', '+28%', 'Exceeded'],
['Latin America', '$1.1M', '+8%', 'Below'],
]
for row_idx, row_data in enumerate(data, 1):
for col_idx, value in enumerate(row_data):
table.cell(row_idx, col_idx).text = value
prs.save('sales_report.pptx')
undefineddata = [
['北美', '$5.2M', '+15%', '已完成'],
['欧洲', '$3.8M', '+12%', '已完成'],
['亚太', '$2.9M', '+28%', '超额完成'],
['拉美', '$1.1M', '+8%', '未完成'],
]
for row_idx, row_data in enumerate(data, 1):
for col_idx, value in enumerate(row_data):
table.cell(row_idx, col_idx).text = value
prs.save('sales_report.pptx')
undefinedLimitations
局限性
- Cannot render complex animations
- Limited SmartArt support
- No video embedding via API
- Master slide editing is complex
- Chart types limited to standard Office charts
- 无法渲染复杂动画
- SmartArt支持有限
- 无法通过API嵌入视频
- 母版幻灯片编辑复杂
- 图表类型仅限于标准Office图表
Installation
安装
bash
pip install python-pptxbash
pip install python-pptx