多PDF合并

用PyPDF,里面有现成的工具,需要提前下载包(pip install PyPDF2)
python代码

import os
from PyPDF2 import PdfMerger


def merge_pdf(pdf_path):
    pdfs = [f for f in os.listdir(pdf_path) if f.endswith('.pdf')]
    pdfs.sort()
    pdfs = [os.path.join(pdf_path, pdf_name) for pdf_name in pdfs]
    merger = PdfMerger()
    for pdf in pdfs:
        with open(pdf, 'rb') as tmp:
            merger.append(tmp)
    with open('res.pdf', 'wb') as res:
        merger.write(res)


if __name__ == '__main__':
    path = "D:\Dqt'workplace\研一课程\最优化-2023"  # 合并pdf文件夹路径
    merge_pdf(path)

你可能感兴趣的:(pdf,python)