问题
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:
page = input1.getPage(i)
page.rotateCounterClockwise(90)
output.addPage(page)
I can't find someone explaining the procedure. There is, however, a question in stackoverflow but the answer's just vague.
Thanks in advance. Sorry if I missed something.
回答1:
This is a known bug with the rotateClockwise
function. There is a fix in place which has not yet been merged. Just edit the '_rotate' method in your pdf.py
with this fix
def _rotate(self, angle):
rotateObj = self.get("/Rotate", 0)
currentAngle = rotateObj if isinstance(rotateObj, int) else rotateObj.getObject()
self[NameObject("/Rotate")] = NumberObject(currentAngle + angle)
回答2:
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.
来源:https://stackoverflow.com/questions/42615771/how-can-i-rotate-a-page-in-pypdf2