PYPDF水印返回错误 [英] PYPDF watermarking returns error

查看:179
本文介绍了PYPDF水印返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我收到此错误,但我无法尝试使用pypdf2给pdf文件加水印.

hi im trying to watermark a pdf fileusing pypdf2 though i get this error i cant figure out what goes wrong.

我收到以下错误:

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位上使用python 2.7.6和pypdf2 1.19. 希望有人可以告诉我我做错了.

using python 2.7.6 with pypdf2 1.19 on windows 32bit. hopefully someone can tell me what i do wrong.

我的python文件:

my python file:

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()

推荐答案

尝试写入StringIO对象而不是磁盘文件.因此,替换为:

Try writing to a StringIO object instead of a disk file. So, replace this:

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 我的文章中的工作示例.

If above code works, then you might be having file writing permission issues. For reference, look at the PyPDF working example in my article.

这篇关于PYPDF水印返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆