首页 > 解决方案 > PyPDF2:使用 python3 将输出写入标准输出失败

问题描述

我正在尝试使用 Python 3.7.2 和 PyPDF2 1.26 来选择输入 PDF 文件的某些页面并将输出写入标准输出(实际代码更复杂,这只是一个 MCVE):

import sys
from PyPDF2 import PdfFileReader, PdfFileWriter

input = PdfFileReader("example.pdf")
output = PdfFileWriter()
output.addPage(input.getPage(0))

output.write(sys.stdout)

这失败并出现以下错误:

UserWarning: File <<stdout>> to write to is not in binary mode. It may not be written to correctly. [pdf.py:453]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/site-packages/PyPDF2/pdf.py", line 487, in write
    stream.write(self._header + b_("\n"))
TypeError: write() argument must be str, not bytes

问题似乎sys.stdout是没有以二进制模式打开。正如一些答案所暗示的,我尝试了以下方法:

output.write(sys.stdout.buffer)

这失败并出现以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/site-packages/PyPDF2/pdf.py", line 491, in write
    object_positions.append(stream.tell())
OSError: [Errno 29] Illegal seek

我还尝试了改变标准输入/标准输出在 Python 3 中打开方式的答案:

sout = open(sys.stdout.fileno(), "wb")
output.write(sout)

这失败并出现与上述相同的错误。

如何使用 PyPDF2 库将 PDF 输出到标准输出?

更一般地说,如何正确切换sys.stdout到二进制模式(类似于 Perl 的binmode STDOUT)?

注意:无需告诉我我可以以二进制模式打开文件并将 PDF 写入该文件。这样可行; 但是,我特别想将 PDF 写入标准输出。

标签: pythonpython-3.xpypdf2

解决方案


从文档中

write(stream)

将添加到此对象的页面集合作为 PDF 文件写入。

参数: stream – 将文件写入的对象。对象必须支持write方法tell方法,类似于文件对象。

事实证明,如果不重定向到文件,则sys.stdout.buffer无法tell使用,因此您不能将其用作PdfFileWriter.write.

假设您的脚本名为myscript. 如果您调用 just myscript,则会收到此错误,但如果您将其与重定向一起使用,如下所示:

myscript > myfile.pdf

然后 Python 理解它是一个可搜索的流,你不会得到错误。


推荐阅读