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!")
相关推荐
赵民勇25 分钟前
Python 协程详解与技巧总结
python
极光代码工作室36 分钟前
基于YOLO目标检测的智能监控系统
python·深度学习·yolo·机器学习·计算机视觉
江华森1 小时前
Python 进阶编程实战 — 从多版本环境到百万级登录系统
python
C+-C资深大佬1 小时前
python while循环
服务器·开发语言·python
zh路西法2 小时前
【现代控制理论与卡尔曼滤波】从状态空间到Python仿真实现
开发语言·python
Vodka~3 小时前
WSL2 + RViz GPU渲染机械臂
人工智能·python
8Qi83 小时前
hello-agents学习笔记--Memory让Agent拥有记忆
人工智能·python·llm·agent·ai编程·vibecoding
Esaka_Forever4 小时前
Python 完整内存管理机制详解
开发语言·python·spring
Weigang4 小时前
用 LlamaIndex 做 RAG 前,先把 Reader、Index、Retriever 的边界写清楚
人工智能·python·开源