我们有一个简单的脚本来读取传入的PDF文件。如果是横向的,它会将其旋转到纵向,以便稍后由另一个程序使用。在我遇到一个IndirectObject作为页面上的/Rotate键值的文件之前,所有的pyPdf都运行得很好。这个对象是可解析的,所以我可以知道/Rotate的值是什么,但是当尝试rotateClockwise或rotateCounterClockwise时,我得到了一个回溯,因为pyPdf不需要/Rotate中的IndirectObject。我已经对这个文件做了相当多的工作,试图用这个值覆盖IndirectObject,但是我没有得到任何结果。我甚至尝试将相同的IndirectObject传递给rotateClockwise,它会抛出相同的回溯,这是pdf.pyc中之前的一行代码
我的问题很简单,就是。。。有没有针对pyPdf或PyPDF2的补丁,可以让它在这种设置中不会卡住,或者我可以用一种不同的方式来旋转页面,或者是一个我还没有见过/考虑过的不同的库?我试过PyPDF2,它也有同样的问题。我把PDFMiner看作是一个替代品,但它似乎更倾向于从PDF文件中获取信息,而不是操纵它们。这是我在ipython中使用pyPDF处理文件的输出,PyPDF2的输出非常相似,但信息的一些格式略有不同:
In [1]: from pyPdf import PdfFileReader
In [2]: mypdf = PdfFileReader(open("RP121613.pdf","rb"))
In [3]: mypdf.getNumPages()
Out[3]: 1
In [4]: mypdf.resolvedObjects
Out[4]:
{0: {1: {'/Pages': IndirectObject(2, 0), '/Type': '/Catalog'},
2: {'/Count': 1, '/Kids': [IndirectObject(4, 0)], '/Type': '/Pages'},
4: {'/Count': 1,
'/Kids': [IndirectObject(5, 0)],
'/Parent': IndirectObject(2, 0),
'/Type': '/Pages'},
5: {'/Contents': IndirectObject(6, 0),
'/MediaBox': [0, 0, 612, 792],
'/Parent': IndirectObject(4, 0),
'/Resources': IndirectObject(7, 0),
'/Rotate': IndirectObject(8, 0),
'/Type': '/Page'}}}
In [5]: mypage = mypdf.getPage(0)
In [6]: myrotation = mypage.get("/Rotate")
In [7]: myrotation
Out[7]: IndirectObject(8, 0)
In [8]: mypdf.getObject(myrotation)
Out[8]: 0
In [9]: mypage.rotateCounterClockwise(90)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/root/<ipython console> in <module>()
/usr/lib/python2.7/site-packages/pyPdf/pdf.pyc in rotateCounterClockwise(self, angle)
1049 def rotateCounterClockwise(self, angle):
1050 assert angle % 90 == 0
-> 1051 self._rotate(-angle)
1052 return self
1053
/usr/lib/python2.7/site-packages/pyPdf/pdf.pyc in _rotate(self, angle)
1054 def _rotate(self, angle):
1055 currentAngle = self.get("/Rotate", 0)
-> 1056 self[NameObject("/Rotate")] = NumberObject(currentAngle + angle)
1057
1058 def _mergeResources(res1, res2, resource):
TypeError: unsupported operand type(s) for +: 'IndirectObject' and 'int'
In [10]: mypage.rotateClockwise(90)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/root/<ipython console> in <module>()
/usr/lib/python2.7/site-packages/pyPdf/pdf.pyc in rotateClockwise(self, angle)
1039 def rotateClockwise(self, angle):
1040 assert angle % 90 == 0
-> 1041 self._rotate(angle)
1042 return self
1043
/usr/lib/python2.7/site-packages/pyPdf/pdf.pyc in _rotate(self, angle)
1054 def _rotate(self, angle):
1055 currentAngle = self.get("/Rotate", 0)
-> 1056 self[NameObject("/Rotate")] = NumberObject(currentAngle + angle)
1057
1058 def _mergeResources(res1, res2, resource):
TypeError: unsupported operand type(s) for +: 'IndirectObject' and 'int'
In [11]: mypage.rotateCounterClockwise(myrotation)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/root/<ipython console> in <module>()
/usr/lib/python2.7/site-packages/pyPdf/pdf.pyc in rotateCounterClockwise(self, angle)
1048 # @param angle Angle to rotate the page. Must be an increment of 90 deg.
1049 def rotateCounterClockwise(self, angle):
-> 1050 assert angle % 90 == 0
1051 self._rotate(-angle)
1052 return self
TypeError: unsupported operand type(s) for %: 'IndirectObject' and 'int'如果有人想深入查看我正在使用的文件,我将很乐意提供它。
发布于 2014-08-24 14:38:11
您需要对IndirectObject的一个实例应用getObject,因此在本例中应该是
myrotation.getObject()发布于 2017-08-14 23:48:29
我意识到这是一个古老的问题,但我在搜索中发现了这篇文章,试图在找到解决方案之前更快地解决这个问题。据我所知,这是一个bug。
https://github.com/mstamy2/PyPDF2/pull/338/files
总之,我直接编辑了PyPDF2源代码来实现修复。找到PyPDF2/pdf.py并搜索def _rotate(self,angle):行。替换为以下内容:
def _rotate(self, angle):
rotateObj = self.get("/Rotate", 0)
currentAngle = rotateObj if isinstance(rotateObj, int) else rotateObj.getObject()
self[NameObject("/Rotate")] = NumberObject(currentAngle + angle)它现在就像一个护身符一样工作。
https://stackoverflow.com/questions/20646375
复制相似问题