Extract pages

GroupDocs.Merger for Python via .NET lets you extract selected pages from a source document and save them as a new document. You can specify pages by exact numbers or by a page range with an even/odd filter — all through ExtractOptions.

Here are the steps to extract document pages:

  • Instantiate Merger with the path to the source document.
  • Create an ExtractOptions object with the desired page numbers or range.
  • Call merger.extract_pages() passing the ExtractOptions object.
  • Call merger.save() to write the resulting document.
from groupdocs.merger import Merger
from groupdocs.merger.domain.options import ExtractOptions, RangeMode

def extract_pages_by_numbers():
    # Load the source PDF document
    with Merger("./input.pdf") as merger:
        # Extract pages 2, 4, and 6 into a new document
        merger.extract_pages(ExtractOptions([2, 4, 6]))
        # Save the document that contains only the extracted pages
        merger.save("./output.pdf")

def extract_pages_even_range():
    # Load the source PDF document
    with Merger("./input.pdf") as merger:
        # Extract all even-numbered pages between pages 1 and 6 (i.e. pages 2, 4, 6)
        merger.extract_pages(ExtractOptions(
            start_number=1,
            end_number=6,
            mode=RangeMode.EVEN_PAGES,
        ))
        # Save the resulting document
        merger.save("./output.pdf")

if __name__ == "__main__":
    extract_pages_by_numbers()
    extract_pages_even_range()

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

Binary file (PDF, 250 KB)

Download full output

Explanation

  • Load source document: Merger("./input.pdf") opens the document as a context manager, ensuring resources are released on exit.
  • Extract by exact page numbers (ExtractOptions([2, 4, 6])): pass a list of 1-based page numbers. The output document contains those pages in their original order.
  • Extract by range with filter (ExtractOptions(start_number=1, end_number=6, mode=RangeMode.EVEN_PAGES)): specify a start and end page, then use RangeMode.EVEN_PAGES or RangeMode.ODD_PAGES to select only the matching pages within that range.
  • merger.extract_pages(): the correct Python method name. The equivalent .NET method is ExtractPages — do not use merger.extract() (it does not exist).
  • merger.save("./output.pdf"): always call save() after extract_pages() to write the output document.
Tip
Page numbers are always 1-based. Requesting a page number beyond the document’s page count raises an exception.

API reference

See also