将 Python 源代码文件递归转换为 Markdown 文档的命令行工具。
Project description
py2md
🐍 将 Python 源代码文件递归转换为结构化 Markdown 文档的命令行工具。
功能特性
- 递归扫描:自动遍历目标目录下所有
.py文件(支持自定义扩展名) - 保留层级:输出 Markdown 中完整保留文件的目录结构路径
- 灵活配置:支持自定义编码、排除隐藏文件、多扩展名过滤等选项
- 友好 CLI:基于 Typer 的命令行接口,支持彩色输出和详细统计
- 类型安全:全面使用 Type Hints,提供
py.typed标记文件
安装
pip install py2md-cli
快速开始
# 最简用法:扫描 my_project 目录,自动生成 my_project.md
py2md my_project
# 指定输出文件
py2md my_project --output docs/source.md
# 使用 GBK 编码读取
py2md my_project --encoding gbk
# 包含隐藏文件/目录
py2md my_project --include-hidden
# 同时收集 .py 和 .pyx 文件
py2md my_project --ext .py --ext .pyx
命令行参数
| 参数 | 缩写 | 说明 | 默认值 |
|---|---|---|---|
INPUT_DIR |
— | 待扫描的目标文件夹路径(必填) | — |
--output |
-o |
输出 Markdown 文件路径 | 与输入目录同名的 .md 文件 |
--encoding |
-e |
读取源文件时使用的字符编码 | utf-8 |
--exclude-hidden / --include-hidden |
— | 是否排除隐藏文件和目录 | 排除 |
--ext |
-x |
要收集的文件扩展名(可多次指定) | .py |
输出示例
生成的 Markdown 文件结构如下:
# my_project — Python 文件汇总
> **生成时间**:2026-07-08 10:30:00 UTC
> **根目录**:`/path/to/my_project`
## 路径:`main.py`
\`\`\`python
# main.py 的源代码内容
\`\`\`
---
## 路径:`utils/helpers.py`
\`\`\`python
# helpers.py 的源代码内容
\`\`\`
---
作为 Python 库使用
from py2md import py_to_markdown
result = py_to_markdown(
input_dir="./my_project",
output_path="output.md",
encoding="utf-8",
exclude_hidden=True,
)
print(f"扫描文件数:{result.files_found}")
print(f"成功:{result.files_success}")
print(f"失败:{result.files_failed}")
print(f"成功率:{result.success_rate}%")
从源码安装(开发模式)
git clone https://github.com/py2md/py2md.git
cd py2md
pip install -e ".[dev]"
构建与发布
# 构建分发包
python -m build
# 上传到 PyPI
twine upload dist/*
# 上传到 TestPyPI(测试)
twine upload --repository testpypi dist/*
许可证
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
py2md_cli-0.1.1.tar.gz
(11.6 kB
view details)
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
py2md_cli-0.1.1-py3-none-any.whl
(11.9 kB
view details)
File details
Details for the file py2md_cli-0.1.1.tar.gz.
File metadata
- Download URL: py2md_cli-0.1.1.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dea9da83e5786bfe03cad368a151f84471857f6c366ed7d66ecd2eb7734d08f7
|
|
| MD5 |
e86fcfcd1071afbee16893d5969e694a
|
|
| BLAKE2b-256 |
4129da2a1fefa70dec54ace8f0da8d9fa3eca964e0b98d4f6e4424ac9cd42b07
|
File details
Details for the file py2md_cli-0.1.1-py3-none-any.whl.
File metadata
- Download URL: py2md_cli-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
928328115269782f8c49cc75e616c054a261aeee07a656d146b015f708ace218
|
|
| MD5 |
a4eb8724803fb35fc771db19fc376ec6
|
|
| BLAKE2b-256 |
f9c180fc7f5aa982499278e93449979c96ec38d2658f92a769f06bc00f5886ad
|