嗨,我试图水印一个pdf文件使用pypdf2,虽然我得到这个错误,我不知道哪里出了问题。
我得到以下错误:
Traceback (most recent call last): File "test.py", line 13, in <module>
page.mergePage(watermark.getPage(0)) File "C:\Python27\site-packages\PyPDF2\pdf.py", line 1594, in mergePage
self._mergePage(page2) File "C:\Python27\site-packages\PyPDF2\pdf.py", line 1651, in _mergePage
page2Content, rename, self.pdf) File "C:Python27\site-packages\PyPDF2\pdf.py", line 1547, in
_contentStreamRename
op = operands[i] KeyError: 0在windows 32位上使用python2.7.6和pypdf2 1.19。希望有人能告诉我我做错了什么。
我的python文件:
from PyPDF2 import PdfFileWriter, PdfFileReader
output = PdfFileWriter()
input = PdfFileReader(open("test.pdf", "rb"))
watermark = PdfFileReader(open("watermark.pdf", "rb"))
# print how many pages input1 has:
print("test.pdf has %d pages." % input.getNumPages())
print("watermark.pdf has %d pages." % watermark.getNumPages())
# add page 0 from input, but first add a watermark from another PDF:
page = input.getPage(0)
page.mergePage(watermark.getPage(0))
output.addPage(page)
# finally, write "output" to document-output.pdf
outputStream = file("outputs.pdf", "wb")
output.write(outputStream)
outputStream.close()发布于 2013-11-28 09:18:49
尝试写入StringIO对象而不是磁盘文件。因此,将其替换为:
outputStream = file("outputs.pdf", "wb")
output.write(outputStream)
outputStream.close()在这方面:
outputStream = StringIO.StringIO()
output.write(outputStream) #write merged output to the StringIO object
outputStream.close()如果上面的代码有效,那么您可能会遇到文件写入权限问题。为了参考,请查看PyPDF 我的文章中的工作示例。
发布于 2015-04-25 17:23:14
当我试图使用PyPDF2在报告室生成的页面中合并时,我遇到了这个错误,该页面使用内联图像canvas.drawInlineImage(...),它将图像存储在PDF的对象流中。对图像使用类似技术的其他PDF也可能以同样的方式受到影响--实际上,PDF的内容流有一个数据对象抛入其中,而并不期望它。
如果可以的话,一个解决方案可以是重新生成源pdf,但不要使用内联内容流存储的图像,例如在reportlab中用canvas.drawImage(...)生成。
这是这方面的问题 on PyPDF2。
https://stackoverflow.com/questions/20221991
复制相似问题