
第14讲:PPT 智能生成 Skill 开发
掌握 PPT 的自动化生成技能,实现从内容到演示文稿的一键转换,让汇报材料制作效率提升10倍。
一、场景分析
1.1 用户痛点
制作 PPT 是职场中最耗时的工作之一:
- 内容组织困难:有了数据却不知道如何组织成清晰的演示结构
- 排版设计繁琐:调整字体、颜色、对齐方式耗费大量时间
- 图表制作复杂:将 Excel 数据转换为可视化图表步骤多
- 模板应用不便:找不到合适的模板,或者模板不会用
- 重复劳动多:每周/每月的例行汇报格式相似却要重新做
1.2 典型应用场景
| 场景 | 需求描述 | Skill 价值 |
|---|
| 数据汇报 | 将销售/财务数据自动转为汇报 PPT | 一键生成,自动图表 |
| 会议材料 | 根据议程自动生成会议 PPT | 结构化内容组织 |
| 产品介绍 | 从产品文档生成介绍 PPT | 内容智能提取 |
| 培训课件 | 将文字教材转为课件 PPT | 自动分页配图 |
| 项目总结 | 根据项目文档生成总结 PPT | 关键信息提取 |
二、核心功能设计
2.1 Skill 功能架构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| 📊 PPT 智能生成器 ├── 内容生成 │ ├── 大纲生成 │ ├── 内容扩写 │ ├── 标题优化 │ └── 要点提炼 ├── 模板应用 │ ├── 模板选择 │ ├── 主题配色 │ ├── 字体设置 │ └── 版式匹配 ├── 图表插入 │ ├── Excel 数据导入 │ ├── 图表类型推荐 │ ├── 数据可视化 │ └── 图表美化 ├── 多媒体处理 │ ├── 图片插入 │ ├── 图片排版 │ ├── 图标添加 │ └── 形状绘制 └── 批量操作 ├── 批量替换 ├── 格式统一 ├── 页码添加 └── 母版更新
|
2.2 技术选型
PPT 处理的核心技术栈:
| 功能 | Python 库 | 说明 |
|---|
| PPT 操作 | python-pptx | 创建、修改 PPT |
| 图表生成 | matplotlib / plotly | 生成数据图表 |
| 图片处理 | Pillow | 图片裁剪、调整 |
| 内容生成 | 大模型 API | 生成文案内容 |
三、技术实现
3.1 Coze 平台实现
3.1.1 基础操作代码
创建 PPT 文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| from pptx import Presentation from pptx.util import Inches, Pt from pptx.dml.color import RgbColor from pptx.enum.text import PP_ALIGN, MSO_ANCHOR
def create_presentation(): """创建新的 PPT 演示文稿""" prs = Presentation() prs.slide_width = Inches(13.333) prs.slide_height = Inches(7.5) return prs
def add_title_slide(prs, title, subtitle=None): """添加标题页""" slide_layout = prs.slide_layouts[0] slide = prs.slides.add_slide(slide_layout) title_shape = slide.shapes.title title_shape.text = title if subtitle: subtitle_shape = slide.placeholders[1] subtitle_shape.text = subtitle return slide
def add_content_slide(prs, title, content_list): """添加内容页""" slide_layout = prs.slide_layouts[1] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = title body_shape = slide.placeholders[1] tf = body_shape.text_frame for i, item in enumerate(content_list): if i == 0: p = tf.paragraphs[0] else: p = tf.add_paragraph() p.text = item p.level = 0 return slide
|
添加图表:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| from pptx.chart.data import ChartData from pptx.enum.chart import XL_CHART_TYPE from pptx.util import Inches
def add_chart_slide(prs, title, chart_data, chart_type='bar'): """ 添加带图表的幻灯片 Args: prs: Presentation 对象 title: 幻灯片标题 chart_data: 图表数据字典 chart_type: 图表类型 (bar, line, pie, column) """ slide_layout = prs.slide_layouts[5] slide = prs.slides.add_slide(slide_layout) title_shape = slide.shapes.add_textbox( Inches(0.5), Inches(0.3), Inches(12), Inches(0.8) ) title_shape.text_frame.text = title chart_data_obj = ChartData() chart_data_obj.categories = chart_data['categories'] for series_name, values in chart_data['series'].items(): chart_data_obj.add_series(series_name, values) chart_type_map = { 'bar': XL_CHART_TYPE.BAR_CLUSTERED, 'column': XL_CHART_TYPE.COLUMN_CLUSTERED, 'line': XL_CHART_TYPE.LINE, 'pie': XL_CHART_TYPE.PIE } x, y, cx, cy = Inches(1), Inches(1.5), Inches(11), Inches(5.5) chart = slide.shapes.add_chart( chart_type_map.get(chart_type, XL_CHART_TYPE.COLUMN_CLUSTERED), x, y, cx, cy, chart_data_obj ).chart chart.has_legend = True chart.legend.include_in_layout = False return slide
|
添加图片:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| def add_image_slide(prs, title, image_path, left=None, top=None, width=None, height=None): """ 添加带图片的幻灯片 Args: prs: Presentation 对象 title: 幻灯片标题 image_path: 图片路径 left, top, width, height: 位置和尺寸(英寸) """ slide_layout = prs.slide_layouts[5] slide = prs.slides.add_slide(slide_layout) title_shape = slide.shapes.add_textbox( Inches(0.5), Inches(0.3), Inches(12), Inches(0.8) ) title_shape.text_frame.text = title left = left or Inches(1) top = top or Inches(1.5) height = height or Inches(5) slide.shapes.add_picture(image_path, left, top, height=height) return slide
|
3.1.2 样式设置代码
设置字体样式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| from pptx.util import Pt from pptx.dml.color import RgbColor
def set_text_style(text_frame, font_name='微软雅黑', font_size=18, font_color=None, bold=False): """ 设置文本框样式 Args: text_frame: 文本框对象 font_name: 字体名称 font_size: 字体大小(磅) font_color: 字体颜色 RGB 元组 (R, G, B) bold: 是否加粗 """ for paragraph in text_frame.paragraphs: for run in paragraph.runs: run.font.name = font_name run.font.size = Pt(font_size) run.font.bold = bold if font_color: run.font.color.rgb = RgbColor(*font_color)
|
应用主题色:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| def apply_theme(prs, primary_color, secondary_color, background_color): """ 应用自定义主题色 Args: prs: Presentation 对象 primary_color: 主色 RGB secondary_color: 辅色 RGB background_color: 背景色 RGB """ slide_master = prs.slide_master background = slide_master.background fill = background.fill fill.solid() fill.fore_color.rgb = RgbColor(*background_color)
|
3.1.3 完整生成示例
从大纲生成 PPT:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| def generate_ppt_from_outline(outline, template=None): """ 根据大纲生成完整 PPT Args: outline: 大纲字典 { 'title': '演示标题', 'subtitle': '副标题', 'slides': [ { 'type': 'title', 'title': '标题', 'content': '内容列表' }, { 'type': 'chart', 'title': '图表页', 'chart_data': {...} } ] } template: 模板路径(可选) """ if template: prs = Presentation(template) else: prs = create_presentation() add_title_slide(prs, outline['title'], outline.get('subtitle')) for slide_data in outline['slides']: slide_type = slide_data.get('type', 'content') if slide_type == 'title': add_content_slide(prs, slide_data['title'], slide_data['content']) elif slide_type == 'chart': add_chart_slide( prs, slide_data['title'], slide_data['chart_data'], slide_data.get('chart_type', 'column') ) elif slide_type == 'image': add_image_slide( prs, slide_data['title'], slide_data['image_path'] ) output_path = f"{outline['title']}.pptx" prs.save(output_path) return output_path
|
3.2 OpenClaw 平台实现
OpenClaw 的 PPT Skill 示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| from openclaw import Skill, Tool from pptx import Presentation from pptx.util import Inches
class PPTSkill(Skill): name = "PPT智能生成器" description = "自动生成演示文稿" @Tool def create_from_text(self, title: str, content: str, output: str) -> str: """从文字内容生成 PPT""" prs = Presentation() slide = prs.slides.add_slide(prs.slide_layouts[0]) slide.shapes.title.text = title sections = content.split('\n\n') for section in sections: lines = section.strip().split('\n') if len(lines) > 0: slide = prs.slides.add_slide(prs.slide_layouts[1]) slide.shapes.title.text = lines[0] if len(lines) > 1: tf = slide.placeholders[1].text_frame for i, line in enumerate(lines[1:]): if i == 0: p = tf.paragraphs[0] else: p = tf.add_paragraph() p.text = line prs.save(output) return f"PPT 已生成: {output}" @Tool def add_data_chart(self, ppt_file: str, slide_title: str, data: dict, chart_type: str = "column") -> str: """向 PPT 添加数据图表""" prs = Presentation(ppt_file) slide = prs.slides.add_slide(prs.slide_layouts[5]) slide.shapes.title.text = slide_title prs.save(ppt_file) return "图表已添加"
|
四、Prompt 设计
4.1 系统 Prompt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 你是 PPT 智能生成助手,专门帮助用户快速创建演示文稿。
你可以执行以下操作: 1. 内容生成:根据主题生成大纲和文案 2. 模板应用:选择合适的模板并应用 3. 图表插入:将数据转换为可视化图表 4. 图片排版:自动插入和排版图片 5. 格式统一:统一字体、颜色、布局
工作流程: 1. 理解用户的 PPT 制作需求 2. 询问关键信息(主题、页数、风格等) 3. 生成内容大纲供用户确认 4. 生成完整 PPT 5. 提供修改建议
设计原则: - 每页只传达一个核心观点 - 文字精简,多用图表 - 保持视觉一致性 - 遵循"10-20-30"法则(10页、20分钟、30号字体)
|
4.2 内容生成 Prompt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 请根据以下主题生成 PPT 大纲:
主题:{topic} 目标受众:{audience} 页数要求:{pages} 页 风格:{style}
要求: 1. 生成包含标题页的结构 2. 每页包含标题和要点 3. 逻辑清晰,层次分明 4. 适合转化为 PPT 格式
输出格式: { "title": "演示标题", "subtitle": "副标题", "slides": [ { "title": "页面标题", "content": ["要点1", "要点2", "要点3"], "type": "content" } ] }
|
五、实战案例
5.1 案例一:销售数据汇报 PPT
场景:销售经理需要制作月度销售汇报 PPT。
解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| def generate_sales_report_ppt(sales_data, month): """生成销售汇报 PPT""" outline = { 'title': f'{month}月销售数据汇报', 'subtitle': '销售业绩分析与展望', 'slides': [ { 'type': 'content', 'title': '本月业绩概览', 'content': [ f'总销售额:¥{sales_data["total"]:,.0f}', f'达成率:{sales_data["achievement"]:.1%}', f'同比增长:{sales_data["yoy_growth"]:+.1%}', f'环比增长:{sales_data["mom_growth"]:+.1%}' ] }, { 'type': 'chart', 'title': '各区域销售对比', 'chart_data': { 'categories': list(sales_data['regions'].keys()), 'series': { '销售额': list(sales_data['regions'].values()) } }, 'chart_type': 'column' }, { 'type': 'chart', 'title': '产品销售占比', 'chart_data': { 'categories': list(sales_data['products'].keys()), 'series': { '销量': list(sales_data['products'].values()) } }, 'chart_type': 'pie' }, { 'type': 'content', 'title': '下月工作计划', 'content': [ '重点客户跟进:10家', '新产品推广:3款', '团队培训:2场', '目标销售额:¥500万' ] } ] } return generate_ppt_from_outline(outline)
|
5.2 案例二:产品介绍 PPT
场景:产品经理需要根据产品文档生成介绍 PPT。
解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| def generate_product_ppt(product_doc): """从产品文档生成介绍 PPT""" key_info = extract_product_info(product_doc) outline = { 'title': key_info['name'], 'subtitle': key_info['slogan'], 'slides': [ { 'type': 'content', 'title': '产品定位', 'content': key_info['positioning'] }, { 'type': 'content', 'title': '核心功能', 'content': key_info['features'] }, { 'type': 'content', 'title': '目标用户', 'content': key_info['target_users'] }, { 'type': 'content', 'title': '竞争优势', 'content': key_info['advantages'] }, { 'type': 'content', 'title': '应用场景', 'content': key_info['scenarios'] } ] } return generate_ppt_from_outline(outline)
def extract_product_info(doc): """使用大模型提取产品信息""" return { 'name': '产品名称', 'slogan': '产品口号', 'positioning': ['定位1', '定位2'], 'features': ['功能1', '功能2', '功能3'], 'target_users': ['用户1', '用户2'], 'advantages': ['优势1', '优势2'], 'scenarios': ['场景1', '场景2'] }
|
六、实战练习
练习 1:大纲转 PPT
创建一个 Skill,实现以下功能:
- 接收用户输入的主题和要点
- 自动生成 PPT 大纲
- 根据大纲生成完整 PPT
- 应用合适的模板和配色
练习 2:数据可视化 PPT
创建一个 Skill,实现以下功能:
- 读取 Excel 数据文件
- 分析数据并选择合适的图表类型
- 生成包含数据图表的 PPT
- 添加数据解读文字
练习 3:批量 PPT 处理
创建一个 Skill,实现以下功能:
- 批量修改 PPT 模板
- 统一所有幻灯片的字体和颜色
- 批量添加公司 Logo
- 批量添加页码
七、常见问题
Q1:如何保持生成的 PPT 美观?
解决方案:
- 使用预设计好的模板
- 限制每页文字数量(不超过 6 行)
- 使用大模型生成简洁的文案
- 合理运用留白
Q2:图表显示不清晰怎么办?
解决方案:
- 使用高分辨率图片
- 设置合适的图表尺寸
- 确保字体大小可读(不小于 18 磅)
- 使用对比度高的配色
Q3:如何处理中文字体问题?
解决方案:
- 指定系统中文字体(如微软雅黑、思源黑体)
- 注意字体版权问题
- 考虑将文字转为图片(不推荐,会失去可编辑性)
八、下节预告
下一讲我们将学习 Word 文档处理 Skill 开发,包括:
- Word 文档自动生成
- 模板填充与邮件合并
- 格式统一与样式设置
- 文档批量处理
加入学习群
👉 加入AI编程学习交流群

本讲是《AI Skills 从入门到实践》系列课程的第14讲。
🎓 AI 编程实战课程
想系统学习 AI 编程?程序员晚枫的 AI 编程实战课 帮你从零上手!