name: pptx
description: 演示文稿创建、编辑和分析。当Claude需要处理演示文稿(.pptx文件)以创建新演示文稿、修改内容、处理布局、添加演讲者笔记或任何演示文稿任务时。
source: anthropics/skills
license: Apache-2.0
PowerPoint 处理
创建演示文稿 (Python)
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
# 添加标题幻灯片
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Hello, World!"
subtitle.text = "python-pptx 演示"
# 添加内容幻灯片
bullet_slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(bullet_slide_layout)
shapes = slide.shapes
title_shape = shapes.title
body_shape = shapes.placeholders[1]
title_shape.text = "关键点"
tf = body_shape.text_frame
tf.text = "第一个要点"
p = tf.add_paragraph()
p.text = "第二个要点"
p.level = 1
prs.save('presentation.pptx')
添加图片
from pptx.util import Inches
blank_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_layout)
left = Inches(1)
top = Inches(1)
width = Inches(5)
slide.shapes.add_picture('image.png', left, top, width=width)
添加表格
rows, cols = 3, 4
left = Inches(1)
top = Inches(2)
width = Inches(6)
height = Inches(1.5)
table = slide.shapes.add_table(rows, cols, left, top, width, height).table
# 设置列宽
table.columns[0].width = Inches(2)
# 添加内容
table.cell(0, 0).text = "标题 1"
table.cell(1, 0).text = "数据 1"
添加图表
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
chart_data = CategoryChartData()
chart_data.categories = ['东部', '西部', '中西部']
chart_data.add_series('销售额', (19.2, 21.4, 16.7))
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
)
编辑现有演示文稿
prs = Presentation('existing.pptx')
# 访问幻灯片
for slide in prs.slides:
for shape in slide.shapes:
if shape.has_text_frame:
print(shape.text_frame.text)
# 修改文本
slide = prs.slides[0]
slide.shapes.title.text = "新标题"
prs.save('modified.pptx')
最佳实践
- 使用幻灯片布局保持一致性
- 保持文本简洁,使用视觉元素
- 使用Inches()或Pt()进行尺寸设置
- 创建过程中频繁保存