doc-extract

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

doc-extract

文档文本提取

Shared document-to-text extraction. One script, no state: reads an input file, prints JSON to stdout, writes nothing to disk (PHI-safe — no caches, no temp files; callers own any caching).
共享式文档转文本提取工具。单脚本、无状态:读取输入文件,将JSON输出到标准输出,不向磁盘写入任何内容(符合PHI安全要求——无缓存、无临时文件;缓存由调用方自行管理)。

Setup (once)

初始化(仅需一次)

bash
cd <this skill dir> && bun install
This pulls liteparse (the
lit
bin, used for PDF/DOCX/XLSX/PPTX, OCR included) and rtf-to-text (RTF). Without it, PDFs still work via a
pdftotext -layout
fallback if poppler is installed; other binary formats require liteparse.
bash
cd <this skill dir> && bun install
该命令会拉取liteparse
lit
可执行文件,支持PDF/DOCX/XLSX/PPTX格式,内置OCR功能)和rtf-to-text(用于RTF格式)。如果未安装这些依赖,若系统已安装poppler,PDF仍可通过
pdftotext -layout
作为备选方案处理;其他二进制格式则必须依赖liteparse。

Use

使用方法

bash
bun <this skill dir>/scripts/extract.ts <input-file> [--content-type <mime>]
Output on stdout:
json
{ "text": "...", "method": "liteparse | pdftotext | rtf-to-text | passthrough", "pages": 12 }
  • text
    is page-anchored for paged formats:
    === [page N] ===
    markers between pages.
  • pages
    is present when page markers exist.
  • method
    is the extractor that actually produced the text.
  • Format is taken from the file extension; pass
    --content-type
    (e.g.
    application/pdf
    ) when the file has no useful extension, as with downloaded EHR attachments. Note liteparse refuses extension-less files, so those PDFs go through the
    pdftotext
    fallback.
  • Errors print
    {"error": "..."}
    to stderr and exit 1.
bash
bun <this skill dir>/scripts/extract.ts <input-file> [--content-type <mime>]
标准输出内容:
json
{ "text": "...", "method": "liteparse | pdftotext | rtf-to-text | passthrough", "pages": 12 }
  • 对于分页格式,
    text
    包含页面锚点:页面之间会有
    === [page N] ===
    标记。
  • 当存在页面标记时,会输出
    pages
    字段。
  • method
    字段表示实际生成文本所使用的提取器。
  • 格式由文件扩展名识别;当文件无有效扩展名时(如下载的EHR附件),需传入
    --content-type
    参数(例如
    application/pdf
    )。注意liteparse不处理无扩展名的文件,此类PDF会通过
    pdftotext
    备选方案处理。
  • 错误信息会以
    {"error": "..."}
    格式输出到标准错误流,并以状态码1退出。

Table caveat

表格提取注意事项

Tables with multiple value columns (option A vs option B, in-tier vs out-of-tier) can interleave columns line-by-line in the extracted text: fragments of adjacent cells alternate, and a cell's text can even land mid-sentence inside a neighboring column. Values usually survive, but which column a value belongs to can become ambiguous. When an answer comes from one column of a multi-column table and the document has no redundant restatement of the value elsewhere, verify it by reading the original page directly before treating it as ground truth. The extracted text's
=== [page N] ===
anchor tells you which page: pass it to the Read tool's
pages
parameter (e.g. pages: "37") to render just that page to vision instead of the whole document.
包含多值列的表格(如选项A vs 选项B、计划内 vs 计划外)在提取后的文本中可能会逐行交错显示列内容:相邻单元格的文本片段交替出现,甚至某个单元格的文本可能会插入到相邻列的句子中间。数值通常会保留,但数值所属的列可能会变得模糊。当答案来自多列表格中的某一列,且文档其他位置未重复该数值时,在将其视为真实数据前,请直接查看原始页面进行验证。提取文本中的
=== [page N] ===
锚点会告知对应的页码:可将该页码传入Read工具的
pages
参数(例如pages: "37"),仅渲染该页面而非整个文档。

For other skills

供其他技能调用

Import the functions instead of shelling out when you're already in bun TS:
ts
import { extract, resolveLit } from "../doc-extract/scripts/extract";
const lit = resolveLit([myRoot]); // also checks myRoot/node_modules/.bin/lit
const text = extract(lit, "/path/to/file.pdf"); // string | null
The contracts skill consumes it this way (its ingest caching stays on the contracts side).
当您已在bun TS环境中时,可直接导入函数而非通过shell调用:
ts
import { extract, resolveLit } from "../doc-extract/scripts/extract";
const lit = resolveLit([myRoot]); // also checks myRoot/node_modules/.bin/lit
const text = extract(lit, "/path/to/file.pdf"); // string | null
contracts技能就是通过这种方式调用的(其导入缓存由contracts侧自行管理)。