This guide walks you through setting up GroupDocs.Merger for Python via .NET and running three short examples that cover the most common document manipulation scenarios.
After activating the virtual environment, install the latest package version:
py-mpipinstallgroupdocs-merger-net
python3 -m pip install groupdocs-merger-net
python3 -m pip install groupdocs-merger-net
You should see:
Successfully installed groupdocs-merger-net-*
Example 1: Merge Two Documents
The simplest use case — load a base document, append a second document, and save the combined result.
fromgroupdocs.mergerimportMergerdefmerge_two_documents():# Load the first DOCX as the merge basewithMerger("./input.docx")asmerger:# Append the second DOCXmerger.join("./input2.docx")# Save the combined documentmerger.save("./output.docx")if__name__=="__main__":merge_two_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.
Merger("./input.docx") — opens the first document as the merge base inside a context manager that releases resources on exit.
merger.join("./input2.docx") — appends the entire second document after the last page of the first.
merger.save("./output.docx") — writes the combined document to disk.
Example 2: Extract Specific Pages from a PDF
Use extract_pages with an ExtractOptions page list to produce a new document containing only the specified pages.
fromgroupdocs.mergerimportMergerfromgroupdocs.merger.domain.optionsimportExtractOptionsdefextract_pages_from_pdf():# Load the source PDFwithMerger("./input.pdf")asmerger:# Extract pages 1 and 2 into a new documentmerger.extract_pages(ExtractOptions([1,2]))# Save the extracted pages as a new PDFmerger.save("./output.pdf")if__name__=="__main__":extract_pages_from_pdf()
input.pdf is a sample file used in this example. Click here to download it.
ExtractOptions([1, 2]) — specifies a 1-based list of page numbers to keep. Pages not in the list are discarded.
merger.extract_pages(...) — filters the loaded document to only the specified pages.
merger.save("./output.pdf") — writes the result (a 2-page PDF) to disk.
You can also use a range with RangeMode instead of an explicit list:
fromgroupdocs.merger.domain.optionsimportExtractOptions,RangeMode# Extract all even-numbered pages between page 1 and page 6merger.extract_pages(ExtractOptions(start_number=1,end_number=6,mode=RangeMode.EVEN_PAGES))
Example 3: Split a Document into Single Pages
Use split with a SplitOptions page list to write each listed page to its own output file.
fromgroupdocs.mergerimportMergerfromgroupdocs.merger.domain.optionsimportSplitOptionsdefsplit_document_pages():# Load the source PDFwithMerger("./input.pdf")asmerger:# Split pages 1, 2, and 3 into separate files# {0} in the path format is replaced with the page numbermerger.split(SplitOptions("./page_{0}.pdf",[1,2,3]))if__name__=="__main__":split_document_pages()
input.pdf is a sample file used in this example. Click here to download it.
After running, you will find page_1.pdf, page_2.pdf, and page_3.pdf in the current directory.
Explanation
SplitOptions("./page_{0}.pdf", [1, 2, 3]) — specifies the output file name pattern and the 1-based list of pages to extract. {0} is replaced with the page number for each output file.
merger.split(...) — writes one output file per listed page.