Overview
Merging Word documents is a common requirement in many applications, whether for combining reports, consolidating information, or just simplifying document management. In this article, we will explore how to merge DOCX files using Python with the GroupDocs.Merger library. This powerful library allows developers to easily manipulate Word documents programmatically while ensuring high performance and accuracy.
This guide will cover:
- How to merge multiple DOCX files
- How to merge documents without section breaks
- How to merge with predefined compliance modes
- Handling document streams in Python
How to merge multiple DOCX files
Merging multiple DOCX files is straightforward with the GroupDocs.Merger library. Below, we outline the key steps involved in this process:
- Import the GroupDocs.Merger package: Begin by importing the required library.
- Create a Merger instance: Instantiate the Merger class using the first DOCX file path.
- Join additional documents: Use the
join
method to add other DOCX files that you wish to merge. - Save the merged document: Finally, call the
save
method to write the merged document to a specified output path.
Here’s a Python code snippet demonstrating these steps:
import groupdocs.merger as gm
import constants
def run():
print(f"----------------------------------------------------------------------------")
print(f"[Example Basic Usage] # MultipleDocumentOperations # Merge : Docx")
# Step 1: Create a Merger object with the first document
with gm.Merger(constants.sample_docx) as merger:
print(f"Document info retrieved successfully")
# Step 2: Join another DOCX file to merge
merger.join(constants.sample_docx)
# Step 3: Save the merged document
merger.save(constants.output_docx)
print(f"Merge to: {constants.output_docx}")
print(f"----------------------------------------------------------------------------")
Merge DOCX documents without section breaks
Sometimes, merging documents requires that the contents be joined without inserting section breaks. This can help maintain the flow of information across the merged documents.
- Create a Merger instance: Just like before, initialize the Merger class.
- Set join options: Define
WordJoinOptions
and set themode
toDISABLE_SECTION_BREAKS
. - Join documents: Add the additional document to merge.
- Save the result: Write the final output to a new DOCX file.
Here’s how you can do it:
import groupdocs.merger as gm
import constants
def run():
print(f"----------------------------------------------------------------------------")
print(f"[Example Basic Usage] # MultipleDocumentOperations # Merge # Word : MergeWordDocumentsWithoutSectionBreaks")
# Step 1: Create a Merger object with the source document
with gm.Merger(constants.sample_doc) as merger:
print(f"Document info retrieved successfully")
# Step 2: Define Word join options to disable section breaks
word_join_options = gm.domain.options.WordJoinOptions()
word_join_options.mode = gm.domain.options.WordJoinMode.DISABLE_SECTION_BREAKS
# Step 3: Join the document without section breaks
merger.join(constants.sample_doc, word_join_options)
# Step 4: Save the merged document
merger.save(constants.output_doc_without_section_breaks)
print(f"Merge to: {constants.output_doc_without_section_breaks}")
print(f"----------------------------------------------------------------------------")
Merge DOCX documents with predefined compliance modes
For specific use cases, documents may need to comply with certain standards. GroupDocs.Merger allows you to merge documents with predefined compliance modes to ensure, for example, that the output meets ISO standards.
- Create a Merger instance: Initialize the Merger class with your base document.
- Set compliance mode: Create a
WordJoinOptions
object and set thecompliance
property. - Join documents: Add the additional document using the join method.
- Save the final document: Save it to your desired output location.
Here is the code for this operation:
import groupdocs.merger as gm
import constants
def run():
print(f"----------------------------------------------------------------------------")
print(f"[Example Basic Usage] # MultipleDocumentOperations # Merge # Word : MergeWordDocumentsWithPredefinedComplianceMode")
# Step 1: Create a Merger object with the document
with gm.Merger(constants.sample_docx) as merger:
print(f"Document info retrieved successfully")
# Step 2: Define Word join options with predefined compliance mode
word_join_options = gm.domain.options.WordJoinOptions()
word_join_options.compliance = gm.domain.options.WordJoinCompliance.ISO_29500_2008_STRICT
# Step 3: Join another document with compliance mode
merger.join(constants.sample_docx, word_join_options)
# Step 4: Save the merged document
merger.save(constants.output_docx_with_predefined_compliance_mode)
print(f"Merge to: {constants.output_docx_with_predefined_compliance_mode}")
print(f"----------------------------------------------------------------------------")
How to merge DOCX from streams
You can also merge DOCX files from in-memory streams, which can be useful when you are dealing with dynamically generated documents.
- Get a file stream: Open your document in binary mode to get the stream.
- Create the Merger instance: Instantiate the
Merger
class using the stream. - Perform the merging operation: Merge as required and save the output.
Here’s how you would implement this:
from turtle import update
import groupdocs.merger as gm
import constants
def run():
print(f"----------------------------------------------------------------------------")
print(f"[Example Advanced Usage] # Loading # LoadDocumentFromStream")
# Step 1: Get the document stream
stream = get_file_stream()
# Step 2: Create a Merger instance using the stream
with gm.Merger(stream) as merger:
print(f"Document loaded from stream successfully")
print(f"----------------------------------------------------------------------------")
def get_file_stream():
file_path = constants.sample_docx
return open(file_path, "rb")
See also
For more details, you can explore the following resources:
- GroupDocs.Merger for Python Documentation
- API Reference for GroupDocs.Merger
- GroupDocs.Merger GitHub Examples
You can also download a free trial from releases.groupdocs.com and acquire a temporary license to try the library without restrictions at Purchase Temporary License.
Code Examples
Here are some additional examples to help you understand the merging process with the GroupDocs.Merger library for Python:
Merge Word Documents Without Starting From a New Page
This example demonstrates how to merge documents so that the last page of the first document is directly followed by the first page of the next document, without any new page inserted in between.
import groupdocs.merger as gm
import constants
def run():
print(f"----------------------------------------------------------------------------")
print(f"[Example Basic Usage] # MultipleDocumentOperations # Merge # Word : MergeWordDocumentsWithoutStartingFromNewPage")
# Step 1: Create a Merger object with the document
with gm.Merger(constants.sample_doc) as merger:
print(f"Document info retrieved successfully")
# Step 2: Define Word join options for continuous mode
word_join_options = gm.domain.options.WordJoinOptions()
word_join_options.mode = gm.domain.options.WordJoinMode.CONTINUOUS
# Step 3: Join documents without inserting new page
merger.join(constants.sample_doc, word_join_options)
# Step 4: Save the merged document
merger.save(constants.output_doc_without_starting_from_new_page)
print(f"Merge to: {constants.output_doc_without_starting_from_new_page}")
print(f"----------------------------------------------------------------------------")
Merge Multiple Documents with Custom Join Options
Here’s how to merge DOCX documents while setting specific join options, such as compliance with a specific ISO standard.
import groupdocs.merger as gm
import constants
def run():
print(f"----------------------------------------------------------------------------")
print(f"[Example Advanced Usage] # Merge with Custom Join Options")
# Step 1: Create a Merger object with the base document
with gm.Merger(constants.sample_docx) as merger:
print(f"Document info retrieved successfully")
# Step 2: Set custom join options for predefined compliance mode
word_join_options = gm.domain.options.WordJoinOptions()
word_join_options.compliance = gm.domain.options.WordJoinCompliance.ISO_29500_2008_STRICT
# Step 3: Join another document with compliance settings
merger.join(constants.sample_docx, word_join_options)
# Step 4: Save the merged document
merger.save(constants.output_docx_with_predefined_compliance_mode)
print(f"Merge to: {constants.output_docx_with_predefined_compliance_mode}")
print(f"----------------------------------------------------------------------------")
See also
To explore more about GroupDocs.Merger and its functionalities, you can check out the following resources:
- GroupDocs.Merger for Python Documentation
- API Reference for GroupDocs.Merger
- GitHub Code Examples for GroupDocs.Merger
- Releases Page for GroupDocs.Merger
Free Trial and Temporary License
You can download a free trial of GroupDocs.Merger from our releases page. Additionally, you can acquire a temporary license for evaluation purposes at Purchase Temporary License. This temporary license allows you to try the library without any limitations and evaluate its capabilities fully.