node docx库的使用

717 阅读2分钟

在 Node.js 中使用 docx 库来创建和编辑 Word 文档非常直观。docx 库提供了丰富的功能来生成复杂的 Word 文档,包括文本、图片、表格、列表等。

下面是一个简单的示例,展示如何使用 docx 库创建一个包含文本、图片和表格的基本 Word 文档:

安装 docx

首先,确保您已经安装了 docx 库。如果没有安装,可以通过以下命令安装:

npm install docx

创建 Word 文档

接下来,我们将创建一个简单的 Word 文档,包含标题、段落、图片和表格。

const { Document, Packer, Paragraph, TextRun, ImageRun, Table, TableRow, TableCell } = require('docx');

// 创建一个文档实例
const doc = new Document({
  sections: [
    {
      properties: {},
      children: [
        // 添加标题
        new Paragraph({
          children: [
            new TextRun({
              text: 'Hello, World!',
              bold: true,
              size: 28,
            }),
          ],
        }),

        // 添加段落
        new Paragraph({
          children: [
            new TextRun('This is a simple paragraph.'),
          ],
        }),

        // 添加图片
        new Paragraph({
          children: [
            new ImageRun({
              data: fs.readFileSync('path/to/image.jpg'),
              transformation: {
                width: 300,
                height: 300,
              },
            }),
          ],
        }),

        // 添加表格
        new Table({
          rows: [
            new TableRow({
              children: [
                new TableCell({
                  children: [
                    new Paragraph({
                      children: [
                        new TextRun('Cell 1'),
                      ],
                    }),
                  ],
                }),
                new TableCell({
                  children: [
                    new Paragraph({
                      children: [
                        new TextRun('Cell 2'),
                      ],
                    }),
                  ],
                }),
              ],
            }),
            new TableRow({
              children: [
                new TableCell({
                  children: [
                    new Paragraph({
                      children: [
                        new TextRun('Cell 3'),
                      ],
                    }),
                  ],
                }),
                new TableCell({
                  children: [
                    new Paragraph({
                      children: [
                        new TextRun('Cell 4'),
                      ],
                    }),
                  ],
                }),
              ],
            }),
          ],
        }),

        // 添加带有编号的列表
        new Paragraph({
          children: [
            new TextRun({
              text: 'Item 1',
              paragraph: { numbering: { reference: 'decimal' } },
            }),
          ],
        }),

        new Paragraph({
          children: [
            new TextRun({
              text: 'Item 2',
              paragraph: { numbering: { reference: 'decimal' } },
            }),
          ],
        }),
      ],
    },
  ],
});

// 将文档打包并写入文件
Packer.toBuffer(doc).then((buffer) => {
  fs.writeFileSync('output.docx', buffer);
  console.log('Document created successfully!');
}).catch((error) => {
  console.error('Error creating document:', error);
});

说明

  1. 导入必需的模块:

    • 导入 Document 类和其他所需的类,如 Paragraph, TextRun, ImageRun, Table, TableRow, TableCell 等。
  2. 创建文档实例:

    • 使用 Document 类创建一个新的文档实例,并定义文档的结构。
  3. 添加内容:

    • 使用 Paragraph 添加段落。
    • 使用 TextRun 添加文本。
    • 使用 ImageRun 添加图片。
    • 使用 Table, TableRow, TableCell 添加表格。
    • 使用 TextRunparagraph 属性添加带有编号的列表。
  4. 打包并保存文档:

    • 使用 Packer.toBuffer 将文档打包成缓冲区。
    • 使用 fs.writeFileSync 将缓冲区写入到文件中。

注意事项

  • 确保您已经安装了 docx 库。
  • 如果您的项目中没有 fs 模块,需要先从 Node.js 核心模块导入它。
  • 图片路径需要替换为实际的图片文件路径。

完整代码

将上述代码保存为一个 .js 文件,例如 create-word.js,然后在命令行中运行它:

node create-word.js

这将在当前目录下生成一个名为 output.docx 的文件,其中包含了文本、图片和表格。

更多格式化选项

docx 库提供了许多其他的格式化选项,例如设置字体颜色、对齐方式、条件格式等。您可以在 docx 文档 中找到更多关于格式化和样式的详细信息。