Change page orientation

GroupDocs.Merger for Python via .NET lets you set Portrait or Landscape orientation for specific pages (or all pages) of a Word document. The orientation is configured through OrientationOptions and applied with merger.change_orientation().

Here are the steps to change the page orientation:

  • Instantiate Merger with the path to the source document.
  • Create an OrientationOptions object with the desired OrientationMode and the list of 1-based page numbers to change.
  • Call merger.change_orientation() passing the OrientationOptions object.
  • Call merger.save() to write the resulting document.
from groupdocs.merger import Merger
from groupdocs.merger.domain.options import OrientationOptions, OrientationMode

def change_page_orientation():
    # Load the source Word document
    with Merger("./input.pdf") as merger:
        # Set pages 1 and 2 to Landscape orientation
        merger.change_orientation(OrientationOptions(OrientationMode.LANDSCAPE, [1, 2]))
        # Save the document with the updated page orientations
        merger.save("./output.pdf")

if __name__ == "__main__":
    change_page_orientation()

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

Binary file (PDF, 336 KB)

Download full output

Explanation

  • Load source document: Merger("./input.pdf") opens the document as a context manager, ensuring resources are released on exit.
  • OrientationOptions(OrientationMode.LANDSCAPE, [1, 2]): the first argument is the orientation enum; the second argument is a list of 1-based page numbers. Use OrientationMode.LANDSCAPE or OrientationMode.PORTRAIT.
  • merger.change_orientation(): the correct Python method name. Do not use merger.orientation() — that method does not exist.
  • merger.save("./output.pdf"): writes the document with the updated page orientations to the specified path.
Tip
Page orientation changes are primarily meaningful for word-processing formats such as DOCX and DOC. Using change_orientation() on PDF files rewrites the page layout; for visual rotation of PDF page content, use Rotate pages instead.

API reference

See also