GroupDocs.Merger for Python via .NET lets you rotate specific pages (or all pages) in a PDF document by 90, 180, or 270 degrees. The rotation is configured through RotateOptions and applied with merger.rotate().
Here are the steps to rotate document pages:
Instantiate Merger with the path to the source document.
Create a RotateOptions object with the desired RotateMode and the list of 1-based page numbers to rotate.
Call merger.rotate() passing the RotateOptions object.
Call merger.save() to write the resulting document.
fromgroupdocs.mergerimportMergerfromgroupdocs.merger.domain.optionsimportRotateOptions,RotateModedefrotate_document_pages():# Load the source PDF documentwithMerger("./input.pdf")asmerger:# Rotate pages 1 and 2 by 90 degrees clockwise# Pass page numbers as a list — positional integer arguments are not supportedmerger.rotate(RotateOptions(RotateMode.ROTATE90,[1,2]))# Save the document with the rotated pagesmerger.save("./output.pdf")if__name__=="__main__":rotate_document_pages()
input.pdf is a sample file used in this example. Click here to download it.
Load source document: Merger("./input.pdf") opens the document as a context manager, ensuring resources are released on exit.
RotateOptions(RotateMode.ROTATE90, [1, 2]): the first argument is the rotation angle enum; the second argument is a list of 1-based page numbers. Available modes are RotateMode.ROTATE90, RotateMode.ROTATE180, and RotateMode.ROTATE270.
Page numbers must be a list: RotateOptions(RotateMode.ROTATE90, [1, 2]) is correct. Passing integers positionally — RotateOptions(mode, 1, 2) — is not supported and raises a runtime error.
merger.rotate(): the correct Python method name. The old method name rotate_pages does not exist and must not be used.
merger.save("./output.pdf"): writes the resulting document with the pages at their new rotation angles.
Tip
Rotation is cumulative across multiple calls within the same Merger session. To rotate different pages by different angles, call merger.rotate() once per angle before calling merger.save().