@benedicte/docx-merge

中文文档
0.1.3发布于 2025/09/09MITTypeScriptESM + CJS近 30 天下载 4983

A fast Node.js library written in TypeScript for merging two Microsoft Word (.docx) documents into one.

原始使用文档
文档目录12 节
  1. 📦 Installation
  2. 📚 Dependencies
  3. 🛠️ API
  4. 💡 Examples
  5. Replace a placeholder in the source DOCX
  6. Get the merged result as a Buffer
  7. Input files as Buffers
  8. Insert at the start of the document
  9. Insert at the end of the document
  10. 🧪 Testing
  11. 🔒 License
  12. 🤝 Contributing

📄 docx-merge

A fast and lightweight Node.js library written in TypeScript for merging two Microsoft Word (.docx) documents into one. Easily insert content at specific positions or based on placeholder patterns.


📦 Installation

Install via npm:

npm install @benedicte/docx-merge

📚 Dependencies

Only two lightweight dependencies:

  • adm-zip – for extracting and rebuilding .docx (ZIP) files
  • fast-xml-parser – for parsing and modifying the DOCX XML content

🛠️ API

mergeDocx(
  sourcePathOrBuffer: string | Buffer,
  contentPathOrBuffer: string | Buffer,
  options: {
    outputPath?: string;
    pattern?: string;
    insertStart?: boolean;
    insertEnd?: boolean;
  }
): void | buffer

Parameters:

  • sourcePathOrBuffer (required) – Path to the base .docx file or the Buffer
  • contentPathOrBuffer (required) – Path to the .docx file to insert into the base or the Buffer
  • options:
    • outputPath – If provided, writes the merged document to this path. If omitted, returns a Buffer
    • pattern – String pattern in the source file to replace with the inserted content
    • insertStart – Insert the content at the beginning of the source file
    • insertEnd – Insert the content at the end of the source file

🔔 Note: You can combine pattern, insertStart, and insertEnd and at least one is required.

💡 Examples

Replace a placeholder in the source DOCX

import { mergeDocx } from "@benedicte/docx-merge";

mergeDocx("./source.docx", "./table.docx", {
  outputPath: "./output.docx",
  pattern: "{{table}}",
});

Get the merged result as a Buffer

import { mergeDocx } from "@benedicte/docx-merge";

const buffer = mergeDocx("./source.docx", "./table.docx", {
  pattern: "{{table}}",
});

// Use buffer (e.g., send as a response in a server)

Input files as Buffers

import { mergeDocx } from "@benedicte/docx-merge";

const sourceBuffer = readFileSync('./source_file.docx');
const contentBuffer = readFileSync('./content_to_insert_in_source_file.docx');

const buffer = mergeDocx(sourceBuffer, contentBuffer, {
  outputPath: "./output.docx",
  pattern: "{{table}}",
});

Insert at the start of the document

import { mergeDocx } from "@benedicte/docx-merge";

mergeDocx("./source.docx", "./table.docx", {
  outputPath: "./output.docx",
  insertStart: true,
});

Insert at the end of the document

import { mergeDocx } from "@benedicte/docx-merge";

mergeDocx("./source.docx", "./table.docx", {
  outputPath: "./output.docx",
  insertEnd: true,
});

🧪 Testing

This project uses Vitest for unit testing.

To run tests:

npm run test

🔒 License

MIT

🤝 Contributing

Contributions, bug reports, and feature requests are welcome! Feel free to open an issue or submit a pull request.

版本与发布记录

@benedicte/docx-merge 共发布 7 个版本,首次发布于 2025/05/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.3latest2025/09/091.2 年前
0.1.22025/05/071.5 年前
0.1.12025/05/061.5 年前
0.1.02025/05/061.5 年前
0.1.0-rc52025/05/061.5 年前
0.1.0-rc42025/05/061.5 年前

关于 @benedicte/docx-merge 的常见问题

@benedicte/docx-merge 是什么?

A fast Node.js library written in TypeScript for merging two Microsoft Word (.docx) documents into one.

如何安装 @benedicte/docx-merge?

在项目目录运行 npm install @benedicte/docx-merge,或使用 pnpm add / yarn add / bun add。

@benedicte/docx-merge 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@benedicte/docx-merge 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@benedicte/docx-merge 可以商用吗?

@benedicte/docx-merge 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@benedicte/docx-merge 的下载量有多少?

根据 npm 官方下载统计,@benedicte/docx-merge 在 2026/10/08 – 2026/11/06 这 30 天内被下载 4,983 次,日均约 166 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@benedicte/docx-merge 还在维护吗?

最新版本 0.1.3 发布于 2025/09/09(1.2 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/05/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。