Quick Start Guide

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.

Prerequisites

To proceed, make sure you have:

  1. A configured environment as described in the System Requirements topic.
  2. Optionally you may Get a Temporary License to test all product features without evaluation limits.

Set Up Your Development Environment

For best practices, use a virtual environment to manage dependencies. Learn more in the Create and Use Virtual Environments documentation.

Create and Activate a Virtual Environment

Create a virtual environment:

py -m venv .venv
python3 -m venv .venv
python3 -m venv .venv

Activate the virtual environment:

.venv\Scripts\activate
source .venv/bin/activate
source .venv/bin/activate

Install the groupdocs-merger-net Package

After activating the virtual environment, install the latest package version:

py -m pip install groupdocs-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.

from groupdocs.merger import Merger

def merge_two_documents():
    # Load the first DOCX as the merge base
    with Merger("./input.docx") as merger:
        # Append the second DOCX
        merger.join("./input2.docx")
        # Save the combined document
        merger.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.

Binary file (DOCX, 8 KB)

Download full output

Your folder should look like this:

demo-app/
 ├── merge_two_documents.py
 ├── input.docx
 └── input2.docx

Run the App

py merge_two_documents.py
python3 merge_two_documents.py
python3 merge_two_documents.py

Check for output.docx in the current directory.

Explanation

  • 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.

from groupdocs.merger import Merger
from groupdocs.merger.domain.options import ExtractOptions

def extract_pages_from_pdf():
    # Load the source PDF
    with Merger("./input.pdf") as merger:
        # Extract pages 1 and 2 into a new document
        merger.extract_pages(ExtractOptions([1, 2]))
        # Save the extracted pages as a new PDF
        merger.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.

Binary file (PDF, 85 KB)

Download full output

Run the App

py extract_pages_from_pdf.py
python3 extract_pages_from_pdf.py
python3 extract_pages_from_pdf.py

Explanation

  • 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:

from groupdocs.merger.domain.options import ExtractOptions, RangeMode

# Extract all even-numbered pages between page 1 and page 6
merger.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.

from groupdocs.merger import Merger
from groupdocs.merger.domain.options import SplitOptions

def split_document_pages():
    # Load the source PDF
    with Merger("./input.pdf") as merger:
        # Split pages 1, 2, and 3 into separate files
        # {0} in the path format is replaced with the page number
        merger.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.

page_1.pdf (84 KB)
page_2.pdf (84 KB)
page_3.pdf (84 KB)

Download full output

Run the App

py split_document_pages.py
python3 split_document_pages.py
python3 split_document_pages.py

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.

Next Steps