这个项目提供一个 Python CLI,用于把当前目录下的 .docx 文件批量转换为 Markdown,并输出 OpenAI、Anthropic、Gemini、DeepSeek 四个平台的 token 统计报告。
uv sync
uv run word-transfer默认运行时会读取 input 目录中的 .docx 文件,并把 .md 文件和 token_report.json 写入 output。
常用参数:
uv run word-transfer --input-dir test_input --output-dir test_output
uv run word-transfer --input-dir test_input --output-dir test_output --recursive
uv run word-transfer --input-dir test_input --output-dir test_output --merge --merge-name combined.md
uv run word-transfer --recursive --merge --merge-name combined.md- 仅支持
.docx,非.docx文件会被跳过。 - 空文件或损坏的
.docx会被跳过,错误原因会写入token_report.json的errors字段。 - Word 标题 1-4 分别转换为 Markdown
#、##、###、####。 - Word 标题 5 及更深层级转换为单独一行加粗文字,例如
**五级标题**。 - 加粗、斜体、加粗斜体、删除线分别转换为
**text**、*text*、***text***、~~text~~。 - 字体、字号、颜色、下划线等 Markdown 无法稳定表达的样式会被忽略。
- 无序列表转换为
- item,有序列表转换为1. item。 - 嵌套列表每层使用两个空格缩进。
- 假设源 Word 中没有图片;如果出现图片,脚本不会输出图片内容。
默认只处理 input 目录第一层的 .docx 文件,不会进入子目录。
开启 --recursive 后,脚本会递归查找 input-dir 下所有子目录中的 .docx 文件。
- 非 merge 模式会在
output-dir下创建相同的相对目录结构。 - 不包含
.docx文件的目录不会在output-dir中创建。 - 例如
input/chapter/a.docx会输出为output/chapter/a.md。
开启 --merge 后,所有 .docx 会按文件名排序后合并到一个 Markdown 文件中。
- 默认输出文件名是
merged.md,可用--merge-name修改。 - 每个源文件会先输出一级标题
# 文件名.docx。 - 与
--recursive一起使用时,一级标题会包含相对路径,例如# chapter/a.docx。 - 文件内原有标题整体顺延一级:原一级标题变为二级标题,原四级标题顺延后超过 Markdown 四级上限,因此会转为加粗文字。
- merge 模式下 token 报告只统计最终合并后的 Markdown 总 token。
脚本会在输出目录生成 token_report.json。
- OpenAI:使用
tiktoken的o200k_base本地统计,模型标签为gpt-5.5。参考 OpenAI latest model 与 tiktoken 文档。 - DeepSeek:使用 DeepSeek 官方 token usage 页面提供的 V3 离线 tokenizer 资源,并通过
tokenizers本地统计,模型标签为deepseek-chat。 - Anthropic:按要求使用离线近似,不调用官方
count_tokensAPI;报告中estimated为true。 - Gemini:按要求使用离线近似,不调用官方
count_tokensAPI;报告中estimated为true。
Anthropic 和 Gemini 的官方精确统计通常需要调用平台 API;本项目初版为了满足离线运行,明确将它们标记为估算值。
uv run pytest测试读取 test_input 中的样例 .docx,输出到被 git 忽略的 test_output。