pdf合并(python)

工具代码,背景是某门课的ppt章节分类得过于详细,比如1.01,然后一份pdf文件只有几页,故借助PyPDF2进行pdf文件合并。

python 复制代码
import os
import copy
from PyPDF2 import PdfMerger

target_path = 'D:\\study\\大四上\\生物信息\\ppt'
pdf_lst = [f for f in os.listdir(target_path) if f.endswith('.pdf')]

classified_lst = []
t = []
prefix = None
for filename in pdf_lst:
    if not filename[0].isdigit():
        continue
    if prefix is None or filename[0] == prefix:
        prefix = filename[0]
    else:
        prefix = filename[0]
        classified_lst.append(copy.deepcopy(t))     # 深浅拷贝问题
        t.clear()
    t.append(filename)
classified_lst.append(t)

fclassified_lst = []
for x in classified_lst:
    t = []
    for y in x:
        t.append(os.path.join(target_path, y))
    fclassified_lst.append(t)

# 将相同的章节合并到一个pdf中
num = 1
file_merger = PdfMerger()
for chapter in fclassified_lst:
    for pdf in chapter:
        file_merger.append(pdf)
    file_merger.write(target_path + "\\sum\\chapter" + str(num) + ".pdf")
    num += 1
    file_merger = PdfMerger()

print("done!")
相关推荐
真*小白18 分钟前
Python语法学习篇(三)【py3】
开发语言·python·学习
不一样的故事12633 分钟前
学习Python是一个循序渐进的过程,结合系统学习、持续实践和项目驱动,
开发语言·python·学习
@HNUSTer42 分钟前
Python数据可视化科技图表绘制系列教程(七)
python·数据可视化·科技论文·专业制图·科研图表
shizidushu43 分钟前
How to work with merged cells in Excel with `openpyxl` in Python?
python·microsoft·excel·openpyxl
郝学胜-神的一滴1 小时前
深入探索 Python 元组:从基础到高级应用
运维·服务器·开发语言·python·程序人生
技术程序猿华锋1 小时前
深度解码OpenAI的2025野心:Codex重生与GPT-5 APIKey获取调用示例
人工智能·vscode·python·gpt·深度学习·编辑器
xchenhao5 小时前
SciKit-Learn 全面分析分类任务 breast_cancer 数据集
python·机器学习·分类·数据集·scikit-learn·svm
独行soc8 小时前
2025年渗透测试面试题总结-66(题目+回答)
java·网络·python·安全·web安全·adb·渗透测试
Y学院10 小时前
Python 数据分析:从新手到高手的“摸鱼”指南
python·数据分析