合并PDF文件 [英] Merge PDF files

查看:144
本文介绍了合并PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个搜索,似乎没有什么东西直接关系到这个问题。是否有可能使用Python来合并单独的PDF文件?

假设如此,我需要进一步扩展。我希望循环通过目录中的文件夹并重复此过程。

我可能会推动我的运气,但可以排除PDF中包含的页面(我的报告生成总是会创建一个额外的空白页面)。 使用 > Pypdf


作为PDF工具包构建的纯Python库。它能够:

*逐页分割文档,
*逐页合并文档,

(还有更多)

使用pyPdf将两个pdf文件合并为一个文件:

 #从pyPdf导入pyPdf库
导入PdfFileWriter,PdfFileReader

#创建一个将文件附加到输出文件的例程
def append_pdf(input,output):
[output.addPage(input.getPage(page_num))for page_num in range(input.numPages)]

#创建一个对象其中pdf页面附加到
output = PdfFileWriter()

#从两个不同的文件中追加两个pdf页面
append_pdf(PdfFileReader(open(SamplePage1.pdf,输出)
append_pdf(PdfFileReader(open(SamplePage2.pdf,rb)),输出)

#将所有收集的页面写入文件
output.write(open(CombinedPages.pdf,wb))


I did a search and nothing really seemed to be directly related to this question. Is it possible, using Python, to merge seperate PDF files?

Assuming so, I need to extend this a little further. I am hoping to loop through folders in a directory and repeat this procedure.

And I may be pushing my luck, but is it possible to exclude a page that is contained in of the PDFs (my report generation always creates an extra blank page).

解决方案

Use Pypdf:

A Pure-Python library built as a PDF toolkit. It is capable of:
* splitting documents page by page,
* merging documents page by page,

(and much more)

An example of two pdf-files being merged into a single file with pyPdf:

# Loading the pyPdf Library
from pyPdf import PdfFileWriter, PdfFileReader

# Creating a routine that appends files to the output file
def append_pdf(input,output):
    [output.addPage(input.getPage(page_num)) for page_num in range(input.numPages)]

# Creating an object where pdf pages are appended to
output = PdfFileWriter()

# Appending two pdf-pages from two different files
append_pdf(PdfFileReader(open("SamplePage1.pdf","rb")),output)
append_pdf(PdfFileReader(open("SamplePage2.pdf","rb")),output)

# Writing all the collected pages to a file
output.write(open("CombinedPages.pdf","wb"))

这篇关于合并PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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