Pptx Manipulation

作者 claude-office-skills9c4c7d5cd281MIT499 个星标收录于 2026年10月8日更新于 2026年10月8日仓库8个月前更新

>

仅含说明Documents & Office
AI 生成的概览

使用 python-pptx 代码创建、编辑 PowerPoint .pptx 演示文稿并提取其中内容。

功能
该技能指导智能体编写并运行 python-pptx 代码来构建和修改 PowerPoint 演示文稿。内容涵盖按版式添加幻灯片、文本框、形状、图片、表格和标准图表,以及设置文字、填充、线条和对齐格式。它还支持从现有演示文稿中提取文本,并根据大纲或 markdown 内容生成幻灯片。
适用场景
适用于需要生成或修改 .pptx 文件的任务,例如制作路演演示文稿、向幻灯片添加图表或表格,或从现有演示文稿中提取文本。也适合根据结构化内容生成幻灯片以及基于模板的幻灯片制作。
运行要求
需要 python-pptx 库(pip install python-pptx)和 Python 运行环境。该技能不附带脚本,仅提供说明和代码示例。元数据中引用了 office-mcp 服务器及 create_pptx、extract_from_pptx 等工具。

PPTX Manipulation Skill

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.

How to Use

  1. Describe the presentation you want to create or modify
  2. Provide content, data, or images to include
  3. 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"

Domain Knowledge

python-pptx Fundamentals

python
from pptx import Presentationfrom pptx.util import Inches, Ptfrom pptx.enum.shapes import MSO_SHAPEfrom pptx.enum.text import PP_ALIGN
# Create new presentationprs = Presentation()
# Or open existingprs = Presentation('existing.pptx')

Presentation 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)

Slide Layouts

python
# Common layout indices (may vary by template)TITLE_SLIDE = 0TITLE_CONTENT = 1SECTION_HEADER = 2TWO_CONTENT = 3COMPARISON = 4TITLE_ONLY = 5BLANK = 6
# Add slide with layoutslide_layout = prs.slide_layouts[TITLE_CONTENT]slide = prs.slides.add_slide(slide_layout)

Adding Content

Title Slide
python
slide_layout = prs.slide_layouts[0]  # Title slideslide = prs.slides.add_slide(slide_layout)
title = slide.shapes.titlesubtitle = slide.placeholders[1]
title.text = "Quarterly Report"subtitle.text = "Q4 2024 Performance Review"
Text Content
python
# Using placeholderbody = slide.placeholders[1]tf = body.text_frametf.text = "First bullet point"
# Add more paragraphsp = tf.add_paragraph()p.text = "Second bullet point"p.level = 0
p = tf.add_paragraph()p.text = "Sub-bullet"p.level = 1
Text 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 = Truep.font.size = Pt(18)
Shapes
python
from pptx.enum.shapes import MSO_SHAPE
# Rectangleshape = 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"
# Common shapes:# MSO_SHAPE.RECTANGLE, ROUNDED_RECTANGLE# MSO_SHAPE.OVAL, CHEVRON, ARROW_RIGHT# MSO_SHAPE.CALLOUT_ROUNDED_RECTANGLE
Images
python
# Add imageslide.shapes.add_picture(    'image.png',    Inches(1), Inches(2),  # position    width=Inches(4)        # auto height)
# Or specify both dimensionsslide.shapes.add_picture(    'logo.png',    Inches(8), Inches(0.5),    Inches(1.5), Inches(0.75))

Tables

python
# Create tablerows, cols = 4, 3left = 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 widthstable.columns[0].width = Inches(2)table.columns[1].width = Inches(3)table.columns[2].width = Inches(3)
# Add headersheaders = ['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
# Add datadata = [    ['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

Charts

python
from pptx.chart.data import CategoryChartDatafrom pptx.enum.chart import XL_CHART_TYPE
# Chart datachart_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))
# Add chartx, 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
# Customizechart.has_legend = Truechart.legend.include_in_layout = False

Formatting

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 = Truerun.font.italic = Truerun.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)
Paragraph Alignment
python
from pptx.enum.text import PP_ALIGN
p.alignment = PP_ALIGN.CENTER  # LEFT, RIGHT, JUSTIFY

Best Practices

  1. Use Templates: Start with a .pptx template for consistent branding
  2. Layout First: Plan slide structure before coding
  3. Reuse Slide Masters: Maintain consistency across presentations
  4. Optimize Images: Compress images before adding
  5. Test Output: Always verify generated presentations

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 prs

Data-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 chart

Examples

Example 1: Create a Pitch Deck

python
from pptx import Presentationfrom pptx.util import Inches, Pt
prs = Presentation()
# Slide 1: Titleslide = prs.slides.add_slide(prs.slide_layouts[0])slide.shapes.title.text = "StartupX"slide.placeholders[1].text = "Revolutionizing Document Processing"
# Slide 2: Problemslide = prs.slides.add_slide(prs.slide_layouts[1])slide.shapes.title.text = "The Problem"body = slide.placeholders[1].text_framebody.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 3: Solutionslide = prs.slides.add_slide(prs.slide_layouts[1])slide.shapes.title.text = "Our Solution"body = slide.placeholders[1].text_framebody.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 4: Marketslide = prs.slides.add_slide(prs.slide_layouts[5])  # Title onlyslide.shapes.title.text = "Market Opportunity: $50B by 2028"
# Add chartfrom pptx.chart.data import CategoryChartDatafrom 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')

Example 2: Report with Data Table

python
from pptx import Presentationfrom pptx.util import Inches, Pt
prs = Presentation()
# Title slideslide = prs.slides.add_slide(prs.slide_layouts[0])slide.shapes.title.text = "Sales Performance Report"slide.placeholders[1].text = "Q4 2024"
# Data slideslide = prs.slides.add_slide(prs.slide_layouts[5])slide.shapes.title.text = "Regional Performance"
# Create tabletable = slide.shapes.add_table(5, 4, Inches(0.5), Inches(1.5), Inches(9), Inches(4)).table
# Headersheaders = ['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
# Datadata = [    ['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')

Limitations

  • Cannot render complex animations
  • Limited SmartArt support
  • No video embedding via API
  • Master slide editing is complex
  • Chart types limited to standard Office charts

Installation

bash
pip install python-pptx

Resources

来源与署名

来源:claude-office-skills/skills位于pptx-manipulation提交9c4c7d5

许可证: MIT

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架