Python实现多个pdf文件合并

今天想把多个PDF文件合并为一个文件,网上的软件都是收费的,于是想到万能的python,代码分析如下;

#!/user/bin/env python
#-*-coding: utf-8-*-
#@Time        : 2020/10/7 000722:29
#@Author      : GodSpeed
#@File        : pdf合并.py
#@Software    : PyCharm

# pip3 install PyPDF2

# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyPDF2

import os
from PyPDF2 import PdfFileMerger

target_path = r'C:\Users\Administrator\Pictures\PDF原图'
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]

file_merger = PdfFileMerger()
for pdf in pdf_lst:
    file_merger.append(pdf)     # 合并pdf文件

file_merger.write(r"C:\Users\Administrator\Pictures\PDF原图\merge.pdf")


猜你喜欢

转载自blog.csdn.net/Narutolxy/article/details/108956894