Merge Word documents

GroupDocs.Merger for Python via .NET makes it straightforward to combine multiple Word processing documents — DOCX, DOC, RTF, and others — into a single file. All formatting, styles, images, tables, and other content are preserved without requiring Microsoft Word or any other desktop software.

Word processing files range from plain-text formats (TXT, CSV) to rich-text formats such as DOCX, DOC, and RTF that support full font, paragraph, and page formatting.

Steps to merge DOCX documents

  1. Create an instance of the Merger class and pass the path to the first (base) DOCX file.
  2. Call merger.join() with the path to each additional DOCX file to append.
  3. Call merger.save() with the desired output path to write the merged file.
from groupdocs.merger import Merger

def merge_word_documents():
    # Load the first document as the merge base
    with Merger("./input.docx") as merger:
        # Append the second Word document
        merger.join("./input2.docx")
        # Save the combined document
        merger.save("./output.docx")

if __name__ == "__main__":
    merge_word_documents()

input.docx is a sample file used in this example. Click here to download it.

input2.docx is a sample file used in this example. Click here to download it.

Binary file (DOCX, 8 KB)

Download full output

Explanation

  • Load base document: Merger("./input.docx") opens the first DOCX file as the merge base inside a context manager.
  • Append second document: merger.join("./input2.docx") appends the content of the second file. Call join again for each additional file.
  • Save result: merger.save("./output.docx") writes the merged document to disk.

Merge Word documents in continuous mode

Use WordJoinOptions with WordJoinMode.CONTINUOUS to join documents without inserting a page break between them — the second document flows on directly after the last paragraph of the first.

from groupdocs.merger import Merger
from groupdocs.merger.domain.options import WordJoinOptions, WordJoinMode

def merge_word_documents_continuous():
    # Load the first document as the merge base
    with Merger("./input.docx") as merger:
        # Configure join options for continuous (no page break) merging
        word_join_options = WordJoinOptions()
        word_join_options.mode = WordJoinMode.CONTINUOUS
        # Append the second document in continuous mode
        merger.join("./input2.docx", word_join_options)
        # Save the combined document
        merger.save("./output.docx")

if __name__ == "__main__":
    merge_word_documents_continuous()

input.docx is a sample file used in this example. Click here to download it.

input2.docx is a sample file used in this example. Click here to download it.

Binary file (DOCX, 8 KB)

Download full output

Explanation

  • WordJoinOptions: Provides Word-specific join settings.
  • WordJoinMode.CONTINUOUS: The appended document starts immediately after the last content of the preceding document rather than on a new page.

Merge Word files Live Demo

GroupDocs.Merger for Python via .NET provides an online Word Merger App, which allows you to try it for free and check its quality and accuracy.

API reference

See also