mcpField
P
pptx

pptx

github.com/autumnsgrove文档处理3748 次安装53 周前更新
代码仓库

关于 pptx

pptx 是一项使用 Python 的 python-pptx 库创建、编辑和自动化 PowerPoint 演示文稿的技能,适用于需要程序化生成幻灯片的工作。它涵盖文本、图表、图片、表格及模板等内容与格式处理。

主要功能

  1. 01创建和配置演示文稿
  2. 02添加、复制和重排幻灯片
  3. 03插入文本、图表、图片和表格
  4. 04设置主题、颜色与字体
  5. 05编辑现有文件和套用模板
  6. 06从数据批量生成幻灯片

适用场景

  1. 01从业务数据生成演示文稿
  2. 02批量制作报告幻灯片
  3. 03基于模板生成品牌演示文稿
  4. 04更新现有 PowerPoint 文件

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/autumnsgrove/claudeskills --skill pptx

安装后在 Claude Code 中输入/pptx确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

README.md
examples/
bulk-generation.md
business-presentation.md
chart-examples.md
editing-presentations.md
image-handling.md
table-examples.md
references/
advanced-techniques.md
design-best-practices.md
library-setup.md
templates-and-themes.md
troubleshooting.md
scripts/
pptx_helper.py

详细介绍

PowerPoint (PPTX) 技能

概述

此技能使用 Python 的 python-pptx 库,提供全面的 PowerPoint 演示文稿创建、编辑和自动化功能。以编程方式创建专业演示文稿,全面控制版式、主题、内容、图表和可视化内容。

核心功能

  • 演示文稿创建:新建演示文稿、模板、元数据、页面配置
  • 幻灯片管理:使用预定义版式添加、复制、删除和重新排序幻灯片
  • 内容类型:文本、形状、图片、表格、图表、SmartArt、超链接
  • 设计与格式设置:主题、配色方案、字体、填充、边框、效果
  • 高级功能:切换效果、动画、嵌入对象、视频/音频、评论

安装

安装所需的库:

pip install python-pptx
# or with uv
uv pip install python-pptx

基本导入:

from pptx import Presentation
from pptx.util import Inches, Pt, Cm
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN, MSO_ANCHOR
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE

如需完整的库安装和配套程序包(Pillow、pandas、matplotlib),请参阅 references/library-setup.md。

核心工作流

工作流 1:创建商务演示文稿

目标: 创建包含标题幻灯片、内容幻灯片和结尾页的专业演示文稿。

步骤:

  1. 初始化演示文稿

    • 创建新的演示文稿对象
    • 设置幻灯片尺寸(标准 16:9 或 4:3)
    • 配置元数据(标题、作者、主题、关键词)
  2. 添加标题幻灯片

    • 使用标题幻灯片版式(通常为 prs.slide_layouts[0])
    • 设置标题和副标题文本
    • 应用格式(字号、颜色、粗体)
  3. 添加内容幻灯片

    • 使用合适的版式(项目符号、双栏、仅标题、空白)
    • 填充占位符或添加文本框
    • 设置文本格式,体现适当的层级
  4. 添加视觉元素

    • 插入尺寸和位置合适的图片
    • 添加格式化数据图表
    • 创建并设置单元格样式的表格
  5. 保存演示文稿

    • 保存为 .pptx 格式
    • 验证文件是否已创建

快速示例:

from pptx import Presentation
from pptx.util import Inches, Pt

prs = Presentation()
prs.slide_width = Inches(10)
prs.slide_height = Inches(7.5)

# Title slide
slide = prs.slides.add_slide(prs.slide_layouts[0])
slide.shapes.title.text = "Q4 Business Review"
slide.placeholders[1].text = "Prepared by: Jane Doe\nDate: October 25, 2025"

prs.save('presentation.pptx')

完整实现请参阅 examples/business-presentation.md。

工作流 2:添加图表

目标: 创建柱状图、折线图和饼图等数据可视化内容。

步骤:

  1. 使用 CategoryChartData 准备图表数据
  2. 定义类别和数据系列
  3. 设置位置并将图表添加到幻灯片
  4. 设置图表格式(图例、网格线、标签)

快速示例:

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

chart_data = CategoryChartData()
chart_data.categories = ['Q1', 'Q2', 'Q3', 'Q4']
chart_data.add_series('2025', (9.5, 10.8, 11.2, 13.1))

chart = slide.shapes.add_chart(
    XL_CHART_TYPE.COLUMN_CLUSTERED,
    Inches(1), Inches(2), Inches(8), Inches(4.5),
    chart_data
).chart

所有图表类型请参阅 examples/chart-examples.md。

工作流 3:处理图片

目标: 在演示文稿中添加、定位和设置图片格式。

步骤:

  1. 使用 slide.shapes.add_picture() 添加图片
  2. 指定位置(左侧、顶部)和尺寸(宽度、高度)
  3. 如有需要,计算居中位置
  4. 添加图片前进行优化(使用 Pillow 预处理)

快速示例:

# Add image with auto-scaled aspect ratio
pic = slide.shapes.add_picture('logo.png', Inches(1), Inches(1), height=Inches(2))

# Center image on slide
pic.left = int((prs.slide_width - pic.width) / 2)
pic.top = int((prs.slide_height - pic.height) / 2)

高级技巧请参阅 examples/image-handling.md。

工作流 4:创建表格

目标: 添加带格式的结构化数据表格。

步骤:

  1. 定义表格尺寸(行数、列数)
  2. 设置位置并添加表格
  3. 设置列宽
  4. 为表头设置粗体格式和背景颜色
  5. 设置数据单元格的填充内容和对齐方式

快速示例:

table = slide.shapes.add_table(4, 3, Inches(1.5), Inches(2), Inches(7), Inches(3)).table

# Header formatting
cell = table.cell(0, 0)
cell.text = "Product"
cell.text_frame.paragraphs[0].font.bold = True
cell.fill.solid()
cell.fill.fore_color.rgb = RGBColor(0, 51, 102)

高级格式设置请参阅 examples/table-examples.md。

工作流 5:编辑现有演示文稿

目标: 修改现有 PowerPoint 文件。

步骤:

  1. 使用 Presentation('file.pptx') 打开演示文稿
  2. 遍历幻灯片以查找内容
  3. 修改文本和形状,或添加新元素
  4. 使用相同或不同的文件名保存

快速示例:

prs = Presentation('existing.pptx')

# Find and update text
for slide in prs.slides:
    for shape in slide.shapes:
        if hasattr(shape, "text") and "Old Name" in shape.text:
            shape.text = shape.text.replace("Old Name", "New Name")

prs.save('updated.pptx')

复制幻灯片和高级编辑请参阅 examples/editing-presentations.md。

工作流 6:使用模板

目标: 使用母版幻灯片和模板,保持品牌风格一致。

步骤:

  1. 从模板文件开始:Presentation('template.pptx')
  2. 检查可用版式
  3. 使用模板版式添加幻灯片
  4. 始终使用统一的品牌颜色

快速示例:

prs = Presentation('corporate_template.pptx')

# Use template layouts
title_slide = prs.slides.add_slide(prs.slide_layouts[0])
content_slide = prs.slides.add_slide(prs.slide_layouts[1])

# Layouts inherit master formatting
prs.save('branded_presentation.pptx')

母版幻灯片自定义请参阅 references/templates-and-themes.md。

工作流 7:批量生成幻灯片

目标: 根据数据自动生成多张幻灯片。

步骤:

  1. 从 CSV、JSON 或数据库加载数据
  2. 创建演示文稿对象
  3. 遍历数据记录
  4. 为每条记录生成一张幻灯片
  5. 使用记录数据填充幻灯片

快速示例:

import pandas as pd

df = pd.read_csv('employee_data.csv')
prs = Presentation()

for _, row in df.iterrows():
    slide = prs.slides.add_slide(prs.slide_layouts[1])
    slide.shapes.title.text = row['Name']
    # Add employee details to slide body

prs.save('employee_directory.pptx')

完整实现请参见 examples/bulk-generation.md。

设计原则

颜色与字体排版
  • 使用 60-30-10 配色规则(60% 主色、30% 次色、10% 强调色)
  • 确保符合 WCAG AA 对比度要求(最低 4.5:1)
  • 字体系列最多使用 2 种
  • 正文最小字号:18pt,以确保可读性
布局与构图
  • 使用三分法安排元素位置
  • 四周至少保留 0.5" 的页边距
  • 每张幻灯片上的元素数量控制在 5-7 个
  • 保持对齐一致(贴合网格)
视觉层次
  • 尺寸体现重要性(越大越重要)
  • 使用颜色对比突出重点
  • 按 Z 字形安排内容流向
图表最佳实践
  • 选择合适的图表类型(比较用条形图,趋势用折线图,部分与整体关系用饼图)
  • 颜色最多使用 3-5 种
  • 始终标注坐标轴并显示数据标签
  • 尽量少用网格线

完整设计指南请参见 references/design-best-practices.md。

常见模式

应用品牌颜色
BRAND_COLORS = {
    'primary': RGBColor(0, 51, 102),
    'secondary': RGBColor(0, 153, 204),
    'accent': RGBColor(255, 102, 0)
}

# Apply to text
shape.text_frame.paragraphs[0].font.color.rgb = BRAND_COLORS['primary']

# Apply to fill
shape.fill.solid()
shape.fill.fore_color.rgb = BRAND_COLORS['secondary']
居中元素
def center_shape(shape, prs):
    """Center shape on slide."""
    shape.left = int((prs.slide_width - shape.width) / 2)
    shape.top = int((prs.slide_height - shape.height) / 2)
文本自动适配
from pptx.enum.text import MSO_AUTO_SIZE

text_frame = shape.text_frame
text_frame.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE  # Shrink text
# or
text_frame.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT  # Expand shape

故障排除速查

“ModuleNotFoundError: No module named 'pptx'”

pip install python-pptx

“AttributeError: 'NoneType' object has no attribute...”

  • 检查占位符索引:[p.placeholder_format.idx for p in slide.placeholders]
  • 确认布局包含预期的占位符

未找到图像

  • 使用绝对路径:os.path.abspath('image.png')
  • 确认文件存在:os.path.exists(img_path)

文本无法放入

  • 启用自动适配:text_frame.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE
  • 使用省略号截断过长文本

文件体积过大

  • 添加图像前先压缩(使用 Pillow)
  • 将图像调整为演示文稿尺寸(最大 1920x1080)

完整故障排除指南请参见 references/troubleshooting.md。

辅助脚本

scripts/pptx_helper.py 模块提供实用函数:

  • create_presentation():使用默认设置初始化
  • add_title_slide():添加格式化的标题幻灯片
  • add_bullet_slide():添加包含项目符号的幻灯片
  • add_image_slide():添加包含居中图像的幻灯片
  • add_chart_slide():添加包含图表的幻灯片
  • add_table_slide():添加格式化的表格
  • apply_brand_colors():应用一致的配色方案
  • optimize_images():批量优化图像

用法:

from scripts.pptx_helper import create_presentation, add_title_slide, add_chart_slide

prs = create_presentation(title="My Presentation")
add_title_slide(prs, "Main Title", "Subtitle")
add_chart_slide(prs, "Sales Data", chart_type='bar',
                categories=['Q1', 'Q2', 'Q3', 'Q4'],
                values=[10, 20, 15, 25])
prs.save('output.pptx')

更多资源

文档
详细参考资料
示例
设计资源

最佳实践总结

  1. 始终使用模板,确保品牌形象统一
  2. 将图片添加到演示文稿前先进行优化
  3. 限制每张幻灯片上的文字(最多 5–7 个项目符号)
  4. 使用高对比度,确保易于阅读
  5. 演示前在目标设备上进行测试
  6. 控制文件大小(电子邮件附件小于 20 MB)
  7. 使用演讲者备注记录详细的讲解要点
  8. 遵循 6×6 规则:最多 6 个项目符号,每个项目符号最多 6 个词
  9. 创建图表前验证数据
  10. 保持间距和对齐方式一致

使用此技能的场景:

  • 根据数据创建商务演示文稿
  • 自动生成报告
  • 从数据库批量创建幻灯片
  • 创建基于模板的演示文稿
  • 创建包含图表/图片的教育内容
  • 将文档转换为幻灯片

评论

支持 Markdown