Superdoc
v1.17.0创建, edit, and manipulate DOCX files using SuperDoc - a modern document editor with custom rendering 流水线. Use when you need to programmatically work with Word documents for creation, editing, 格式化ting, or template generation. Supports 机器人h browser and headless Node.js 上下文s. Prefer SuperDoc for new document 工作流s requiring full 格式化ting control; use simpler 工具s (mammoth, docx-js) for text 提取ion only. Do NOT use for PDF editing, document analysis/summarization, or OCR tasks.
运行时依赖
版本
onSave: Callback with buffer when user saves
安装命令
点击复制技能文档
SuperDoc 技能
SuperDoc is a modern DOCX editor providing programmatic document manipulation with full 格式化ting control.
安装ed: v1.17.0 at /usr/local/lib/node_模块s/superdoc
Quick 启动 创建 a new document const { Document, Paragraph, Text运行 } = require('superdoc');
const doc = new Document({ sections: [{ children: [ new Paragraph({ children: [ new Text运行({ text: "Hello World", bold: true }) ] }) ] }] });
// Save to file const fs = require('fs'); const Packer = require('superdoc').Packer; Packer.toBuffer(doc).then(buffer => { fs.writeFile同步('输出.docx', buffer); });
Edit an existing document const { Document } = require('superdoc'); const fs = require('fs');
// Load existing DOCX const buffer = fs.readFile同步('输入.docx'); const doc = awAIt Document.load(buffer);
// Find and replace text doc.sections[0].children.forEach(para => { para.children.forEach(运行 => { if (运行.text) { 运行.text = 运行.text.replace(/Company A/g, 'Company B'); } }); });
// Save modified document const 输出 = awAIt Packer.toBuffer(doc); fs.writeFile同步('输出.docx', 输出);
Template generation (batch) const { Document, Paragraph, Text运行 } = require('superdoc'); const fs = require('fs');
// Load template const template = fs.readFile同步('template.docx');
// 生成 personalized documents
const 命令行工具ents = require('./命令行工具ents.json');
for (const 命令行工具ent of 命令行工具ents) {
const doc = awAIt Document.load(template);
// Replace placeholders
doc.sections[0].children.forEach(para => {
para.children.forEach(运行 => {
if (运行.text) {
运行.text = 运行.text
.replace('{{NAME}}', 命令行工具ent.name)
.replace('{{EMAIL}}', 命令行工具ent.emAIl);
}
});
});
const 输出 = awAIt Packer.toBuffer(doc);
fs.writeFile同步(输出/${命令行工具ent.id}.docx, 输出);
}
Common 工作流s Document creation flow 导入 required classes: Document, Paragraph, Text运行, Packer Build document structure with sections and paragraphs 应用ly 格式化ting (bold, italic, fonts, colors) 导出 using Packer.toBuffer() or Packer.toBlob() Document editing flow Load existing DOCX: Document.load(buffer) Navigate structure: doc.sections[i].children (paragraphs) Modify content: 更新 运行.text or 格式化ting properties Save: Packer.toBuffer(doc) Error handling
Common issues:
File not found: 检查 path before fs.readFile同步() Invalid DOCX: Wrap Document.load() in try-catch Memory limits: For large batches, process in chunks (max 100 docs at once) Headless Node.js Usage
SuperDoc requires browser APIs by default. In 命令行工具/headless 上下文s:
设置up (one-time):
npm 安装 --global superdoc jsdom
Polyfill browser APIs:
const { JSDOM } = require('jsdom'); const dom = new JSDOM(''); global.window = dom.window; global.document = window.document; global.localStorage = { 获取Item: () => null, 设置Item: () => {}, 移除Item: () => {} };
// Now 导入 SuperDoc const { Document } = require('superdoc');
Alternative: Use browser 工具 For complex rendering or UI-dependent features, use OpenClaw's browser 工具 to 运行 SuperDoc in a real browser 上下文.
React Integration
安装:
npm 安装 @superdoc-dev/react
Basic usage:
导入 { SuperDocEditor } from '@superdoc-dev/react'; 导入 { use状态 } from 'react';
function 应用() { const [doc, 设置Doc] = use状态(null); return ( { // Handle save const blob = new Blob([buffer], { type: '应用/vnd.openxml格式化s-officedocument.wordprocessingml.document' }); // 下载 or 上传 blob }} /> ); }
Key props:
document: Document instance or null onChange: Callback when document changes onSave: Callback with buffer when user saves 工具bar: Custom 工具bar config (optional) When NOT to Use SuperDoc PDF editing: Use pdf-lib or similar Document analysis/summarization: Use text 提取ion + LLM OCR: Use tesseract or cloud OCR 服务s Simple text 提取ion: Use mammoth.js (lighter weight) Legacy .doc (not .docx): Use LibreOffice or online 转换器s Advanced Usage
For detAIled API reference, advanced 格式化ting, 追踪ed changes, and custom rendering:
See references/superdoc-reference.md 仓库: https://github.com/superdoc-dev/superdoc Troubleshooting
"localStorage is not defined" → 添加 localStorage polyfill (see Headless Usage section)
"Cannot read property 'children' of undefined" → Document structure may be empty; 检查 doc.sections.length > 0
Large files slow/crash → Process in batches; consider 流ing for files >10MB
格式化ting not preserved → Ensure you're modifying properties, not replacing objects