如何在python中将两个pdf页面缝合为一个

如何在python中将两个pdf页面缝合为一个,python,pdf,pypdf,Python,Pdf,Pypdf,我正在使用python,我想将两个PDF页面合并成一个页面。我的目的是将这两个页面合并为一个页面,而不是两个PDF。有没有办法将这两个PDF逐一合并?我不想把这两者合并。如果没有重叠,有没有办法将它们结合起来 使用模块PyPDF2() 例如: from PyPDF2 import PdfFileMerger pdf_list = ['/path/to/first.pdf', '/path/to/second.pdf'] merger = PdfFileMerger() for i in pdf

我正在使用python,我想将两个PDF页面合并成一个页面。我的目的是将这两个页面合并为一个页面,而不是两个PDF。有没有办法将这两个PDF逐一合并?我不想把这两者合并。如果没有重叠,有没有办法将它们结合起来

使用模块PyPDF2()

例如:

from PyPDF2 import PdfFileMerger
pdf_list = ['/path/to/first.pdf', '/path/to/second.pdf']
merger = PdfFileMerger()

for i in pdf_list:
    merger.append(open(i, 'rb'))

with open('/path/to/save/new.pdf', 'wb') as fout:
    merger.write(fout)

如果我理解正确,您希望以这种方式缝合两页:

---------
|   |   |
| 1 | 2 |
|   |   |
---------
pyPDF3模块允许您这样做

从PyPDF3导入PdfileWriter、PdfileReader
从PyPDF3.pdf导入页面对象
pdf_filenames=[“out_mitry.pdf”,“out_cdg.pdf”]
input1=PdfileReader(打开(pdf_文件名[0],“rb”),strict=False)
input2=PdfileReader(打开(pdf文件名[1],“rb”),strict=False)
page1=input1.getPage(0)
page2=input2.getPage(0)
总宽度=page1.mediaBox.upperRight[0]+page2.mediaBox.upperRight[0]
总高度=最大值([page1.mediaBox.upperRight[1],page2.mediaBox.upperRight[1]))
新建页面=PageObject.CreateBankPage(无、总宽度、总高度)
#在0,0位置添加第一页
新建页面。合并页面(第1页)
#添加沿x轴移动的第二页
新建页面.mergeTranslatedPage(第2页,第1页,mediaBox.upperRight[0],0)
输出=PdfileWriter()
output.addPage(新页面)
output.write(打开(“result.pdf”、“wb”))

你看到了吗?我只有一份pdf。我需要把一个pdf的两页合并成一页,你说把两页pdf合并成一页是什么意思?!我能理解你为什么要这么做的一个原因,那就是如果你有一页空白。我还没有尝试过,但我认为如果您使用io导入字节io的
,然后读取PDF的内容,将其附加到列表中,然后使用
PdfWriter()写入字节,则可以合并内容