python实现PDF文件合并成一个文件

使用python将几个pdf文件合并成一个文件,需要使用PyPDF2包。
代码如下:

!/usr/bin/python
  
import os
from PyPDF2 import PdfFileMerger

target_path = './'

pdf_lst = [f for f in os.listdir(target_path) if f.endswith('.pdf')]
pdf_lst = [os.path.join(target_path, filename) for filename in pdf_lst]

pdf_lst.sort()
file_merger = PdfFileMerger()

for pdf in pdf_lst:
        print pdf
        file_merger.append(pdf)

file_merger.write("./merge.pdf")

猜你喜欢

转载自blog.csdn.net/yangcunbiao/article/details/125248205