如何使用PyPDF2附加PDF页面 [英] How to append PDF pages using PyPDF2

查看:344
本文介绍了如何使用PyPDF2附加PDF页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人有使用python lib PyPDF2将两页PDF文件合并为一个文件的经验. 当我尝试page1.mergePage(page2)时,导致page2覆盖page1.如何将page2添加到page1的底部?

Is anybody has experience merging two page of PDF file into one using python lib PyPDF2. When I try page1.mergePage(page2) it results with page2 overlayed page1. How to make it to add page2 to the bottom of the page1?

推荐答案

当我在网上搜索python pdf合并解决方案时,我注意到对合并与追加存在普遍的误解.

As I'm searching the web for python pdf merging solution, I noticed that there's a general misconception with merging versus appending.

大多数人将追加操作称为合并,但并非如此.您在问题中描述的实际上是mergePage 的预期用途,应该被称为applyPageOnTopOfAnother,但这有点长.您正在寻找的实际上是附加两个文件/pages转换为新文件.

Most people call the appending action a merge but it's not. What you're describing in your question is really the intended use of mergePage which should be called applyPageOnTopOfAnother but that's a little long. What you are (were) looking for is really appending two files/pages into a new file.

使用PdfFileMerger类及其 append方法

Using the PdfFileMerger class and its append method.

merge() 方法相同,但假定您想要连接 所有页面都放在文件末尾,而不是指定位置.

Identical to the merge() method, but assumes you want to concatenate all pages onto the end of the file instead of specifying a position.

这是从 pypdf将多个pdf文件合并为一个pdf的一种方法:

from PyPDF2 import PdfFileMerger, PdfFileReader

# ...

merger = PdfFileMerger()

merger.append(PdfFileReader(file(filename1, 'rb')))
merger.append(PdfFileReader(file(filename2, 'rb')))

merger.write("document-output.pdf")

附加特定的PDF页面

要附加不同PDF文件的特定页面,请将PdfFileWriter类与

Appending specific PDF pages

And to append specific pages of different PDF files, use the PdfFileWriter class with the addPage method.

将页面添加到此PDF文件.该页面通常是从 PdfFileReader 实例.

Adds a page to this PDF file. The page is usually acquired from a PdfFileReader instance.

file1 = PdfFileReader(file(filename1, "rb"))
file2 = PdfFileReader(file(filename2, "rb"))

output = PdfFileWriter()

output.addPage(file1.getPage(specificPageIndex))
output.addPage(file2.getPage(specificPageIndex))

outputStream = file("document-output.pdf", "wb")
output.write(outputStream)
outputStream.close()

将两页合并为一页

使用 mergePage

将两个页面的内容流合并为一个.资源参考 (即字体)在两个页面中都得到维护. mediabox/cropbox/etc 此页面的内容未更改.参数页面的内容流将 被添加到此页面内容流的末尾,这意味着它 将会在此页面之后或在顶部" 绘制.

Merges the content streams of two pages into one. Resource references (i.e. fonts) are maintained from both pages. The mediabox/cropbox/etc of this page are not altered. The parameter page’s content stream will be added to the end of this page’s content stream, meaning that it will be drawn after, or "on top" of this page.

file1 = PdfFileReader(file(filename1, "rb"))
file2 = PdfFileReader(file(filename2, "rb"))

output = PdfFileWriter()

page = file1.getPage(specificPageIndex)
page.mergePage(file2.getPage(specificPageIndex))

output.addPage(page)

outputStream = file("document-output.pdf", "wb")
output.write(outputStream)
outputStream.close()

这篇关于如何使用PyPDF2附加PDF页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆