Word Document Creator by jmsktm/claude-settings
npx skills add https://github.com/jmsktm/claude-settings --skill 'Word Document Creator'Word 文档生成器技能提供了通过编程方式生成 Microsoft Word (.docx) 文档的全面功能。它使用 Node.js 的 docx 库来处理格式、样式、表格、图像、页眉、页脚和复杂布局。该技能对于自动化文档生成、报告创建和基于模板的工作流程至关重要。
从简单的信件到包含表格、图表、图像和自定义样式的复杂报告,皆可创建。该技能支持从零开始创建和基于模板的变量替换生成。
目的: 构建包含标题、段落、列表和格式的 Word 文档
步骤:
docx 库并创建 Document 实例实现:
const { Document, Packer, Paragraph, TextRun, HeadingLevel, AlignmentType } = require('docx');
const fs = require('fs');
async function createDocument(outputPath) {
const doc = new Document({
sections: [{
properties: {},
headers: {
default: new Header({
children: [new Paragraph({ text: "Company Name", alignment: AlignmentType.RIGHT })]
})
},
children: [
new Paragraph({
text: "Business Proposal",
heading: HeadingLevel.HEADING_1,
alignment: AlignmentType.CENTER
}),
new Paragraph({
children: [
new TextRun({ text: "Prepared for: ", bold: true }),
new TextRun("Client Name")
]
}),
new Paragraph({
text: "Executive Summary",
heading: HeadingLevel.HEADING_2
}),
new Paragraph({
text: "This proposal outlines the scope, timeline, and budget for the project...",
alignment: AlignmentType.JUSTIFIED
})
]
}]
});
const buffer = await Packer.toBuffer(doc);
fs.writeFileSync(outputPath, buffer);
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
目的: 使用 Word 模板文件,并将占位符替换为实际数据
步骤:
pizzip 和 docxtemplater 加载模板 .docx 文件实现:
const Docxtemplater = require('docxtemplater');
const PizZip = require('pizzip');
const fs = require('fs');
function generateFromTemplate(templatePath, data, outputPath) {
const content = fs.readFileSync(templatePath, 'binary');
const zip = new PizZip(content);
const doc = new Docxtemplater(zip, {
paragraphLoop: true,
linebreaks: true
});
// 包含占位符值的数据对象
doc.setData({
clientName: data.clientName,
projectName: data.projectName,
date: new Date().toLocaleDateString(),
items: data.items, // 用于循环的数组
total: data.total
});
doc.render();
const buf = doc.getZip().generate({
type: 'nodebuffer',
compression: 'DEFLATE'
});
fs.writeFileSync(outputPath, buf);
}
// 模板占位符:{clientName}, {projectName} 等
// 循环语法:{#items}{name} - {price}{/items}
目的: 构建包含数据表格和嵌入图像的复杂文档
步骤:
实现:
const { Document, Packer, Paragraph, Table, TableRow, TableCell, Media } = require('docx');
const fs = require('fs');
async function createTableDocument(data, imagePath, outputPath) {
const image = Media.addImage(doc, fs.readFileSync(imagePath), 300, 200);
const doc = new Document({
sections: [{
children: [
new Paragraph({ text: "Sales Report Q4 2025", heading: HeadingLevel.HEADING_1 }),
new Table({
rows: [
// 表头行
new TableRow({
children: [
new TableCell({ children: [new Paragraph({ text: "Month", bold: true })] }),
new TableCell({ children: [new Paragraph({ text: "Revenue", bold: true })] }),
new TableCell({ children: [new Paragraph({ text: "Growth", bold: true })] })
]
}),
// 数据行
...data.map(row => new TableRow({
children: [
new TableCell({ children: [new Paragraph(row.month)] }),
new TableCell({ children: [new Paragraph(row.revenue)] }),
new TableCell({ children: [new Paragraph(row.growth)] })
]
}))
]
}),
new Paragraph({ text: "" }), // 间距
new Paragraph({ text: "Revenue Trend Chart", heading: HeadingLevel.HEADING_2 }),
new Paragraph({ children: [image] }),
new Paragraph({ text: "Figure 1: Quarterly Revenue Trend", italics: true })
]
}]
});
const buffer = await Packer.toBuffer(doc);
fs.writeFileSync(outputPath, buffer);
}
目的: 从数据源创建多个个性化文档
步骤:
目的: 应用样式、字体、颜色、间距和页面布局
步骤:
实现:
const doc = new Document({
styles: {
paragraphStyles: [
{
id: "CustomHeading",
name: "Custom Heading",
basedOn: "Heading1",
next: "Normal",
run: {
size: 32,
bold: true,
color: "2E74B5",
font: "Calibri"
},
paragraph: {
spacing: { before: 240, after: 120 }
}
}
]
},
sections: [{
properties: {
page: {
margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 } // 1440 = 1 英寸
}
},
children: [
new Paragraph({ text: "Styled Heading", style: "CustomHeading" })
]
}]
});
| 操作 | 命令/触发器 |
|---|---|
| 创建新文档 | "create word document [name]" |
| 基于模板 | "generate docx from template [file]" |
| 添加表格 | "add table with [rows] rows to [doc]" |
| 插入图像 | "insert [image] into word doc" |
| 批量生成 | "create word docs for each [data]" |
| 应用样式 | "format word doc with [style]" |
| 添加页眉/页脚 | "add header [text] to document" |
| 创建目录 | "generate table of contents" |
会议纪要:
const doc = new Document({
sections: [{
children: [
new Paragraph({ text: "Meeting Minutes", heading: HeadingLevel.HEADING_1 }),
new Paragraph({ text: `Date: ${date}` }),
new Paragraph({ text: `Attendees: ${attendees.join(', ')}` }),
new Paragraph({ text: "Agenda", heading: HeadingLevel.HEADING_2 }),
// ... 议程项目
new Paragraph({ text: "Action Items", heading: HeadingLevel.HEADING_2 }),
// ... 行动项表格
]
}]
});
邮件合并:
// 模板:Dear {firstName} {lastName}, ...
contacts.forEach(contact => {
generateFromTemplate('letter-template.docx', contact, `letter-${contact.id}.docx`);
});
安装所需包:
npm install docx docxtemplater pizzip
高级功能可选:
npm install docx-templates # 替代模板方案
npm install officegen # 旧版支持
目录:
new TableOfContents("Table of Contents", {
hyperlink: true,
headingStyleRange: "1-3"
});
条件部分:
// 在模板中:{#showSection}Content{/showSection}
doc.setData({ showSection: condition ? {...} : false });
自定义编号:
new Paragraph({
text: "Numbered item",
numbering: {
reference: "custom-numbering",
level: 0
}
});
每周安装次数
–
代码仓库
GitHub 星标数
2
首次出现时间
–
安全审计
The Word Document Creator skill provides comprehensive capabilities for generating Microsoft Word (.docx) documents programmatically. It handles formatting, styles, tables, images, headers, footers, and complex layouts using the docx library for Node.js. This skill is essential for automated document generation, report creation, and template-based workflows.
Create everything from simple letters to complex reports with tables, charts, images, and custom styling. The skill supports both creation from scratch and template-based generation with variable replacement.
Purpose: Build a Word document with headings, paragraphs, lists, and formatting
Steps:
docx library and create Document instanceImplementation:
const { Document, Packer, Paragraph, TextRun, HeadingLevel, AlignmentType } = require('docx');
const fs = require('fs');
async function createDocument(outputPath) {
const doc = new Document({
sections: [{
properties: {},
headers: {
default: new Header({
children: [new Paragraph({ text: "Company Name", alignment: AlignmentType.RIGHT })]
})
},
children: [
new Paragraph({
text: "Business Proposal",
heading: HeadingLevel.HEADING_1,
alignment: AlignmentType.CENTER
}),
new Paragraph({
children: [
new TextRun({ text: "Prepared for: ", bold: true }),
new TextRun("Client Name")
]
}),
new Paragraph({
text: "Executive Summary",
heading: HeadingLevel.HEADING_2
}),
new Paragraph({
text: "This proposal outlines the scope, timeline, and budget for the project...",
alignment: AlignmentType.JUSTIFIED
})
]
}]
});
const buffer = await Packer.toBuffer(doc);
fs.writeFileSync(outputPath, buffer);
}
Purpose: Use a template Word file and replace placeholders with actual data
Steps:
pizzip and docxtemplaterImplementation:
const Docxtemplater = require('docxtemplater');
const PizZip = require('pizzip');
const fs = require('fs');
function generateFromTemplate(templatePath, data, outputPath) {
const content = fs.readFileSync(templatePath, 'binary');
const zip = new PizZip(content);
const doc = new Docxtemplater(zip, {
paragraphLoop: true,
linebreaks: true
});
// Data object with placeholder values
doc.setData({
clientName: data.clientName,
projectName: data.projectName,
date: new Date().toLocaleDateString(),
items: data.items, // Array for looping
total: data.total
});
doc.render();
const buf = doc.getZip().generate({
type: 'nodebuffer',
compression: 'DEFLATE'
});
fs.writeFileSync(outputPath, buf);
}
// Template placeholders: {clientName}, {projectName}, etc.
// Loop syntax: {#items}{name} - {price}{/items}
Purpose: Build complex documents containing data tables and embedded images
Steps:
Implementation:
const { Document, Packer, Paragraph, Table, TableRow, TableCell, Media } = require('docx');
const fs = require('fs');
async function createTableDocument(data, imagePath, outputPath) {
const image = Media.addImage(doc, fs.readFileSync(imagePath), 300, 200);
const doc = new Document({
sections: [{
children: [
new Paragraph({ text: "Sales Report Q4 2025", heading: HeadingLevel.HEADING_1 }),
new Table({
rows: [
// Header row
new TableRow({
children: [
new TableCell({ children: [new Paragraph({ text: "Month", bold: true })] }),
new TableCell({ children: [new Paragraph({ text: "Revenue", bold: true })] }),
new TableCell({ children: [new Paragraph({ text: "Growth", bold: true })] })
]
}),
// Data rows
...data.map(row => new TableRow({
children: [
new TableCell({ children: [new Paragraph(row.month)] }),
new TableCell({ children: [new Paragraph(row.revenue)] }),
new TableCell({ children: [new Paragraph(row.growth)] })
]
}))
]
}),
new Paragraph({ text: "" }), // Spacing
new Paragraph({ text: "Revenue Trend Chart", heading: HeadingLevel.HEADING_2 }),
new Paragraph({ children: [image] }),
new Paragraph({ text: "Figure 1: Quarterly Revenue Trend", italics: true })
]
}]
});
const buffer = await Packer.toBuffer(doc);
fs.writeFileSync(outputPath, buffer);
}
Purpose: Create multiple personalized documents from a data source
Steps:
Purpose: Apply styles, fonts, colors, spacing, and page layout
Steps:
Implementation:
const doc = new Document({
styles: {
paragraphStyles: [
{
id: "CustomHeading",
name: "Custom Heading",
basedOn: "Heading1",
next: "Normal",
run: {
size: 32,
bold: true,
color: "2E74B5",
font: "Calibri"
},
paragraph: {
spacing: { before: 240, after: 120 }
}
}
]
},
sections: [{
properties: {
page: {
margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 } // 1440 = 1 inch
}
},
children: [
new Paragraph({ text: "Styled Heading", style: "CustomHeading" })
]
}]
});
| Action | Command/Trigger |
|---|---|
| Create new document | "create word document [name]" |
| From template | "generate docx from template [file]" |
| Add table | "add table with [rows] rows to [doc]" |
| Insert image | "insert [image] into word doc" |
| Batch generate | "create word docs for each [data]" |
| Apply style | "format word doc with [style]" |
| Add header/footer | "add header [text] to document" |
| Create TOC | "generate table of contents" |
Meeting Minutes:
const doc = new Document({
sections: [{
children: [
new Paragraph({ text: "Meeting Minutes", heading: HeadingLevel.HEADING_1 }),
new Paragraph({ text: `Date: ${date}` }),
new Paragraph({ text: `Attendees: ${attendees.join(', ')}` }),
new Paragraph({ text: "Agenda", heading: HeadingLevel.HEADING_2 }),
// ... agenda items
new Paragraph({ text: "Action Items", heading: HeadingLevel.HEADING_2 }),
// ... action items table
]
}]
});
Mail Merge:
// Template: Dear {firstName} {lastName}, ...
contacts.forEach(contact => {
generateFromTemplate('letter-template.docx', contact, `letter-${contact.id}.docx`);
});
Install required packages:
npm install docx docxtemplater pizzip
Optional for advanced features:
npm install docx-templates # Alternative templating
npm install officegen # Legacy support
Table of Contents:
new TableOfContents("Table of Contents", {
hyperlink: true,
headingStyleRange: "1-3"
});
Conditional Sections:
// In template: {#showSection}Content{/showSection}
doc.setData({ showSection: condition ? {...} : false });
Custom Numbering:
new Paragraph({
text: "Numbered item",
numbering: {
reference: "custom-numbering",
level: 0
}
});
Weekly Installs
–
Repository
GitHub Stars
2
First Seen
–
Security Audits
Skills CLI 使用指南:AI Agent 技能包管理器安装与管理教程
29,800 周安装