@ fastgpt-sdk/ anydoc (英语)
将Word,PowerPoint,Excel,OpenDocument,RTF,EPUB,CSV等PDF文件转换为干净的GitHub-Flavored Markdown. 节点.js 绑定 任何文件 破碎的箱子,建造于 纵火。也可以通过下列方式作为托管的API提供: Firecrawl 解析,它增加了我们的OCR模型 扫描页 任何doc不能自己阅读.
每个格式都分为一个共享的文档模型,并通过一个单一的Markdown序列器来制作,所以标题,表格,列表和脚注无论采用哪种格式,都会出现相同的出处. 转换在 libuv 线程池上运行, 并且从不屏蔽事件循环 。 TypeScript 有包件的船型.
npm install @fastgpt-sdk/anydoc
支持的格式
| 格式 | 扩展 |
|---|---|
| 单词 | .doc, (中文(简体) ), .docx, (中文(简体) ), .docm |
| 电源点 | .ppt, (中文(简体) ), .pps, (中文(简体) ), .pot, (中文(简体) ), .pptx, (中文(简体) ), .pptm, (中文(简体) ), .ppsx, (中文(简体) ), .ppsm |
| 埃克塞尔 | .xls, (中文(简体) ), .xlsx, (中文(简体) ), .xlsm, (中文(简体) ), .xlsb |
| 打开文档 | .odt, (中文(简体) ), .ods, (中文(简体) ), .odp |
| 丰富的文本格式 | .rtf |
| 联布办事处 | .epub |
| 简历 | .csv |
| 专题报告 | .pdf |
国 际
包裹载着一个 anydoc 命令,所以 npx 转换未安装的文档 :
npx @fastgpt-sdk/anydoc report.docx # Markdown to stdout
npx @fastgpt-sdk/anydoc slides.pptx -o slides.md # or to a file
npx @fastgpt-sdk/anydoc - --format csv < data.csv # read stdin
npx @fastgpt-sdk/anydoc scan.pdf --ocr hosted # scanned pages via Firecrawl Parse
Markdown去stdout, 错误stderr,和 anydoc --help 盖取其余.
使用量
import { toDocument, toMarkdown, toMarkdownBytes } from '@fastgpt-sdk/anydoc';
// From a file path:
const markdown = await toMarkdown('report.docx');
// From bytes, with the format detected from the content:
const fromBytes = await toMarkdownBytes(bytes);
// Or name it, which signature-less formats (CSV) need:
const fromCsv = await toMarkdownBytes(bytes, 'csv');
// Preserve embedded images and their positions in the Markdown:
const { markdown, assets } = await toMarkdownBytes(bytes, 'doc', {
embeddedImageMode: 'reference',
maxImageBytes: 32 * 1024 * 1024,
maxImageTotalBytes: 128 * 1024 * 1024,
});
// markdown contains ; assets contains { id: 0, mediaType, data, ... }.
// Or stop at the document model, which also carries embedded assets:
const document = await toDocument(bytes);
扫描页面
anydoc 本地转换而不进行 OCR,所以带有扫描或仅图像页面的 PDF 拒绝使用 needsOcr选择 ocr: 'hosted' 将该文件发送给 Firecrawl 解析。不需要注册。 设定 apiKey 或者说 FIRECRAWL_API_KEY 为了更高的限度.
const markdown = await toMarkdown('scan.pdf', { ocr: 'hosted' });
关于CLI, anydoc scan.pdf --ocr hosted。 。 。 .
错误
转换只有在没有完整的Markdown出文件时才会拒绝. 拒绝是 Error 谁 code 出错的地方:
try {
return await toMarkdown(path);
} catch (error) {
// No document comes out of these, so record the file and take the next one.
if (error.code === 'encrypted' || error.code === 'unsupported') {
unconverted.push({ path, reason: error.code });
return null;
}
throw error;
}
code | 含义 |
|---|---|
unsupported | 未知格式, 或无法转换的格式 |
needsOcr | 扫描或仅图像页 PDF,列表于 pages |
malformed | 结构上无法使用:无法提取有意义的内容 |
encrypted | 加密或密码保护 |
resourceLimit | 跨越了固定的安全限制(减压、筑巢、节点计数) |
missingPart | 缺少任何有意义的产出所需的部分 |
io | 无法读取文件, 从 toMarkdown 仅限 |
hosted | ocr: 'hosted' 无法通过 Firecrawl 解析获取文档 |
error.message 带有细节,将软件包部分命名为格式识别错误部分。 TypeScript 将联盟变为 ConvertErrorCode。 。 。 .
格式检测
该格式从文件内容读取,使用标记其规格指定:PDF头,RTF开放组,OLE流名,ZIP包mime类型和内容类型. CSV 没有这样的标记, 所以检测返回 null 对于它和扩展,或一种明确的格式,代之以命名.
formatFromBytes(bytes); // 'docx', or null when nothing matches
formatFromExtension('.pptm'); // 'pptx'
formatFromPath('report.odt'); // 'odt'
图像和嵌入对象
默认情况下,一个嵌入式图像在字节保持时会作为其备选案文文本 document.assets。通过 embeddedImageMode: 'reference' 改为 toMarkdown 或者说 toMarkdownBytes 待收款项 { markdown, assets }: Markdown 包含 asset:<id> 图像 URL 和 assets 包含相应的 image/* 字节. maxImageBytes 财务报告和审定财务报表 maxImageTotalBytes 将一个图像和所有图像分别封顶; 默认为128 MiB 并用 resourceLimit 当跨越。 非图像嵌入对象保持默认的备选案文行为. 带有外部 URL 的图像总是作为普通的 Markdown 图像来渲染 .
完整的行为说明和基准存在于 仓库读取器。 。 。 .