2017-03-06 158 views
2

我正在用pyPDF2编辑PDF文件。我设法生成我想要的PDF,但我还没有旋转一些页面。如何在pyPDF2中旋转页面?

我去the documentation,发现两种方法:rotateClockwiserotateCounterClockwise,虽然他们说的参数是INT,我不能使它发挥作用。蟒蛇说:

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

为了产生这种错误:

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

我找不到别人解释的过程。但是,在计算器中有question,但答案只是模糊不清。

在此先感谢。对不起,我错过了什么。

回答

3

This is a known bugrotateClockwise函数。尚未合并的There is a fix in place。只需编辑您的pdf.py中的'_rotate'方法并使用此修补程序

def _rotate(self, angle): 
    rotateObj = self.get("/Rotate", 0) 
    currentAngle = rotateObj if isinstance(rotateObj, int) else rotateObj.getObject() 
    self[NameObject("/Rotate")] = NumberObject(currentAngle + angle) 
0

尝试用这种替代你的三条线:

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

我觉得旋转也要做一个GETPAGE呼叫而不是“提取”页面上。