Create, edit, and manipulate DOCX files using SuperDoc - a modern document editor with custom rendering pipeline. Use when you need to programmatically work with Word documents for creation, editing, formatting, or template generation. Supports both browser and headless Node.js contexts. Prefer SuperDoc for new document workflows requiring full formatting control; use simpler tools (mammoth, docx-js) for text extraction only. Do NOT use for PDF editing, document analysis/summarization, or OCR ta
SuperDoc 是一个现代化的 DOCX 编辑器,提供程序化文档操作和完整的格式控制。
已安装: v1.17.0 位于 /usr/local/lib/node_modules/superdoc
javascript
const { Document, Paragraph, TextRun } = require(superdoc);
const doc = new Document({
sections: [{
children: [
new Paragraph({
children: [
new TextRun({ text: Hello World, bold: true })
]
})
]
}]
});
// 保存到文件
const fs = require(fs);
const Packer = require(superdoc).Packer;
Packer.toBuffer(doc).then(buffer => {
fs.writeFileSync(output.docx, buffer);
});
javascript
const { Document } = require(superdoc);
const fs = require(fs);
// 加载现有 DOCX
const buffer = fs.readFileSync(input.docx);
const doc = await Document.load(buffer);
// 查找并替换文本
doc.sections[0].children.forEach(para => {
para.children.forEach(run => {
if (run.text) {
run.text = run.text.replace(/Company A/g, Company B);
}
});
});
// 保存修改后的文档
const output = await Packer.toBuffer(doc);
fs.writeFileSync(output.docx, output);
javascript
const { Document, Paragraph, TextRun } = require(superdoc);
const fs = require(fs);
// 加载模板
const template = fs.readFileSync(template.docx);
// 生成个性化文档
const clients = require(./clients.json);
for (const client of clients) {
const doc = await Document.load(template);
// 替换占位符
doc.sections[0].children.forEach(para => {
para.children.forEach(run => {
if (run.text) {
run.text = run.text
.replace({{NAME}}, client.name)
.replace({{EMAIL}}, client.email);
}
});
});
const output = await Packer.toBuffer(doc);
fs.writeFileSync(output/${client.id}.docx, output);
}
SuperDoc 默认需要浏览器 API。在 CLI/无头环境中:
设置(一次性):
bash
npm install --global superdoc jsdom
填充浏览器 API:
javascript
const { JSDOM } = require(jsdom);
const dom = new JSDOM();
global.window = dom.window;
global.document = window.document;
global.localStorage = {
getItem: () => null,
setItem: () => {},
removeItem: () => {}
};
// 现在导入 SuperDoc
const { Document } = require(superdoc);
替代方案:使用浏览器工具
对于复杂的渲染或依赖 UI 的功能,使用 OpenClaw 的 browser 工具在真实的浏览器环境中运行 SuperDoc。
安装:
bash
npm install @superdoc-dev/react
基本用法:
jsx
import { SuperDocEditor } from @superdoc-dev/react;
import { useState } from react;
function App() {
const [doc, setDoc] = useState(null);
return (
onChange={setDoc}
onSave={(buffer) => {
// 处理保存
const blob = new Blob([buffer], {
type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
});
// 下载或上传 blob
}}
/>
);
}
关键属性:
有关详细的 API 参考、高级格式、修订跟踪和自定义渲染:
localStorage is not defined
→ 添加 localStorage 填充(参见无头使用部分)
Cannot read property children of undefined
→ 文档结构可能为空;检查 doc.sections.length > 0
大文件慢/崩溃
→ 分批处理;对于大于 10MB 的文件考虑使用流式处理
格式未保留
→ 确保修改属性,而不是替换对象
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 superdoc-1776205415 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 superdoc-1776205415 技能
skillhub install superdoc-1776205415
文件大小: 5.35 KB | 发布时间: 2026-4-15 12:43