跳到主要内容

腾讯文档

收藏0

腾讯文档(docs.qq.com)在线云文档操作:创建与编辑文档/表格/幻灯片/思维导图/流程图/智能表格/收集表,管理知识库空间与文件夹,读取搜索内容,文件导入导出与网页剪藏。灵栈搬运自 SkillHub tencent-docs v1.0.42。

让 AI 安装

Minsaroun最新版本 0.1.0更新于 2026-11-14下载 0 次

ocr.js

6.2 KB
1#!/usr/bin/env node
2/**
3 * 腾讯文档 MCP 本地图片 OCR 辅助脚本(跨平台)
4 *
5 * 功能:
6 * 自动将本地图片 base64 编码后调用 ocr.* 工具,支持三种操作:
7 * - extract: 识别图片中的文字
8 * - toword: 将图片转为 Word 文档
9 * - toexcel: 将图片转为 Excel 文档
10 *
11 * 用法:
12 * node ocr.js extract <image> [--accurate|--efficient] [--positions]
13 * node ocr.js toword <image> [<image2> ...] [--title "标题"]
14 * node ocr.js toexcel <image> [<image2> ...] [--title "标题"]
15 *
16 * 依赖:
17 * - Node.js (>= 14)
18 * - mcporter(已配置 tencent-docs 服务)
19 */
20
21"use strict";
22
23const { execFileSync } = require("child_process");
24const fs = require("fs");
25const path = require("path");
26
27// ── 常量 ──────────────────────────────────────────────────────────────────
28const MCP_SERVICE = "tencent-docs";
29const SUPPORTED_EXTS = new Set(["png", "jpg", "jpeg", "bmp", "webp"]);
30const MAX_SINGLE_SIZE = 10 * 1024 * 1024;
31const MAX_TOTAL_SIZE = 50 * 1024 * 1024;
32const MAX_IMAGE_COUNT = 9;
33
34// ── 工具函数 ──────────────────────────────────────────────────────────────
35
36function die(msg) {
37 console.error("ERROR: " + msg);
38 process.exit(1);
39}
40
41function validateImage(filePath) {
42 if (!fs.existsSync(filePath)) die("文件不存在: " + filePath);
43
44 var stat = fs.statSync(filePath);
45 if (!stat.isFile()) die("不是文件: " + filePath);
46
47 var ext = path.extname(filePath).slice(1).toLowerCase();
48 if (!SUPPORTED_EXTS.has(ext)) {
49 die("不支持的格式 ." + ext + ",支持: " + Array.from(SUPPORTED_EXTS).join(", "));
50 }
51 if (stat.size === 0) die("文件为空: " + filePath);
52 if (stat.size > MAX_SINGLE_SIZE) die("文件超过 10MB: " + filePath);
53
54 return stat.size;
55}
56
57function encodeBase64(filePath) {
58 return fs.readFileSync(filePath).toString("base64");
59}
60
61// ── mcporter 调用封装 ────────────────────────────────────────────────────
62// 使用 execFileSync 直接传参数数组,绕过 shell,兼容 Windows 且无命令行长度限制
63function mcpCall(tool, argsObj) {
64 var argsJson = JSON.stringify(argsObj);
65 try {
66 var stdout = execFileSync(
67 "mcporter",
68 ["call", MCP_SERVICE, tool, "--args", argsJson],
69 { encoding: "utf-8", timeout: 120000 }
70 );
71 return stdout.trim();
72 } catch (err) {
73 var msg = err.stderr || err.stdout || err.message || "unknown error";
74 throw new Error("mcporter call " + tool + " failed: " + msg);
75 }
76}
77
78// ── 参数解析 ──────────────────────────────────────────────────────────────
79
80function parseArgs() {
81 var args = process.argv.slice(2);
82
83 if (args.length === 0) {
84 console.log("用法:");
85 console.log(" node ocr.js extract <image> [--accurate|--efficient] [--positions]");
86 console.log(' node ocr.js toword <image> [<image2> ...] [--title "标题"]');
87 console.log(' node ocr.js toexcel <image> [<image2> ...] [--title "标题"]');
88 process.exit(1);
89 }
90
91 var action = args[0];
92 if (["extract", "toword", "toexcel"].indexOf(action) === -1) {
93 die("未知操作 '" + action + "',支持: extract, toword, toexcel");
94 }
95
96 return { action: action, rest: args.slice(1) };
97}
98
99// ── extract ──────────────────────────────────────────────────────────────
100
101function handleExtract(args) {
102 var image = "";
103 var extractType = "basic";
104 var withPositions = false;
105
106 for (var i = 0; i < args.length; i++) {
107 switch (args[i]) {
108 case "--accurate": extractType = "accurate"; break;
109 case "--efficient": extractType = "efficient"; break;
110 case "--positions": withPositions = true; break;
111 default:
112 if (args[i].charAt(0) === "-") die("未知选项 '" + args[i] + "'");
113 if (image) die("extract 只支持单张图片");
114 image = args[i];
115 break;
116 }
117 }
118 if (!image) die("未指定图片路径");
119 validateImage(image);
120
121 console.log("⏳ 正在识别 " + path.basename(image) + " ...");
122
123 var result = mcpCall("ocr.extract", {
124 image_base64: encodeBase64(image),
125 extract_type: extractType,
126 with_positions: withPositions,
127 });
128 console.log(result);
129}
130
131// ── toword / toexcel ─────────────────────────────────────────────────────
132
133function handleConvert(action, args) {
134 var images = [];
135 var title = "";
136
137 for (var i = 0; i < args.length; i++) {
138 if (args[i] === "--title") {
139 if (i + 1 >= args.length) die("--title 需要值");
140 title = args[++i];
141 } else if (args[i].charAt(0) === "-") {
142 die("未知选项 '" + args[i] + "'");
143 } else {
144 images.push(args[i]);
145 }
146 }
147 if (images.length === 0) die("未指定图片路径");
148 if (images.length > MAX_IMAGE_COUNT) die("图片数量超过 " + MAX_IMAGE_COUNT + " 张限制");
149
150 var totalSize = 0;
151 for (var j = 0; j < images.length; j++) {
152 totalSize += validateImage(images[j]);
153 }
154 if (totalSize > MAX_TOTAL_SIZE) die("图片总大小超过 50MB");
155
156 console.log("⏳ 正在处理 " + images.length + " 张图片 ...");
157
158 var callArgs = {
159 images: images.map(function (img) { return { image_base64: encodeBase64(img) }; }),
160 };
161 if (title) callArgs.title = title;
162
163 var result = mcpCall("ocr." + action, callArgs);
164 console.log(result);
165}
166
167// ── 主流程 ────────────────────────────────────────────────────────────────
168
169function main() {
170 var parsed = parseArgs();
171 if (parsed.action === "extract") {
172 handleExtract(parsed.rest);
173 } else {
174 handleConvert(parsed.action, parsed.rest);
175 }
176}
177
178main();

文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。