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
Create an instance of the Merger class and pass the path to the first (base) DOCX file.
Call merger.join() with the path to each additional DOCX file to append.
Call merger.save() with the desired output path to write the merged file.
fromgroupdocs.mergerimportMergerdefmerge_word_documents():# Load the first document as the merge basewithMerger("./input.docx")asmerger:# Append the second Word documentmerger.join("./input2.docx")# Save the combined documentmerger.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.
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.
fromgroupdocs.mergerimportMergerfromgroupdocs.merger.domain.optionsimportWordJoinOptions,WordJoinModedefmerge_word_documents_continuous():# Load the first document as the merge basewithMerger("./input.docx")asmerger:# Configure join options for continuous (no page break) mergingword_join_options=WordJoinOptions()word_join_options.mode=WordJoinMode.CONTINUOUS# Append the second document in continuous modemerger.join("./input2.docx",word_join_options)# Save the combined documentmerger.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.