A CLI tool to serve interactive organization charts from Excel data
Project description
OrgChart
OrgChart 是一款 Python CLI 工具,可将 Excel 表格转换为交互式、可搜索的组织架构图。只需在 Excel 中定义团队结构,运行本地服务器,即可通过基于 D3.js 的美观图表浏览组织架构,支持简历弹窗、实时更新和多格式导出。
功能特性
- Excel 驱动数据管理:在任何电子表格编辑器(Excel、Google Sheets、LibreOffice)中编辑组织架构,无需数据库或复杂配置。
- 交互式 D3.js 可视化:基于 d3-org-chart 构建,支持平滑缩放、拖拽、折叠/展开和搜索。
- 智能搜索与高亮:按姓名、职位、地点、公司或部门搜索,匹配节点自动高亮,祖先路径自动展开。
- 简历弹窗:带有简历数据的节点显示 "Resume" 标签,点击打开精美的 Markdown 渲染弹窗。
- 实时热重载:服务器运行时编辑 Excel 文件,图表通过 Server-Sent Events (SSE) 自动刷新。
- 多种导出格式:一键导出当前视图、完整图表、SVG 或 PDF。
- CLI 数据导出:支持将 Excel 数据导出为 JSON、CSV、Excel 格式。
- 数据格式检查:内置
check命令,可预先验证 Excel 文件格式是否正确。 - 列名映射查看:
show命令可查看所有支持的列名及其中英文别名。 - 双语列名支持:Excel 列头支持中英文别名,不区分大小写。
- 零配置启动:通过 pip 安装后,直接 serve Excel 文件即可运行。
- 完善的异常处理:文件不存在、格式错误、端口冲突等场景均有清晰的彩色错误提示。
安装和环境配置
需要 Python 3.8 或更高版本。
通过 pip 安装
pip install orgchart
从源码安装
git clone https://github.com/your-org/orgchart.git
cd orgchart
pip install -e .
系统要求
- Python >= 3.8
- 现代浏览器(Chrome、Firefox、Safari、Edge)
使用示例和代码片段
快速开始
- 创建包含组织数据的 Excel 文件(参见下方的 Excel 数据格式)。
- 运行服务器:
orgchart serve company.xlsx
浏览器将自动打开 http://localhost:8080,展示交互式架构图。
CLI 命令
serve — 启动服务器
orgchart serve <excel_file> [选项]
| 选项 | 简写 | 默认值 | 说明 |
|---|---|---|---|
--port |
-p |
8080 |
服务器端口 |
--host |
-h |
127.0.0.1 |
绑定主机 |
--watch / --no-watch |
-w |
启用 | 监听 Excel 文件变化 |
--browser / --no-browser |
-b |
启用 | 自动打开浏览器 |
--version |
-v |
- | 显示版本并退出 |
# 使用自定义端口
orgchart serve org.xlsx --port 3000
# 禁用自动打开浏览器和文件监听
orgchart serve org.xlsx --no-browser --no-watch
# 绑定到所有网络接口(局域网共享)
orgchart serve org.xlsx --host 0.0.0.0 --port 8080
# 显示版本
orgchart --version
export — 导出数据
orgchart export <excel_file> --type <json|csv|excel> [--output PATH]
# 导出为 JSON(默认)
orgchart export company.xlsx
# 导出为 CSV
orgchart export company.xlsx --type csv
# 导出为 Excel
orgchart export company.xlsx --type excel
# 指定输出路径
orgchart export company.xlsx --type json --output /tmp/org_data.json
show — 查看列名映射
# 显示所有支持的列及其中英文别名
orgchart show
# 仅查看某一列的映射
orgchart show --column name
check — 检查数据格式
orgchart check company.xlsx
检查内容包括:文件存在性、扩展名有效性、必填列是否缺失、ID 是否重复、parentId 是否指向不存在的节点、是否存在循环依赖等。检查通过返回绿色提示,失败则逐条列出具体错误。
Python 模块调用
from orgchart.excel_reader import load_org_data
nodes, errors, mtime = load_org_data("company.xlsx")
print(f"已加载 {len(nodes)} 名员工")
for node in nodes:
print(f"{node.name} - {node.position}")
Excel 数据格式
Excel 文件的第一行必须是表头。支持以下列(列名不区分大小写,支持中英文别名):
必填列
| 列名 | 别名 | 说明 |
|---|---|---|
name |
姓名、名字、名称、员工姓名 | 员工姓名 |
position |
职位、岗位、职务、Title | 职位名称 |
可选列
| 列名 | 别名 | 说明 |
|---|---|---|
id |
ID、编号、序号 | 唯一标识符(省略时自动生成) |
parentId |
上级ID、汇报对象、上级、父节点、manager | 上级节点 ID |
linkedin |
LinkedIn、领英、linkedin链接 | LinkedIn 个人资料链接 |
location |
地点、位置、城市、办公地点 | 工作地点 |
company |
公司、企业、所属公司 | 公司名称 |
department |
部门、事业部、所属部门 | 部门名称 |
bio |
简介、一句话简介、个人简介 | 一句话简介 |
resume |
简历、详细简历、个人简历 | Markdown 格式的详细简历 |
image |
头像、照片、图片、avatar | 头像图片 URL |
link |
个人链接、主页、个人主页、website | 个人主页链接 |
层级规则
- 使用
parentId定义汇报关系。 - 顶层节点的
parentId应留空。 - 支持多个根节点。
id和parentId可以是数字或字符串,内部统一按字符串处理。- 省略
id时,自动从 1 开始递增分配。 parentId引用不存在的 ID 时,自动重置为None(根节点)并发出警告。
示例数据结构
| id | parentId | name | position | location | company | department | bio | resume | |
|---|---|---|---|---|---|---|---|---|---|
| 1 | Alex Chen | CEO | San Francisco | TechVision | Executive | https://... | 20+ years | # Resume... | |
| 2 | 1 | Jamie O'Brien | CTO | Austin | TechVision | Engineering | https://... | Scaling... | # Resume... |
| 3 | 2 | Casey Li | Engineering Manager | Seattle | TechVision | Engineering | https://... | Full-stack... | # Resume... |
示例文件
examples/sample_org.xlsx— 原始示例,包含 14 名员工。examples/sample_excel.xlsx— 完整模板,包含所有字段(公司、部门、简介等)。
前端交互特性
节点卡片布局
- 动态高度:根据实际字段内容自动调整卡片高度, company/department 标签、location、bio 字段仅在存在时显示。
- 溢出省略:所有文本字段(姓名、职位、地点、公司、部门、简介)超出宽度时自动截断,以省略号显示。
- 悬停浮层:鼠标悬停在截断的文本上时,浏览器弹出原生浮层显示完整内容。
- 姓名点击跳转:点击节点卡片上的姓名,在新标签页打开该员工的 LinkedIn 个人资料页。
- Resume 标签:右上角仅保留 "Resume" 标签(字体 9px),点击可打开 Markdown 简历弹窗。
搜索与导出
- 支持按姓名、职位、地点、公司、部门实时搜索过滤。
- 支持导出当前视图、完整图表、SVG、PDF。
项目架构
目录结构
orgchart/
├── orgchart/ # 主包
│ ├── __init__.py # 包初始化
│ ├── __main__.py # python -m orgchart 入口
│ ├── cli.py # Typer CLI 实现(serve/export/show/check)
│ ├── server.py # Flask HTTP 服务器 + SSE
│ ├── excel_reader.py # Excel 读取(pandas)
│ ├── data_models.py # 数据模型(dataclasses)
│ ├── watcher.py # 文件系统监听(watchdog)
│ └── static/ # 前端资源
│ └── index.html # 交互式图表 UI
├── examples/ # 示例文件
│ ├── sample_org.xlsx # 原始 14 人示例
│ └── sample_excel.xlsx # 全字段模板
├── tests/ # 测试套件
│ ├── test_cli.py
│ ├── test_data_models.py
│ ├── test_excel_reader.py
│ └── test_server.py
├── README.md # 项目文档
├── LICENSE # MIT 许可证
├── pyproject.toml # 项目配置
└── requirements.txt # 运行时依赖
数据流
Excel 文件 (.xlsx)
|
v
pandas.read_excel() --> orgchart/excel_reader.py
|
v
OrgNode 数据类 --> orgchart/data_models.py
|
v
Flask REST API --> orgchart/server.py
| /api/data, /api/config, /api/events
v
前端 (D3.js) --> orgchart/static/index.html
|
v
浏览器渲染 + SSE 实时更新
组件交互
- cli.py:解析命令行参数,启动服务器或执行导出/查看/检查命令。
- server.py:创建 Flask 应用,提供静态文件服务和 REST/SSE 接口。
- excel_reader.py:通过 pandas 读取 Excel,标准化列名,验证数据,并将行转换为
OrgNode对象。 - data_models.py:定义
OrgNode、OrgMeta、OrgConfigResponse数据类,以及双语列名映射表。 - watcher.py:使用 watchdog 监听 Excel 文件变化,变化时调用
reload_data()并通过 SSE 向所有浏览器广播更新事件。 - index.html:从
/api/data获取数据,使用d3-org-chart渲染组织架构图,通过marked.js打开简历弹窗,并通过/api/events的 SSE 监听实时更新。
API 接口说明
GET /api/data
返回完整的组织数据(JSON 格式)。
{
"data": [
{
"id": "1",
"parentId": null,
"name": "Alex Chen",
"position": "CEO & Founder",
"location": "San Francisco, CA",
"company": "TechVision",
"department": "Executive",
"linkedin": "https://linkedin.com/in/alexchen",
"bio": "20+ years in tech industry",
"resume": "# Alex Chen\n\n## Experience...",
"image": "https://...",
"link": "https://alexchen.dev"
}
],
"meta": {
"total": 14,
"generatedAt": "2026-05-09T12:00:00",
"source": "company.xlsx",
"version": "0.1.0"
},
"errors": []
}
GET /api/config
返回服务器配置信息。
{
"watchEnabled": true,
"lastModified": "2026-05-09T12:00:00",
"columns": ["id", "parentId", "name", "position", "location",
"company", "department", "linkedin", "bio", "resume",
"image", "link"]
}
GET /api/events
Server-Sent Events (SSE) 端点。浏览器通过 EventSource 连接,接收 Excel 文件变化时的实时更新。
event: connected
data: {}
event: data-update
data: {"message": "Excel file updated", "timestamp": "2026-05-09T12:00:00Z"}
GET /
返回交互式前端应用(HTML/CSS/JS)。
依赖项清单
运行时依赖
| 包名 | 版本 | 用途 |
|---|---|---|
| Flask | >=2.0.0 | HTTP 服务器和静态文件服务 |
| pandas | >=1.5.0 | Excel 数据读取和处理 |
| watchdog | >=2.0.0 | 文件系统监听,实现热重载 |
| typer | >=0.9.0 | CLI 参数解析 |
| openpyxl | - | pandas 读取 .xlsx 所需的引擎 |
前端依赖(CDN)
| 库名 | 版本 | 用途 |
|---|---|---|
| D3.js | v7 | 数据可视化核心 |
| d3-org-chart | 3.0.1 | 组织架构图渲染 |
| d3-flextree | 2.1.2 | 灵活树形布局 |
| marked.js | latest | Markdown 简历渲染 |
| html2canvas | 1.1.4 | 图片导出(Canvas) |
| jsPDF | 2.3.1 | PDF 导出 |
开发依赖
| 包名 | 版本 | 用途 |
|---|---|---|
| pytest | >=7.0.0 | 单元测试 |
| build | >=0.8.0 | 打包构建 |
| twine | >=4.0.0 | PyPI 发布 |
故障排除
ModuleNotFoundError: No module named 'pandas'
安装项目及其依赖:
pip install -e .
ImportError: Missing dependency for reading Excel files
pandas 需要引擎来读取 .xlsx 文件。安装 openpyxl:
pip install openpyxl
端口已被占用
更换端口:
orgchart serve company.xlsx --port 9090
Excel 文件修改后图表未更新
确保已保存文件。如果使用了 --no-watch,需手动重启服务器。
浏览器未自动打开
使用 --no-browser,然后手动访问终端打印的 URL。
循环依赖警告
检查 parentId 是否形成环路(如 A 的上级是 B,B 的上级是 A)。工具会自动检测并打断循环,同时发出警告。
贡献指南与许可证
贡献指南
欢迎贡献!参与方式:
- Fork 本仓库
- 创建功能分支(
git checkout -b feature/amazing-feature) - 提交更改(
git commit -m 'Add amazing feature') - 推送到分支(
git push origin feature/amazing-feature) - 发起 Pull Request
请确保代码通过现有测试,并为新功能添加测试。
许可证
本项目采用 MIT 许可证。详见 LICENSE 文件。
免责声明
本软件按"现状"提供,不提供任何明示或暗示的担保,包括但不限于对适销性、特定用途适用性和非侵权性的担保。在任何情况下,作者或版权持有人均不对因使用本软件或与本软件相关的其他交易而产生的任何索赔、损害或其他责任负责,无论是合同诉讼、侵权行为还是其他情况。
使用本软件生成的组织架构图和数据导出结果仅供参考,用户应自行核实数据的准确性和完整性。本软件不会将任何数据上传至远程服务器,所有数据处理均在本地完成。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file orgchart_swan-0.1.0.tar.gz.
File metadata
- Download URL: orgchart_swan-0.1.0.tar.gz
- Upload date:
- Size: 64.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0fde9ebdccd952d543e95173e5eed9f10a6bb6f52b80d2b6f042f8bf02f3e827
|
|
| MD5 |
18f9a284e24eba490e4983eadbd292db
|
|
| BLAKE2b-256 |
0693115308fd89a3551f5f3f0722cef925455e2abf366372369ce9e363278308
|
File details
Details for the file orgchart_swan-0.1.0-py3-none-any.whl.
File metadata
- Download URL: orgchart_swan-0.1.0-py3-none-any.whl
- Upload date:
- Size: 29.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b5d1eca6c16cbf0e2545c959e2d976d5dada716062e5e45c1fa2bfdf1d1ca01
|
|
| MD5 |
b175b0f837a724cbad10dd8dc7687d05
|
|
| BLAKE2b-256 |
58a7008baa8cc8865dd4e246f0d512141017b5c1e24d458a58e0ebcfdd1c455a
|