本文目录导读:

- 使用 Python + Graphviz(推荐)
- JavaScript + D3.js(Web端)
- Python + Matplotlib(静态图)
- Shell脚本 + Mermaid(Markdown友好)
- 使用建议
我来介绍几种生成组织结构图的脚本方案,从简单到专业:
使用 Python + Graphviz(推荐)
from graphviz import Digraph
# 创建有向图
dot = Digraph(comment='组织结构图', format='png')
dot.attr(rankdir='TB') # 从上到下布局
# 设置节点样式
dot.attr('node', shape='box', style='filled', fillcolor='lightblue')
# 添加节点
dot.node('CEO', 'CEO\n张三')
dot.node('CTO', 'CTO\n李四')
dot.node('CFO', 'CFO\n王五')
dot.node('COO', 'COO\n赵六')
# 添加部门节点
dot.node('dev1', '开发一部\n10人')
dot.node('dev2', '开发二部\n8人')
dot.node('finance', '财务部\n5人')
dot.node('hr', '人力资源\n3人')
# 添加边(层级关系)
dot.edges(['CEO-CTO', 'CEO-CFO', 'CEO-COO'])
dot.edges(['CTO-dev1', 'CTO-dev2'])
dot.edges(['CFO-finance'])
dot.edges(['COO-hr'])
# 渲染输出
dot.render('org_chart', view=True) # view=True 自动打开图片
JavaScript + D3.js(Web端)
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
.node circle {
fill: #69b3a2;
stroke: #555;
stroke-width: 2px;
}
.node text {
font: 12px sans-serif;
}
.link {
fill: none;
stroke: #555;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<svg width="800" height="600"></svg>
<script>
// 组织数据
const data = {
name: "CEO",
children: [
{
name: "CTO",
children: [
{ name: "Dev Team 1", children: [] },
{ name: "Dev Team 2", children: [] }
]
},
{
name: "CFO",
children: [
{ name: "Finance", children: [] }
]
},
{
name: "COO",
children: [
{ name: "HR", children: [] },
{ name: "Admin", children: [] }
]
}
]
};
// 创建树形布局
const width = 800;
const height = 600;
const treeLayout = d3.tree().size([width - 100, height - 100]);
const root = d3.hierarchy(data);
treeLayout(root);
// 绘制图形
const svg = d3.select("svg");
// 绘制连线
svg.selectAll(".link")
.data(root.links())
.enter().append("path")
.attr("class", "link")
.attr("d", d3.linkVertical()
.x(d => d.x)
.y(d => d.y));
// 绘制节点
const nodes = svg.selectAll(".node")
.data(root.descendants())
.enter().append("g")
.attr("class", "node")
.attr("transform", d => `translate(${d.x},${d.y})`);
nodes.append("circle").attr("r", 8);
nodes.append("text")
.attr("dy", "0.35em")
.attr("x", d => d.children ? -10 : 10)
.style("text-anchor", d => d.children ? "end" : "start")
.text(d => d.data.name);
</script>
</body>
</html>
Python + Matplotlib(静态图)
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def draw_org_chart():
fig, ax = plt.subplots(figsize=(10, 8))
ax.set_xlim(0, 10)
ax.set_ylim(0, 8)
ax.axis('off')
# 定义节点位置和大小
nodes = {
'CEO': (5, 7),
'CTO': (3, 5),
'CFO': (5, 5),
'COO': (7, 5),
'Dev1': (1.5, 3),
'Dev2': (4.5, 3),
'Finance': (5, 3),
'HR': (7, 3)
}
# 绘制连线
connections = [
('CEO', 'CTO'), ('CEO', 'CFO'), ('CEO', 'COO'),
('CTO', 'Dev1'), ('CTO', 'Dev2'),
('CFO', 'Finance'), ('COO', 'HR')
]
for parent, child in connections:
p_pos = nodes[parent]
c_pos = nodes[child]
ax.plot([p_pos[0], c_pos[0]], [p_pos[1]-0.3, c_pos[1]+0.3],
'k-', lw=1, alpha=0.5)
# 绘制节点
for name, (x, y) in nodes.items():
rect = patches.FancyBboxPatch((x-0.8, y-0.3), 1.6, 0.6,
boxstyle="round,pad=0.1",
facecolor='lightblue',
edgecolor='black')
ax.add_patch(rect)
ax.text(x, y, name, ha='center', va='center', fontsize=10)
plt.title('组织结构图', fontsize=14)
plt.tight_layout()
plt.show()
draw_org_chart()
Shell脚本 + Mermaid(Markdown友好)
#!/bin/bash
# 生成Mermaid格式的组织结构图
cat > org_chart.md << 'EOF'
```mermaid
graph TD
CEO[CEO 张三]
CTO[CTO 李四]
CFO[CFO 王五]
COO[COO 赵六]
Dev1[开发一部]
Dev2[开发二部]
Finance[财务部]
HR[人力资源部]
CEO --> CTO
CEO --> CFO
CEO --> COO
CTO --> Dev1
CTO --> Dev2
CFO --> Finance
COO --> HR
EOF
echo "已生成 mermaid 格式的组织结构图: org_chart.md" echo "使用支持 Mermaid 的工具(如 GitHub、Typora)查看"
## 5. Excel VBA(企业内部使用)
```vba
Sub CreateOrgChart()
Dim ws As Worksheet
Set ws = ActiveSheet
' 创建组织结构图
Dim chartObj As Shape
Dim orgChart As SmartArt
' 添加SmartArt组织结构图
Set chartObj = ws.Shapes.AddSmartArt( _
Application.SmartArtLayouts(1), _
Left:=100, Top:=50, Width:=500, Height:=400)
Set orgChart = chartObj.SmartArt
' 添加节点
With orgChart.Nodes
.AddSmartArtNode msoSmartArtNodeBelow
.AddSmartArtNode msoSmartArtNodeBelow
.AddSmartArtNode msoSmartArtNodeBelow
End With
' 设置文本
orgChart.Nodes(1).TextFrame2.TextRange.Text = "CEO"
orgChart.Nodes(2).TextFrame2.TextRange.Text = "CTO"
orgChart.Nodes(3).TextFrame2.TextRange.Text = "CFO"
orgChart.Nodes(4).TextFrame2.TextRange.Text = "COO"
MsgBox "组织结构图已创建!"
End Sub
使用建议
| 场景 | 推荐方案 |
|---|---|
| 生成静态图片 | Python + Graphviz |
| Web展示 | JavaScript + D3.js |
| 快速生成 | Mermaid / 在线工具 |
| 企业内部 | Excel VBA |
| 数据量大 | Python + Pandas + Graphviz |
根据你的具体需求(数据源、输出格式、交互性等)选择最适合的方案。