合并同一目录下pdf文档

python 2.7,两个合并方法效果一样

# -*- coding: utf-8 -*-

import glob
from PyPDF2 import PdfFileWriter, PdfFileReader,PdfFileMerger


def merge(output_path,input_paths):
    pdf_writer = PdfFileWriter()
    for path in input_paths:
        pdf_reader = PdfFileReader(path)
        for page in range(pdf_reader.getNumPages()):
            pdf_writer.addPage(pdf_reader.getPage(page))
    with open(output_path, 'wb') as fh:
        pdf_writer.write(fh)


def merge2(output_path,input_paths):
    pdf_merge = PdfFileMerger()
    for path in input_paths:
        pdf_merge.append(path)
    with open(output_path,'wb') as fileObj:
        pdf_merge.write(fileObj)


if __name__ == '__main__':
    paths = glob.glob('pdf/*.pdf')
    paths.sort()
    merge2('pdf/merge2.pdf',paths)



你可能感兴趣的:(合并同一目录下pdf文档)