我在寫一個需要合併很多單頁pdf文件的小腳本。我希望腳本能夠使用Python3運行,並儘可能少地依賴。用Python3合併PDF文件
對於PDF合併部分,我嘗試使用PyPdf。但是,Python 3的支持似乎是錯誤的;它無法處理Inkscape生成的PDF文件(我需要)。我安裝PyPdf當前Git版本,下面的測試腳本不起作用:
import PyPDF2
output_pdf = PyPDF2.PdfFileWriter()
with open("testI.pdf", "rb") as input:
input_pdf = PyPDF2.PdfFileReader(input)
output_pdf.addPage(input_pdf.getPage(0))
with open("test.pdf", "wb") as output:
output_pdf.write(output)
這將引發以下堆棧跟蹤:
Traceback (most recent call last):
File "test.py", line 7, in <module>
output.addPage(input.getPage(0))
File "/usr/lib/python3.3/site-packages/pyPdf/pdf.py", line 420, in getPage
self._flatten()
File "/usr/lib/python3.3/site-packages/pyPdf/pdf.py", line 574, in _flatten
self._flatten(page.getObject(), inherit)
File "/usr/lib/python3.3/site-packages/pyPdf/generic.py", line 165, in getObject
return self.pdf.getObject(self).getObject()
File "/usr/lib/python3.3/site-packages/pyPdf/pdf.py", line 616, in getObject
retval = readObject(self.stream, self)
File "/usr/lib/python3.3/site-packages/pyPdf/generic.py", line 66, in readObject
return DictionaryObject.readFromStream(stream, pdf)
File "/usr/lib/python3.3/site-packages/pyPdf/generic.py", line 526, in readFromStream
value = readObject(stream, pdf)
File "/usr/lib/python3.3/site-packages/pyPdf/generic.py", line 57, in readObject
return ArrayObject.readFromStream(stream, pdf)
File "/usr/lib/python3.3/site-packages/pyPdf/generic.py", line 152, in readFromStream
obj = readObject(stream, pdf)
File "/usr/lib/python3.3/site-packages/pyPdf/generic.py", line 86, in readObject
return NumberObject.readFromStream(stream)
File "/usr/lib/python3.3/site-packages/pyPdf/generic.py", line 231, in readFromStream
return FloatObject(name.decode("ascii"))
File "/usr/lib/python3.3/site-packages/pyPdf/generic.py", line 207, in __new__
return decimal.Decimal.__new__(cls, str(value), context)
TypeError: optional argument must be a context
相同的腳本,但是,與完美的作品Python 2.7。
我在這裏做錯了什麼?這是圖書館的一個錯誤嗎?我可以在不觸摸PyPDF庫的情況下解決它嗎?
它經歷了嗎?我患有同樣的問題。 – PascalVKooten 2013-03-30 21:29:19
這[回購](https://github.com/colemana/PyPDF2)是一個固定版本。 – janoliver 2013-04-02 20:11:10
這個回購是1歲?你確定它有效嗎? – PascalVKooten 2013-04-03 05:01:02