技能标识:docx
Create, read, edit, and manipulate Word documents (.docx). Supports text, tables, images, headers, footers, styles, and tracked changes.
使用 docx-js 库创建完整的 Microsoft Word 文档,并通过 XML 操作进行编辑。.docx 文件是一个包含 XML 文件的 ZIP 压缩包。
const doc = new Document({
sections: [{
children: [
new Paragraph({
children: [
new TextRun({ text: Hello World!, bold: true, size: 32 })
]
})
]
}]
});
await Packer.toBuffer(doc).then(buffer => {
require(fs).writeFileSync(hello.docx, buffer);
});
console.log(✓ 文档已创建!);
javascript
const { Document, Packer, Section } = require(docx);
const doc = new Document({
sections: [{
properties: {
page: {
size: { width: 12240, height: 15840 }, // 美国信纸(DXA 单位)
margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 } // 1 英寸
}
},
children: [/ 段落、表格等 /]
}]
});
javascript
const { Paragraph, TextRun, PageBreak } = require(docx);
// 基本段落
new Paragraph({
children: [new TextRun(简单文本)]
})
// 格式化段落
new Paragraph({
children: [
new TextRun({
text: 加粗和 ,
bold: true,
size: 28, // 14pt(size 是半磅值)
font: Arial
}),
new TextRun({
text: 斜体,
italics: true,
color: FF0000 // 十六进制红色
})
],
spacing: {
before: 200, // 段前间距
after: 200, // 段后间距
line: 360 // 行高(1.5 倍 = 360)
},
alignment: center // left, center, right, justified
})
// 标题
new Paragraph({
heading: Heading1,
children: [new TextRun(章节标题)]
})
// 分页符
new Paragraph({
children: [new PageBreak()]
})
javascript
const { LevelFormat } = require(docx);
// 在文档中配置编号
const doc = new Document({
numbering: {
config: [
{
reference: bullet-list,
levels: [{
level: 0,
format: LevelFormat.BULLET,
text: •,
alignment: left,
style: {
paragraph: { indent: { left: 720, hanging: 360 } }
}
}]
},
{
reference: number-list,
levels: [{
level: 0,
format: LevelFormat.DECIMAL,
text: %1.,
alignment: left,
style: {
paragraph: { indent: { left: 720, hanging: 360 } }
}
}]
}
]
},
sections: [{
children: [
// 项目符号列表项
new Paragraph({
numbering: { reference: bullet-list, level: 0 },
children: [new TextRun(项目符号点)]
}),
// 编号列表项
new Paragraph({
numbering: { reference: number-list, level: 0 },
children: [new TextRun(编号项)]
})
]
}]
});
javascript
const { Table, TableRow, TableCell, WidthType, BorderStyle, ShadingType } = require(docx);
// 创建表格
new Table({
width: { size: 9360, type: WidthType.DXA }, // 完整内容宽度
columnWidths: [4680, 4680], // 两个等宽列
rows: [
// 表头行
new TableRow({
children: [
new TableCell({
children: [new Paragraph({ children: [new TextRun(表头 1)] })],
shading: { fill: 4472C4, type: ShadingType.SOLID },
borders: {
top: { style: BorderStyle.SINGLE, size: 1 },
bottom: { style: BorderStyle.SINGLE, size: 1 },
left: { style: BorderStyle.SINGLE, size: 1 },
right: { style: BorderStyle.SINGLE, size: 1 }
}
}),
new TableCell({
children: [new Paragraph({ children: [new TextRun(表头 2)] })],
shading: { fill: 4472C4, type: ShadingType.SOLID },
borders: {
top: { style: BorderStyle.SINGLE, size: 1 },
bottom: { style: BorderStyle.SINGLE, size: 1 },
left: { style: BorderStyle.SINGLE, size: 1 },
right: { style: BorderStyle.SINGLE, size: 1 }
}
})
]
}),
// 数据行
new TableRow({
children: [
new TableCell({
children: [new Paragraph({ children: [new TextRun(数据 1)] })],
borders: {
top: { style: BorderStyle.SINGLE, size: 1 },
bottom: { style: BorderStyle.SINGLE, size: 1 },
left: { style: BorderStyle.SINGLE, size: 1 },
right: { style: BorderStyle.SINGLE, size: 1 }
}
}),
new TableCell({
children: [new Paragraph({ children: [new TextRun(数据 2)] })],
borders: {
top: { style: BorderStyle.SINGLE, size: 1 },
bottom: { style: BorderStyle.SINGLE, size: 1 },
left: { style: BorderStyle.SINGLE, size: 1 },
right: { style: BorderStyle.SINGLE, size: 1 }
}
})
]
})
]
})
javascript
const { ImageRun } = require(docx);
const fs = require(fs);
new Paragraph({
children: [
new ImageRun({
type: png, // 必需:png, jpg, jpeg, gif, bmp, svg
data: fs.readFileSync(image.png),
transformation: {
width: 400,
height: 300
},
altText: {
title: 图片标题,
description: 图片描述,
name: 图片名称
}
})
]
})
javascript
const { Header, Footer, PageNumber } = require(docx);
const doc = new Document({
sections: [{
properties: {
page: { margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 } }
},
headers: {
default: new Header({
children: [
new Paragraph({
children: [
new TextRun({ text: 文档页眉, bold: true })
],
alignment: center
})
]
})
},
footers: {
default: new Footer({
children: [
new Paragraph({
children: [
new TextRun(第 ),
new TextRun({ children: [PageNumber.CURRENT] }),
new TextRun( 页,共 ),
new TextRun({ children: [PageNumber.TOTAL_PAGES] })
],
alignment: center
})
]
})
},
children: [/ 内容 /]
}]
});
javascript
const { TableOfContents, HeadingLevel } = require(docx);
const doc = new Document({
sections: [{
children: [
// 目录必须位于标题之前
new TableOfContents(目录, {
hyperlink: true,
headingStyleRange:
以下为平台配置的接入选项,并非逐项实测通过。能否安装取决于客户端支持、技能来源和运行环境:
帮我安装 SkillHub 和 docx-skill-1776104531 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 docx-skill-1776104531 技能
skillhub install docx-skill-1776104531
文件大小: 5.37 KB | 发布时间: 2026-4-14 11:37