PyPDF2-无法合并来自两个不同PDF文件的页面 [英] PyPDF2 - merging pages from two different PDF files is not working
问题描述
我正在尝试将两个PDF文件中的页面合并为具有单个页面的单个PDF.所以我尝试了下面使用PyPDF2的代码:
from PyPDF2 import PdfFileReader,PdfFileWriter
import sys
f = sys.argv[1]
k = sys.argv[2]
print f,k
file1 = PdfFileReader(file(f, "rb"))
file2 = PdfFileReader(file(k, "rb"))
output = PdfFileWriter()
page = file1.getPage(0)
page.mergePage(file2.getPage(0))
output.addPage(page)
outputStream = file("join.pdf", "wb")
output.write(outputStream)
outputStream.close()
它会生成一个文件和一个页面,其中包含文件1中页面1的内容,但是我找不到文件2中页面1的任何数据.似乎没有合并.
使用完全相同的代码,我可以在一个页面中将两个PDF
合并为PDF
,第二个页面与第一个页面重叠,我引用了这个链接以获得详细信息.
而且,按照 我希望这会有所帮助. 更新: 这是对我有用的代码,并将两个pdf页面合并为一个页面. 我添加了一个新代码,现在它也正在旋转最终的pdf.您需要的输出PDF是 这是 Google云端硬盘链接到我的驱动器中,用于 I'm trying to merge pages from two PDF files into a single PDF with a single page. So I tried the code below that uses PyPDF2: It produces a single file and single page with the contents of page 1 from file 1, but I don't find any data from page 1 of file2. Seems like it didn't get merged. On using your exact same code, I am able to get two And, instead of Also, I made slight changes in your code but still, the working is same and correct on my machine. I am using Here is the code: I hope this helps. UPDATE: Here is the code which is working for me and merging the two pdf's page as single page. I have added a new code and now it is also rotating the final pdf. Output PDF that you need is And here is the Google Drive link to my drive for 这篇关于PyPDF2-无法合并来自两个不同PDF文件的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
from pyPdf import PdfFileWriter, PdfFileReader
from pdfnup import generateNup
initial_output = PdfFileWriter()
input1 = PdfFileReader(open("landscape1.pdf", "rb"))
input2 = PdfFileReader(open("landscape2.pdf", "rb"))
initial_output.addPage(input1.getPage(0))
initial_output.addPage(input2.getPage(0))
# creates a new pdf file with required pages as separate pages.
initial_output.write(file("final.pdf", "wb"))
# merges newly created pdf file pages as one.
generateNup("final.pdf", 2, "intermediate.pdf")
# overwrite and rotates the final.pdf
final_output = PdfFileWriter()
final_output.addPage(PdfFileReader(open("intermediate.pdf", "rb")).getPage(0).rotateClockwise(90))
final_output.write(open("final.pdf", "wb"))
final.pdf
PDF
文件.此外,如果要使用相同的文件,则对pdfnup.py
进行了细微更改,以与我的系统兼容,如果要使用同一文件,也可以在上面的驱动器链接中找到它.from PyPDF2 import PdfFileReader,PdfFileWriter
import sys
f = sys.argv[1]
k = sys.argv[2]
print f,k
file1 = PdfFileReader(file(f, "rb"))
file2 = PdfFileReader(file(k, "rb"))
output = PdfFileWriter()
page = file1.getPage(0)
page.mergePage(file2.getPage(0))
output.addPage(page)
outputStream = file("join.pdf", "wb")
output.write(outputStream)
outputStream.close()
PDF
as merged PDF
in one page with the second one overlapping the first one, I referred this link for detailed information.file()
it is better to use open()
as per this Python Documentation, so I did that.Ubuntu 16.04
with python 2.7
.from PyPDF2 import PdfFileReader,PdfFileWriter
import sys
f = sys.argv[1]
k = sys.argv[2]
print f, k
file1 = PdfFileReader(open(f, "rb"))
file2 = PdfFileReader(open(k, "rb"))
output = PdfFileWriter()
page = file1.getPage(0)
page.mergePage(file2.getPage(0))
output.addPage(page)
with open("join.pdf", "wb") as outputStream:
output.write(outputStream)
from pyPdf import PdfFileWriter, PdfFileReader
from pdfnup import generateNup
initial_output = PdfFileWriter()
input1 = PdfFileReader(open("landscape1.pdf", "rb"))
input2 = PdfFileReader(open("landscape2.pdf", "rb"))
initial_output.addPage(input1.getPage(0))
initial_output.addPage(input2.getPage(0))
# creates a new pdf file with required pages as separate pages.
initial_output.write(file("final.pdf", "wb"))
# merges newly created pdf file pages as one.
generateNup("final.pdf", 2, "intermediate.pdf")
# overwrite and rotates the final.pdf
final_output = PdfFileWriter()
final_output.addPage(PdfFileReader(open("intermediate.pdf", "rb")).getPage(0).rotateClockwise(90))
final_output.write(open("final.pdf", "wb"))
final.pdf
PDF
files. Also, I made slight changes into pdfnup.py
for compatibility with my system for Immutableset
if you want to use the same file then, you can find it too in the drive link above.