如果用户要求“把流程图导出为 PPT”,Gorilla 如何调用 python-pptx 库一键生成?
解读
- 场景定位:国内面试常把“一句话需求”抛给候选人,考察能否瞬间拆成“可执行 API 调用”。
- 角色定位:Gorilla 不是写脚本,而是“零样本”直接吐出一段可运行、可验证的 python-pptx 代码片段,用户复制即可生成 .pptx。
- 关键难点:
- 流程图数据结构未给出——必须假设最常见输入(Mermaid JSON、Graphviz DOT 或 networkx.DiGraph)。
- 版式合规——国内企业 PPT 默认“16:9 + 思源黑体 + 蓝灰主色”,否则会被质疑“不专业”。
- 一键——不能让用户再改代码,Gorilla 输出即成品。
- 评价维度:准确率、可验证性、对国内办公审美的贴合度。
知识点
- python-pptx 核心对象:Presentation、slide_layout、shapes.add_shape()、Connector、TextFrame、Paragraph、Font。
- 流程图 → 图论:节点 list[{id, label, x, y}]、边 list[{from, to}]。
- 坐标系:EMU(English Metric Unit),1 cm = 360 000 EMU;国内模板 16:9 建议画布 25.4 cm × 14.29 cm。
- 国内默认字体:Windows 自带“思源黑体”或“微软雅黑”,避免 Linux 服务器缺字体导致 □□。
- 颜色规范:主色 #2F5597(科技蓝)、辅色 #B4C6E7(淡蓝)、强调 #FF9C2A(橙)。
- 可验证:代码末尾自动保存为 flowchart.pptx,并 assert os.path.exists,确保 CI 可测。
- Gorilla 输出格式:只给代码块,注释用中文,方便国内开发同事一眼看懂。
答案
# Gorilla 零样本输出:把流程图导出为 PPT(python-pptx 一键生成)
# 输入:nodes 和 edges 两个列表,已序列化在内存
# 输出:当前目录下的 flowchart.pptx,16:9 版式,可直接投屏
from pptx import Presentation
from pptx.util import Cm
from pptx.enum.shapes import MSO_SHAPE
from pptx.dml.color import RGBColor
import os
# ---- 1. 假设的流程图数据(面试现场可直接替换为真实接口返回) ----
nodes = [
{"id": "start", "label": "开始", "x": 2, "y": 2},
{"id": "input", "label": "输入数据", "x": 2, "y": 4},
{"id": "check", "label": "数据合法?", "x": 2, "y": 6},
{"id": "process", "label": "处理数据", "x": 5, "y": 6},
{"id": "end", "label": "结束", "x": 2, "y": 8},
]
edges = [
{"from": "start", "to": "input"},
{"from": "input", "to": "check"},
{"from": "check", "to": "process"},
{"from": "process", "to": "end"},
{"from": "check", "to": "end"}, # 不合法直接结束
]
# ---- 2. 创建 16:9 空白演示文稿 ----
prs = Presentation()
# 移除默认幻灯片,保证干净
prs.slides._sldIdLst.clear()
blank_layout = prs.slide_layouts[6] # 6=空白布局
slide = prs.slides.add_slide(blank_layout)
# 设置画布大小:33.867 cm x 19.05 cm(16:9)
prs.slide_width = Cm(33.867)
prs.slide_height = Cm(19.05)
# ---- 3. 绘制节点:圆角矩形,统一 3 cm x 1.5 cm ----
shape_map = {} # id -> shape 对象,用于后续连箭头
for n in nodes:
left = Cm(n["x"] * 3) # 横向步长 3 cm,防止重叠
top = Cm(n["y"] * 1.8)
width = Cm(3)
height = Cm(1.5)
shape = slide.shapes.add_shape(
MSO_SHAPE.ROUNDED_RECTANGLE, left, top, width, height
)
shape.fill.solid()
shape.fill.fore_color.rgb = RGBColor(0x2F, 0x55, 0x97) # 科技蓝
shape.line.color.rgb = RGBColor(0xB4, 0xC6, 0xE7) # 淡蓝边框
# 文字
tf = shape.text_frame
tf.text = n["label"]
tf.paragraphs[0].font.size = Cm(0.6)
tf.paragraphs[0].font.name = "微软雅黑"
tf.paragraphs[0].font.color.rgb = RGBColor(255, 255, 255)
tf.margin_top = Cm(0.2)
shape_map[n["id"]] = shape
# ---- 4. 绘制带箭头的连接线 ----
for e in edges:
from_shape = shape_map[e["from"]]
to_shape = shape_map[e["to"]]
connector = slide.shapes.add_connector(
connector_type=1, # 直线箭头
begin_x=from_shape.left + from_shape.width / 2,
begin_y=from_shape.top + from_shape.height,
end_x=to_shape.left + to_shape.width / 2,
end_y=to_shape.top
)
connector.line.color.rgb = RGBColor(0x2F, 0x55, 0x97)
connector.line.width = Cm(0.03)
connector.line.begin_arrowhead_style = 1 # 无起点箭头
connector.line.end_arrowhead_style = 3 # 三角箭头
# ---- 5. 保存并验证 ----
save_path = "flowchart.pptx"
prs.save(save_path)
assert os.path.exists(save_path), "生成失败"
print(f"已生成 {os.path.abspath(save_path)},可直接用于汇报")
复制以上代码即可“一键”得到符合国内审美的流程图 PPT,Gorilla 零样本输出完成。
拓展思考
- 如果流程图来自 Mermaid,可先走
mermaid-parser把.mmd转成上述 nodes/edges JSON,再喂给同一段代码,实现“Mermaid→PPT”全自动。 - 节点过多时,可引入
networkx.spring_layout自动计算坐标,再按 16:9 画布等比缩放,防止溢出。 - 国内部分公司要求 WPS 兼容,需避免使用
MSO_SHAPE里 WPS 未实现的枚举值(如FLOW_CHART_DECISION旧版本缺失),用基础形状最稳。 - 若需批量生成上百页流程图,可把代码封装成 FastAPI 微服务,Gorilla 直接返回
/download/{task_id}.pptx下载链接,满足中台化场景。