我正在尝试在pdf文件中旋转页面,然后用SAME pdf文件中的旋转页面替换旧页面。
我写了以下代码:
#!/usr/bin/python
import os
from pyPdf import PdfFileReader, PdfFileWriter
my_path = "/home/USER/Desktop/files/"
input_file_name = os.path.join(my_path, "myfile.pdf")
input_file = PdfFileReader(file(input_file_name, "rb"))
input_file.decrypt("MyPassword")
output_PDF = PdfFileWriter()
for num_page in range(0, input_file.getNumPages()):
page = input_file.getPage(num_page)
page.rotateClockwise(270)
output_PDF.addPage(page)
#Trying to replace old data with new data in the original file, not
#create a new file and add the new data!
output_file_name = os.path.join(my_path, "myfile.pdf")
output_file = file(output_file_name, "wb")
output_PDF.write(output_file)
output_file.close()
上面的代码给了我一个错误!我甚至尝试过使用:
input_file = PdfFileReader(file(input_file_name, "r+b"))
但它也不起作用......
更改行:
output_file_name = os.path.join(my_path, "myfile.pdf")
使用:
output_file_name = os.path.join(my_path, "myfile2.pdf")
修复了一切,但这不是我想要的......
任何帮助?
错误代码:
回溯(最近一次呼叫最后):文件“12-5.py”,第22行,中 output_PDF.write(output_file)文件“/usr/lib/pymodules/python2.7/pyPdf/pdf.py”,第264行,写入 self._sweepIndirectReferences(externalReferenceMap,self._root)文件“/usr/lib/pymodules/python2.7/pyPdf/pdf.py”,第339行,in _sweepIndirectReferences self._sweepIndirectReferences(externMap,realdata)文件“/usr/lib/pymodules/python2.7/pyPdf/pdf.py”,第315行, _sweepIndirectReferences value = self._sweepIndirectReferences(externMap,value)文件“/usr/lib/pymodules/python2.7/pyPdf/pdf.py”,第339行,in _sweepIndirectReferences self._sweepIndirectReferences(externMap,realdata)文件“/usr/lib/pymodules/python2.7/pyPdf/pdf.py”,第315行, _sweepIndirectReferences value = self._sweepIndirectReferences(externMap,value)文件“/usr/lib/pymodules/python2.7/pyPdf/pdf.py”,第324行,in _sweepIndirectReferences value = self._sweepIndirectReferences(externMap,data [i])File“/usr/lib/pymodules/python2.7/pyPdf/pdf.py”,第339行,in _sweepIndirectReferences self._sweepIndirectReferences(externMap,realdata)文件“/usr/lib/pymodules/python2.7/pyPdf/pdf.py”,第315行, _sweepIndirectReferences value = self._sweepIndirectReferences(externMap,value)文件“/usr/lib/pymodules/python2.7/pyPdf/pdf.py”,第324行,in _sweepIndirectReferences value = self._sweepIndirectReferences(externMap,data [i])File“/usr/lib/pymodules/python2.7/pyPdf/pdf.py”,第345行,in _sweepIndirectReferences newobj = data.pdf.getObject(data)文件“/usr/lib/pymodules/python2.7/pyPdf/pdf.py”,第649行,在getObject中 retval = readObject(self.stream,self)文件“/usr/lib/pymodules/python2.7/pyPdf/generic.py”,第67行,in 的readObject return DictionaryObject.readFromStream(stream,pdf)File“/usr/lib/pymodules/python2.7/pyPdf/generic.py”,第564行,in readFromStream raise utils.PdfReadError,“无法在流后找到'endstream'标记。” pyPdf.utils.PdfReadError:无法找到'endstream'标记 流后。
答案 0 :(得分:1)
我怀疑这个问题是PyPDF在写入文件时正在读取文件。
正确的修复 - 正如您所注意到的 - 是写入单独的文件,然后用新文件替换原始文件。像这样:
output_file_name = os.path.join(my_path, "myfile-temporary.pdf")
output_file = file(output_file_name, "wb")
output_PDF.write(output_file)
output_file.close()
os.rename(output_file_name, input_file_name)
我已经编写了一些简化此代码的代码:https://github.com/shazow/unstdlib.py/blob/master/unstdlib/standard/contextlib_.py#L14
from unstdlib.standard.contextlib_ import open_atomic
with open_atomic(input_file_name, "wb") as output_file:
output_PDF.write(output_file)
这将自动创建一个临时文件,写入它,然后替换原始文件。
编辑:我最初误读了这个问题。以下是我的错误,但对其他人的回答可能有帮助。
您的代码很好,并且应该在"大多数" PDF文件。
您遇到的问题是PyPDF与您尝试使用的特定PDF之间不兼容。这可能是PyPDF中的错误,也可能是PDF不完全有效。
你可以尝试两件事:
查看PyPDF2是否可以读取该文件。使用pip install PyPDF2
安装PyPDF2,将import pyPdf …
替换为import PyPDF2 …
,然后重新运行脚本。
使用其他程序重新编码PDF,看看是否有效。例如,使用convert bad.pdf bad.ps; convert bad.ps maybe-good.pdf
之类的东西可以修复问题。