anydoc 由 Firecrawl 开发,旨在为 LLM 和 AI 代理提供统一的文档读取接口。其核心优势包括:

  • 广泛的格式支持:支持 14 种格式,包括 Word (.doc, .docx)、PowerPoint (.ppt, .pptx)、Excel (.xls, .xlsx)、OpenDocument、RTF、EPUB、CSV 和 PDF。
  • 统一且高质量的输出:所有格式解析为同一个文档模型,并渲染为一致的 Markdown,完整保留标题、表格、列表、脚注、公式(LaTeX)和图片等结构。
  • 极快的速度:纯 Rust 实现,无外部依赖或 ML 模型,中位转换时间 < 5ms
  • 智能格式检测:通过文件内容(而非扩展名)识别格式,即使文件后缀错误也能正确转换。
  • 多语言绑定:提供 Node.js、Python浏览器 (WebAssembly) 的 SDK,可无缝集成。
  • Agent 就绪:可作为 Agent Skill 安装,让 Claude Code、Cursor 等 AI 助手直接读取文档。

📦 安装方式

1. 作为 CLI 工具(全局使用)

1
2
3
4
5
6
# 使用 npx 直接运行(无需安装)
npx @firecrawl/anydoc report.docx

# 或全局安装为永久命令
npm install -g @firecrawl/anydoc
anydoc slides.pptx -o slides.md

2. 在 Node.js 项目中安装

1
npm install @firecrawl/anydoc

3. 在 Python 项目中安装

1
pip install firecrawl-anydoc

4. 在浏览器中使用 (WebAssembly)

1
npm install @firecrawl/anydoc-wasm

5. 作为 Agent Skill(为 AI 助手安装)

1
npx skills add firecrawl/anydoc

安装后,AI 助手(如 Claude Code)可通过 anydoc CLI 读取你指定的任何文档。

🚀 使用示例

CLI 基本用法

1
2
3
4
5
6
7
8
9
10
11
# 转换文档并输出到终端
npx @firecrawl/anydoc report.docx

# 指定输出文件
npx @firecrawl/anydoc slides.pptx -o slides.md

# 从标准输入读取 CSV 数据
cat data.csv | npx @firecrawl/anydoc - --format csv

# 查看所有选项
npx @firecrawl/anydoc --help

Python 使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
import anydoc

# 从文件路径转换
markdown = anydoc.to_markdown("report.docx")
print(markdown)

# 从字节数据转换(自动检测格式)
with open("presentation.pptx", "rb") as f:
data = f.read()
markdown = anydoc.to_markdown_bytes(data)

# 对于无特征格式(如 CSV),需显式指定
csv_markdown = anydoc.to_markdown_bytes(data, "csv")

Node.js 使用示例

1
2
3
4
5
6
7
8
import { toMarkdown, toMarkdownBytes } from '@firecrawl/anydoc';

// 从文件路径
const markdown = await toMarkdown('report.docx');

// 从字节数据
const bytes = fs.readFileSync('presentation.pptx');
const fromBytes = await toMarkdownBytes(bytes);

浏览器 (WebAssembly) 使用示例

1
2
3
4
5
6
7
8
9
import init, { toMarkdownBytes } from '@firecrawl/anydoc-wasm';

await init();

// 从 File 对象或 ArrayBuffer 转换
const fileInput = document.getElementById('file');
const file = fileInput.files[0];
const bytes = new Uint8Array(await file.arrayBuffer());
const markdown = toMarkdownBytes(bytes, file.name); // 传入文件名辅助检测

⚙️ 高级功能与配置

  • 获取完整文档模型:除了直接获得 Markdown,你还可以获取结构化的文档模型(Document),其中包含所有文本、元数据和嵌入的图片/资产(以字节形式)

    1
    2
    3
    4
    # Python 示例
    doc = anydoc.to_document(data)
    for asset in doc.assets:
    print(f"图片: {asset.media_type}, 大小: {len(asset.bytes)} 字节")
  • 错误处理:转换失败时会返回明确的错误类型,便于处理异常情况。

    • Python:抛出 anydoc.ConvertError 子类异常(如 Encrypted, Malformed, Unsupported)。
    • Node.js:错误对象的 error.code 属性包含错误名称。
  • 格式检测函数:你可以单独使用格式检测功能:

    1
    2
    3
    4
    from anydoc import format_from_bytes, format_from_extension

    detected = format_from_bytes(data) # 返回枚举,如 'Docx'
    from_ext = format_from_extension("myfile.odt") # 返回 'Odt'

📊 性能与质量

根据项目文档中的 benchmark(基准测试),anydoc 在速度和转换质量上均优于常见的替代方案(如 LibreOffice、pandoc、unstructured):

  • 速度:中位转换时间 4.4 毫秒,比最快的竞争者(pandoc,102ms)快 23 倍
  • 质量:在 14 种格式 的转换任务中,anydoc 在完整性、结构保留、格式保真度和输出整洁度方面均获得最高评分(81/100),远超其他工具。

❓ 常见问题与注意事项

  • PDF 支持:anydoc 支持基于文本的 PDF 转换(使用 pdf-inspector),不支持扫描版或纯图片 PDF(会返回 Unsupported 错误)。对于扫描件,建议配合 OCR 工具(如 Firecrawl Parse 的 OCR 模型)使用。
  • 加密文档:遇到加密或需要密码的文档,会返回 Encrypted 错误,目前无法自动处理。
  • 大型文档:针对可能存在的恶意或超大文档,anydoc 设置了资源限制(如解压层数、节点数量),超限时会返回 ResourceLimit 错误。
  • CSV 识别:CSV 文件无固定文件头,因此必须通过文件名(.csv 扩展名)或 API 参数显式指定格式。

总结

anydoc 是一个为速度和一致性而生的现代文档转换工具。它的部署极其简单,通过 npm 或 pip 一行命令即可安装。其核心价值在于将复杂的办公文档快速、干净地转换为 LLM 友好的 Markdown,非常适合用于 RAG 系统的数据预处理、AI 代理的文件读取管道或文档自动化处理流程。对于开发者和 AI 应用构建者,推荐根据你的技术栈选择 Node.js 或 Python 绑定 进行集成。如果你需要处理扫描版 PDF 或进行大规模云端转换,可以关注其背后的 Firecrawl 托管服务。