有没有办法在Python 2.x中将二进制输出写入sys.stdout?在Python 3.x中,您可以使用sys.stdout.buffer(或分离stdout等等),但我无法找到任何Python 2.5/2.6的解决方案.
编辑,解决方案:来自ChristopheD的链接,如下:
import sys
if sys.platform == "win32":
    import os, msvcrt
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
编辑:我正在尝试将PDF文件(二进制形式)推送到stdout以便在Web服务器上提供服务.当我尝试使用sys.stdout.write编写文件时,它会将各种回车符添加到二进制流中,导致PDF呈现损坏.
编辑2:对于这个项目,遗憾的是我需要在Windows Server上运行,因此Linux解决方案已经完成.
Simply Dummy示例(从磁盘上的文件读取,而不是动态生成,只是因为我们知道生成代码不是问题):
file = open('C:\\test.pdf','rb') 
pdfFile = file.read() 
sys.stdout.write(pdfFile)
我正在使用 Python 3.8.5。我正在尝试编写一个简短的脚本来连接 PDF 文件并从这个 Stack Overflow 问题中学习,我正在尝试使用PyPDF2. 不幸的是,我似乎无法在PyPDF2.PdfFileReader不崩溃的情况下创建实例。
我的代码如下所示:
import pathlib
import PyPDF2
pdf_path = pathlib.Path('1.pdf')
with pdf_path.open('rb') as pdf_file:
    reader = PyPDF2.PdfFileReader(pdf_file, strict=False)
当我尝试运行它时,我得到以下回溯:
Traceback (most recent call last):
  File "C:\...\pdf\open_pdf.py", line 6, in <module>
    reader = PyPDF2.PdfFileReader(pdf_file, strict=False)
  File "C:\...\.virtualenvs\pdf-j0HnXL2B\lib\site-packages\PyPDF2\pdf.py", line 1084, in __init__
    self.read(stream)
  File "C:\...\.virtualenvs\pdf-j0HnXL2B\lib\site-packages\PyPDF2\pdf.py", line 1883, in read
    stream.seek(-11, 1)
OSError: [Errno 22] Invalid argument
为了帮助重现该问题,我使用上述代码和示例 PDF 文件创建了此 GitHub存储库。
我究竟做错了什么?
在python 3中,我有一个各种格式的图像列表(pdf、png、jpg、gif),我将它们全部合并到一个多页pdf中。
使用PyPDF2,可以合并 PDF 文件。但不支持 png、jpg 等。此处对此进行了很好的介绍:
合并 PDF 文件
使用img2pdf,可以将 png、jpg 等图像类型转换为 PDF 并进行合并。但是,它不支持输入 PDF 文件。此处:
从图像列表创建 PDF
因此,由于我可以将 PDF、PNG、JPG 作为输入,因此我习惯这样处理它:
from PyPDF2 import PdfFileMerger
import img2pdf
if not ext == 'pdf':
    with open("output.pdf", "wb") as f:
        f.write(img2pdf.convert(images))
else:
    merger = PdfFileMerger()
    for pdf in images:    
        merger.append(pdf)
    merger.write("output.pdf")
问题是:我是否需要这 2 个库来将一系列图像(包括 PDF)合并到一个 PDF 中?换句话说,是否有一个库可以将任何图像(包括 PDF)作为输入,并将它们全部合并为一个 PDF?
我正在使用 python,我想将两个 PDF 页面合并为一个页面。我的目的是将这两页合并为一个而不是两个 PDF。有什么办法可以将两个PDF一张一张合并起来吗?我不想合并这两个。在不重叠的情况下,有什么办法可以将它们结合起来吗?
(刮互联网网站转换成PDF类的一部分)应该合并使用pypdf网页生成的PDF文件可以不明白这了这个功能.
这是方法代码:
def mergePdf(self,mainname,inputlist=0):
    """merging the pdf pages
    getting an inputlist to merge or defaults to the class instance self.pdftomerge list"""
    from pyPdf import PdfFileWriter, PdfFileReader
    self._mergelist = inputlist or self.pdftomerge
    self.pdfoutput = PdfFileWriter()
    for name in self._mergelist:
        print "merging %s into main pdf file: %s" % (name,mainname)
        self._filestream = file(name,"rb")
        self.pdfinput = PdfFileReader(self._filestream)
        for p in self.pdfinput.pages:
            self.pdfoutput.addPage(p)
        self._filestream.close()
    self._pdfstream = file(mainname,"wb")
    self._pdfstream.open()
    self.pdfoutput.write(self._pdfstream)
    self._pdfstream.close()
我一直收到这个错误:
  File "c:\tmp\easy_install-iik9vj\pyPdf-1.13-py2.7-win32.egg.tmp\pyPdf\pdf.py", line 264, in write
    self._sweepIndirectReferences(externalReferenceMap, self._root)
  File "c:\tmp\easy_install-iik9vj\pyPdf-1.13-py2.7-win32.egg.tmp\pyPdf\pdf.py", line 339, in …我有一个dataframe
              a            b   c
0   2610.101010 13151.030303   33.000000
1   1119.459459 5624.216216    65.777778
2   3584.000000 18005.333333    3.000000
3   1227.272727 5303.272727    29.333333
4   1661.156504 8558.836558   499.666667
我正在使用绘制直方图,plotly.express并且还describe使用以下简单代码打印表格:
import plotly.express as px
for col in df.columns:
    px.histogram(df, x=col, title=col).show()
    print(df[col].describe().T)
是否可以在每个直方图旁边添加describe并将所有图(及其各自的直方图)保存在单个 pdf 中?