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!")
相关推荐
我材不敲代码2 小时前
Python实现打包贪吃蛇游戏
开发语言·python·游戏
0思必得04 小时前
[Web自动化] Selenium处理动态网页
前端·爬虫·python·selenium·自动化
韩立学长4 小时前
【开题答辩实录分享】以《基于Python的大学超市仓储信息管理系统的设计与实现》为例进行选题答辩实录分享
开发语言·python
qq_192779874 小时前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
u0109272714 小时前
使用Plotly创建交互式图表
jvm·数据库·python
爱学习的阿磊4 小时前
Python GUI开发:Tkinter入门教程
jvm·数据库·python
Imm7775 小时前
中国知名的车膜品牌推荐几家
人工智能·python
tudficdew5 小时前
实战:用Python分析某电商销售数据
jvm·数据库·python
sjjhd6525 小时前
Python日志记录(Logging)最佳实践
jvm·数据库·python
2301_821369616 小时前
用Python生成艺术:分形与算法绘图
jvm·数据库·python