关闭python pypdf的问题 - 写作。获取valueError:关闭文件的I / O操作

时间:2011-07-21 09:02:32

标签: python pypdf

无法弄清楚这一点 这个函数(用于将互联网网站划分为pdf的类的一部分)应该使用pypdf合并从网页生成的pdf文件。

这是方法代码:

def mergePdf(self,mainname,inputlist=0):
    """merging the pdf pages
    getting an inputlist to merge or defaults to the class instance self.pdftomerge list"""
    from pyPdf import PdfFileWriter, PdfFileReader
    self._mergelist = inputlist or self.pdftomerge
    self.pdfoutput = PdfFileWriter()

    for name in self._mergelist:
        print "merging %s into main pdf file: %s" % (name,mainname)
        self._filestream = file(name,"rb")
        self.pdfinput = PdfFileReader(self._filestream)
        for p in self.pdfinput.pages:
            self.pdfoutput.addPage(p)
        self._filestream.close()

    self._pdfstream = file(mainname,"wb")
    self._pdfstream.open()
    self.pdfoutput.write(self._pdfstream)
    self._pdfstream.close()

我一直收到这个错误:

  File "c:\tmp\easy_install-iik9vj\pyPdf-1.13-py2.7-win32.egg.tmp\pyPdf\pdf.py", line 264, in write
    self._sweepIndirectReferences(externalReferenceMap, self._root)
  File "c:\tmp\easy_install-iik9vj\pyPdf-1.13-py2.7-win32.egg.tmp\pyPdf\pdf.py", line 339, in _sweepIndirectReferences
    self._sweepIndirectReferences(externMap, realdata)
  File "c:\tmp\easy_install-iik9vj\pyPdf-1.13-py2.7-win32.egg.tmp\pyPdf\pdf.py", line 315, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "c:\tmp\easy_install-iik9vj\pyPdf-1.13-py2.7-win32.egg.tmp\pyPdf\pdf.py", line 339, in _sweepIndirectReferences
    self._sweepIndirectReferences(externMap, realdata)
  File "c:\tmp\easy_install-iik9vj\pyPdf-1.13-py2.7-win32.egg.tmp\pyPdf\pdf.py", line 315, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "c:\tmp\easy_install-iik9vj\pyPdf-1.13-py2.7-win32.egg.tmp\pyPdf\pdf.py", line 324, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, data[i])
  File "c:\tmp\easy_install-iik9vj\pyPdf-1.13-py2.7-win32.egg.tmp\pyPdf\pdf.py", line 339, in _sweepIndirectReferences
    self._sweepIndirectReferences(externMap, realdata)
  File "c:\tmp\easy_install-iik9vj\pyPdf-1.13-py2.7-win32.egg.tmp\pyPdf\pdf.py", line 315, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "c:\tmp\easy_install-iik9vj\pyPdf-1.13-py2.7-win32.egg.tmp\pyPdf\pdf.py", line 345, in _sweepIndirectReferences
    newobj = data.pdf.getObject(data)
  File "c:\tmp\easy_install-iik9vj\pyPdf-1.13-py2.7-win32.egg.tmp\pyPdf\pdf.py", line 645, in getObject
    self.stream.seek(start, 0)
ValueError: I/O operation on closed file

但当我检查self._pdfstream的状态时,我得到:

<open file 'c:\python27\learn\dive.pdf', mode 'wb' at 0x013B2020>

我做错了什么?

我会很高兴得到任何帮助

1 个答案:

答案 0 :(得分:7)

好的,我发现了你的问题。你拨打file()是对的。不要试着打电话给open()

您的问题是,当您致电self.pdfoutput.write(self._pdfstream)时仍需要打开输入文件,因此您需要删除该行self._filestream.close()

编辑:此脚本将触发此问题。第一次写入将成功,第二次写入将失败。

from pyPdf import PdfFileReader as PfR, PdfFileWriter as PfW

input_filename = 'in.PDF' # replace with a real file
output_filename = 'out.PDF' # something that doesn't exist

infile = file(input_filename, 'rb')
reader = PfR(infile)
writer = PfW()

writer.addPage(reader.getPage(0))
outfile = file(output_filename, 'wb')
writer.write(outfile)
print "First Write Successful!"
infile.close()
outfile.close()

infile = file(input_filename, 'rb')
reader = PfR(infile)
writer = PfW()

writer.addPage(reader.getPage(0))
outfile = file(output_filename, 'wb')
infile.close() # BAD!

writer.write(outfile)
print "You'll get an IOError Before this line"
outfile.close()