Create PowerPoint (python-pptx)
Setup
CODEBLOCK0
Output scripts to a logical location (e.g., 前端开发/demo/ or project folder),
then run with python3 <script.py> and open the result.
Core helpers
Read and import scripts/pptx_helpers.py for ready-made drawing primitives:
background, horizontal/vertical lines, textboxes, ovals, diagonal connectors,
and fade transitions. Copy or import as needed.
Key units: EMU (English Metric Units). 1 pt = 12700 EMU, 1 cm ≈ 360000 EMU.
Standard 16:9 slide = 12192000 × 6858000 EMU.
Workflow
- 1. Understand the content — milestones, categories, colors, # slides
- Plan layout — compute X/Y positions in EMU up front; avoid magic numbers
- Build shapes — use helpers or INLINECODE4
- Add transitions — always call
add_fade_transition(slide) from helpers - Run and open — INLINECODE6
Multi-slide instead of click animations (WPS-safe default)
WPS does not reliably support click-triggered PowerPoint animations.
Always use multiple slides to reveal content progressively:
CODEBLOCK1
Add a fade transition (add_fade_transition) to each slide for smooth switching.
If the user explicitly asks for animations AND they are using Microsoft PowerPoint
(not WPS), you may attempt XML-based animations — but read references/wps-compat.md
first for the XML structure and known pitfalls.
Common patterns
Colors & theme
Define all colors as RGBColor constants at the top. Dark backgrounds look
premium — use near-black (0x06, 0x0D, 0x1E) with bright accents.
Timeline layout
CODEBLOCK2
Collision resolution
When multiple cards share the same or nearby X position, spread them:
CODEBLOCK3
Shape IDs for animation
INLINECODE11 assigns shape IDs automatically. To retrieve them after creation:
CODEBLOCK4
For connectors added via raw XML, read back the max existing ID first:
CODEBLOCK5
Template assets
Ready-to-use .pptx base files in assets/. Use them as the starting
Presentation() object to inherit their design/theme:
CODEBLOCK6
| File | Style | Source |
|---|
| INLINECODE15 | 深色商务 · Pitch Deck 风格 · 60 slides | Slidesgo "Product Vision Pitch Deck" (Attribution required) |
| INLINECODE16 |
明亮教育 · 笔记本课程风格 · 多 slides | Slidesgo "Notebook Lesson XL" (Attribution required) |
Attribution: Free Slidesgo templates require keeping the attribution slide.
When using these files, do NOT delete the last "Credits" slide.
Reference files
Read the relevant file based on the task:
- -
references/pptx-patterns.md — EMU 单位速查、预设形状 ID、连接器 XML、
过渡 XML、多段落文字框、典型脚本结构
- -
references/charts.md — python-pptx 原生图表 API:柱状、折线、饼图、散点、
多系列、样式设置(当用户需要数据图表时读此文件)
- -
references/standard-slides.md — 标准商务幻灯片函数库:标题页、目录页、
要点页、图文并排、数据页、章节分隔页、结尾页(当用户需要完整 PPT 结构时读此文件)
- -
references/wps-compat.md — WPS 动画兼容性踩坑记录(当用户提到 WPS 或
动画效果异常时读此文件)
创建 PowerPoint (python-pptx)
环境配置
bash
pip install python-pptx # 或:uv pip install python-pptx
将脚本输出到逻辑位置(例如 前端开发/demo/ 或项目文件夹),
然后使用 python3 运行并打开结果文件。
核心辅助函数
阅读并导入 scripts/pptx_helpers.py 以获取现成的绘图原语:
背景、水平/垂直线、文本框、椭圆、对角线连接器
以及淡入过渡效果。根据需要复制或 import。
关键单位:EMU(英制度量单位)。1 pt = 12700 EMU,1 cm ≈ 360000 EMU。
标准 16:9 幻灯片 = 12192000 × 6858000 EMU。
工作流程
- 1. 理解内容 — 里程碑、类别、颜色、幻灯片数量
- 规划布局 — 预先计算 EMU 中的 X/Y 位置;避免使用魔法数字
- 构建形状 — 使用辅助函数或 slide.shapes.addshape/addtextbox
- 添加过渡 — 始终从辅助函数调用 addfadetransition(slide)
- 运行并打开 — python3 script.py && open output.pptx
使用多幻灯片替代点击动画(WPS 安全默认设置)
WPS 无法可靠支持点击触发的 PowerPoint 动画。
始终使用多张幻灯片来逐步展示内容:
幻灯片 1 → 仅骨架/结构
幻灯片 2 → 骨架 + 第一层数据
幻灯片 3 → 骨架 + 所有数据层
为每张幻灯片添加淡入过渡效果(addfadetransition)以实现平滑切换。
如果用户明确要求动画并且他们使用的是 Microsoft PowerPoint
(而非 WPS),可以尝试基于 XML 的动画 — 但请先阅读 references/wps-compat.md
了解 XML 结构和已知陷阱。
常见模式
颜色与主题
在顶部将所有颜色定义为 RGBColor 常量。深色背景显得高级 —
使用接近黑色(0x06, 0x0D, 0x1E)搭配亮色强调。
时间线布局
python
TLL, TLR = 850000, 11950000 # 左右边距(EMU)
TLW = TLR - TL_L
MSTEP = TLW // 11 # 12 个月 → 11 个间隔
def month_x(m): # 基于 1 的月份 → EMU X 位置
return TLL + MSTEP * (m - 1)
碰撞解决
当多个卡片共享相同或相近的 X 位置时,进行分散:
python
def resolvecollisions(events, cardw, gap):
events.sort(key=lambda e: e[cx])
need = card_w + gap
for _ in range(120):
moved = False
for i in range(len(events) - 1):
a, b = events[i], events[i+1]
if b[cx] - a[cx] < need:
push = (need - (b[cx] - a[cx])) / 2
a[cx] -= push; b[cx] += push; moved = True
if not moved:
break
动画的形状 ID
python-pptx 会自动分配形状 ID。创建后获取它们:
python
shp = slide.shapes.add_shape(...)
shapeid = shp.shapeid # 在动画 XML 中使用此 ID
对于通过原始 XML 添加的连接器,先读取最大现有 ID:
python
def maxexisting_id(slide):
return max((int(el.get(id)) for el in slide.element.iter()
if el.get(id) and el.get(id).isdigit()), default=1)
模板资源
assets/ 中提供可直接使用的 .pptx 基础文件。将它们用作起始
Presentation() 对象以继承其设计/主题:
python
from pptx import Presentation
prs = Presentation(/Users/scott/.cursor/skills/create-pptx/assets/business-dark.pptx)
| 文件 | 风格 | 来源 |
|---|
| assets/business-dark.pptx | 深色商务 · Pitch Deck 风格 · 60 张幻灯片 | Slidesgo Product Vision Pitch Deck(需注明出处) |
| assets/education.pptx |
明亮教育 · 笔记本课程风格 · 多张幻灯片 | Slidesgo Notebook Lesson XL(需注明出处) |
注明出处:免费的 Slidesgo 模板需要保留致谢幻灯片。
使用这些文件时,请勿删除最后的致谢幻灯片。
参考文件
根据任务阅读相关文件:
- - references/pptx-patterns.md — EMU 单位速查、预设形状 ID、连接器 XML、
过渡 XML、多段落文本框、典型脚本结构
- - references/charts.md — python-pptx 原生图表 API:柱状图、折线图、饼图、散点图、
多系列、样式设置(当用户需要数据图表时阅读此文件)
- - references/standard-slides.md — 标准商务幻灯片函数库:标题页、目录页、
要点页、图文并排页、数据页、章节分隔页、结尾页(当用户需要完整 PPT 结构时阅读此文件)
- - references/wps-compat.md — WPS 动画兼容性踩坑记录(当用户提到 WPS 或
动画效果异常时阅读此文件)