PyPDF 2是一个纯python PDF库,能够分割、合并、裁剪和转换PDF文件的页面。它还可以向PDF文件中添加自定义数据、查看选项和密码。它可以从PDF检索文本和元数据,还可以将整个文件合并在一起。
PyPDF 2 1.26.0文档:
https://pythonhosted.org/PyPDF2/
新建PDF1
新建PDF2
使用pip 安装pypddf2
新建pdfMerge.py
from PyPDF2 import PdfFileReader, PdfFileWriter
def merge_pdfs(paths, output):
pdf_writer = PdfFileWriter()
for path in paths:
pdf_reader = PdfFileReader(path)
for page in range(pdf_reader.getNumPages()):
# 将每页添加到writer对象
pdf_writer.addPage(pdf_reader.getPage(page))
# 写入合并的pdf
with open(output, 'wb') as out:
pdf_writer.write(out)
if __name__ == '__main__':
paths = ['1.pdf', '2.pdf']
merge_pdfs(paths, output='merged.pdf')
运行
打开输出的merge.pdf
https://download.csdn.net/download/badao_liumang_qizhi/11146143