'How can I rotate a page with PyPDF2?

I'm editing a PDF file with pyPDF2. I managed to generate the PDF I want but I've yet to rotate some pages.

I went to the documentation and found two methods: rotateClockwise and rotateCounterClockwise, and while they say the parameter is an int, I can't make it work. Python says:

TypeError: unsupported operand type(s) for +: 'IndirectObject' and 'int'

To produce this error:

from PyPDF2 import PdfFileReader, PdfFileWriter


reader = PdfFileReader("example.pdf")

with open("out.pdf", "wb") as fh:
    writer = PdfFileWriter(fh)
    page = reader.getPage(0)
    page.rotateCounterClockwise(90)
    writer.addPage(page)

I can't find someone explaining the procedure. There is, however, a question in stackoverflow but the answer's just vague.



Solution 1:[1]

Try replacing your three lines with this:

output.addPage(input1.getPage(i).rotateCounterClockwise(90))

I think the rotate has to be done to a getPage call and not on the "extracted" page.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James C. Taylor