0.1.2 โ€ข Published 6 months ago

@benedicte/docx-merge v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

๐Ÿ“„ 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(
  sourcePath: string,
  contentPath: string,
  options: {
    outputPath?: string;
    pattern?: string;
    insertStart?: boolean;
    insertEnd?: boolean;
  }
): void | buffer

Parameters:

  • sourcePath (required) โ€“ Path to the base .docx file
  • contentPath (required) โ€“ Path to the .docx file to insert into the base
  • 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)

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.

0.1.2

6 months ago

0.1.1

6 months ago

0.1.0

6 months ago

0.1.0-rc5

6 months ago

0.1.0-rc4

6 months ago

0.1.0-rc3

6 months ago