How can I rotate a page in pyPDF2?

蓝咒 提交于 2020-02-03 09:27:32

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!