2011-12-01 33 views
3

我在Windows平臺上使用Python 2.4和PyPdf 1.13。 我想從列表合併PDF文件合併成一個使用下面的代碼:Python中Pypdf軟件包中的聲明錯誤

import os 
from pyPdf import PdfFileWriter, PdfFileReader 

attached=["C:\\test\\T_tech.pdf","C:\\test\\00647165-Backup.pdf"] 
output=PdfFileWriter() 
maxpage=0 

os.chdir("C:\\test") 
name= attached[0] 
name = os.path.basename(name).split('.')[0] 

for nam in attached: 
    input= PdfFileReader(file(nam,"rb")) 
    maxpage=input.getNumPages() 
    for i in range(0,maxpage): 
    output.addPage(input.getPage(i)) 

outputStream =file("Output-"+name+".pdf","wb") 
output.write(outputStream) 
outputStream.close() 

當我運行這段代碼我收到以下錯誤。

Traceback (most recent call last): 
    File "C:\Python24\pdfmerge.py", line 13, in ? 
     input= PdfFileReader(file(nam,"rb")) 
    File "C:\Python24\Lib\site-packages\pyPdf\pdf.py", line 374, in __init__ 
     self.read(stream) 
    File "C:\Python24\Lib\site-packages\pyPdf\pdf.py", line 847, in read 
     assert False 
    AssertionError 

任何幫助,非常感謝。

回答

1

從來源:

  # bad xref character at startxref. Let's see if we can find 
      # the xref table nearby, as we've observed this error with an 
      # off-by-one before. 
      stream.seek(-11, 1) 
      tmp = stream.read(20) 
      xref_loc = tmp.find("xref") 
      if xref_loc != -1: 
       startxref -= (10 - xref_loc) 
       continue 
      else: 
       # no xref table found at specified location 
       assert False 
       break 

你打的是後者「沒有交叉引用發現表...」狀態。嘗試修補源代碼,省略斷言並查看它是否仍然有效。

+0

這可能與他使用Python 2.4的事實有關。我無法在Windows上重現Python 2.7.2的錯誤。 – jsalonen

+0

嗨,布萊恩,謝謝你的嘗試。我試着通過評論它,但仍然沒有改變。 – gaya3

+0

「不變」?這似乎不太可能。至少,如果你以某種方式觸及另一個斷言失敗,它應該在另一條線上。 –