2011-12-01 26 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

“不变”?这似乎不太可能。至少,如果你以某种方式触及另一个断言失败,它应该在另一条线上。 –