python - 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 写入标准输出。
解决方案
从文档中:
write(stream)
将添加到此对象的页面集合作为 PDF 文件写入。
参数:
stream
– 将文件写入的对象。对象必须支持write
方法和tell
方法,类似于文件对象。
事实证明,如果不重定向到文件,则sys.stdout.buffer
无法tell
使用,因此您不能将其用作PdfFileWriter.write
.
假设您的脚本名为myscript
. 如果您调用 just myscript
,则会收到此错误,但如果您将其与重定向一起使用,如下所示:
myscript > myfile.pdf
然后 Python 理解它是一个可搜索的流,你不会得到错误。
推荐阅读
- python - Re-Naming Files as they are being opened in Python For Loop
- django - DJANGO 1.11 - Can't find fixtures
- java - 如何在分布式环境中创建 64 位数字 ID
- php - PHP getElementsByTagName('*') 避免重复节点 | “在文字广告中”通过分隔内容节点
- deep-learning - Transformer - Attention is all you need - 编码器解码器交叉注意力
- javascript - 多个版本的 html2canvas
- java - 何以java(spring boot)从SAP b1获取最近添加的销售订单单据号
- python - 从包含字典列表的系列创建熊猫数据框
- ruby - 如何通过换行解析文本?
- python - ArgumentError:无法在表上创建 ForeignKeyConstraint