from io import ByteIO
# e.g. writer = PdfWriter()
# ... do what you want to do with the PDFs
with BytesIO() as bytes_stream:
writer.write(bytes_stream)
bytes_stream.seek(0)
data = bytes_stream.read() # that is now the "bytes" represention
import requests
import PyPDF2
from io import BytesIO
def merge_pdfs_remotely(urls, output_filename):
# Create a list of file-like objects from the URLs
file_streams = [BytesIO(requests.get(url).content) for url in urls]
# Create the PDF merger object
merger = PyPDF2.PdfFileMerger()
# Add each PDF file to the merger
for stream in file_streams:
merger.append(PyPDF2.PdfFileReader(stream))
2条答案
按热度按时间uqjltbpv1#
请转到
pypdf
,它本质上与PyPDF2
相同,但开发将在那里继续(我是这两个项目的维护者)。您的问题已在文档中得到解答:
不是写入文件,而是写入
io.ByteIO
流:z9zf31ra2#
要合并PDF文件而不将其保存在本地,可以使用requests库下载每个文件的内容,然后将内容传递给PyPDF2库中的PdfFileReader类。