mcpskills.net
技能MCP智能体提示词
mcpskills.net — A curated directory of AI agent Skills and MCP servers
TermsPrivacy
← 返回技能
Productivity

PowerPoint 处理 (PPTX)

以编程方式创建和操作 Microsoft PowerPoint 演示文稿。当用户需要生成、编辑或转换 PowerPoint 文件时使用。

作者:Anthropic仓库 →来源 →

以编程方式创建和操作 Microsoft PowerPoint 演示文稿。

环境准备

安装

pip install python-pptx

基本用法

from pptx import Presentation

# Create presentation
prs = Presentation()

# Add slide
slide_layout = prs.slide_layouts[0]  # Title slide
slide = prs.slides.add_slide(slide_layout)

# Add content
title = slide.shapes.title
title.text = "Hello, World!"

# Save
prs.save('presentation.pptx')

演示文稿结构

幻灯片版式

# Available layouts
layouts = {
    0: 'Title Slide',
    1: 'Title and Content',
    2: 'Section Header',
    3: 'Two Content',
    4: 'Comparison',
    5: 'Title Only',
    6: 'Blank',
    7: 'Content with Caption',
    8: 'Picture with Caption',
}

添加幻灯片

from pptx import Presentation

prs = Presentation()

# Title slide
slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Presentation Title"
subtitle.text = "Subtitle"

# Content slide
slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(slide_layout)
title = slide.shapes.title
title.text = "Section Title"
body = slide.placeholders[1]
body.text = "Content goes here"

内容类型

文本框

from pptx.util import Inches, Pt

# Add text box
txBox = slide.shapes.add_textbox(Inches(1), Inches(1), Inches(5), Inches(2))
tf = txBox.text_frame
tf.text = "Hello, World!"

# Format text
p = tf.paragraphs[0]
p.text = "Formatted text"
p.font.size = Pt(24)
p.font.bold = True
p.font.color.rgb = RGBColor(0, 0, 255)

图片

from pptx.util import Inches

# Add image
slide.shapes.add_picture('image.png', Inches(1), Inches(1), Inches(4), Inches(3))

形状

from pptx.enum.shapes import MSO_SHAPE

# Add shape
shape = slide.shapes.add_shape(
    MSO_SHAPE.RECTANGLE,
    Inches(1), Inches(1), Inches(2), Inches(1)
)
shape.fill.solid()
shape.fill.fore_color.rgb = RGBColor(0, 128, 255)
shape.line.color.rgb = RGBColor(0, 0, 0)

表格

from pptx.util import Inches

# Add table
rows, cols = 4, 3
table = slide.shapes.add_table(rows, cols, Inches(1), Inches(1), Inches(6), Inches(3)).table

# Set column widths
table.columns[0].width = Inches(2)
table.columns[1].width = Inches(2)
table.columns[2].width = Inches(2)

# Add data
for i, row in enumerate(table.rows):
    for j, cell in enumerate(row.cells):
        cell.text = f'Row {i}, Col {j}'

高级功能

幻灯片母版

from pptx import Presentation

prs = Presentation()

# Access master
master = prs.slide_masters[0]

# Modify master
for shape in master.shapes:
    if shape.has_text_frame:
        for paragraph in shape.text_frame.paragraphs:
            paragraph.font.name = 'Arial'

图表

from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE

# Create chart data
chart_data = CategoryChartData()
chart_data.categories = ['January', 'February', 'March']
chart_data.add_series('Series 1', (10, 20, 30))

# Add chart
chart = slide.shapes.add_chart(
    XL_CHART_TYPE.COLUMN_CLUSTERED,
    Inches(1), Inches(1), Inches(6), Inches(4),
    chart_data
).chart

动画

from pptx.enum.shapes import MSO_SHAPE

# Add shape
shape = slide.shapes.add_shape(
    MSO_SHAPE.RECTANGLE,
    Inches(1), Inches(1), Inches(2), Inches(1)
)

# Add animation
from pptx.oxml.ns import qn
from lxml import etree

animation = shape._element.spPr.makeelement(qn('p:anim'), {})
# Configure animation...

模板系统

使用模板

from pptx import Presentation

# Load template
prs = Presentation('template.pptx')

# Modify slides
for slide in prs.slides:
    for shape in slide.shapes:
        if shape.has_text_frame:
            for paragraph in shape.text_frame.paragraphs:
                if '{{TITLE}}' in paragraph.text:
                    paragraph.text = paragraph.text.replace('{{TITLE}}', 'New Title')

# Save
prs.save('output.pptx')

最佳实践

  • 使用幻灯片母版保持样式一致
  • 保持文本简洁
  • 使用高质量图片
  • 在不同屏幕尺寸上测试
  • 考虑无障碍访问
  • 使用一致的字体和颜色
  • 提供演讲者备注