Paper_Tips,用python读取PDF标题,并归类

好久没更新了, 主要是懒, 以后尽量坚持定期更新吧, 一周一更, 也算是对自己的激励与要求.

背景

最近整理ICPR2018大会的论文, 发现论文名称都是数字编号, 而且都在一个文件夹里, 这看着头都大了, 想找一片感兴趣的文章要打开, 看标题, 如果不感兴趣, 还要接着重复打开, 关闭. 麻烦. 于是查资料, 学习并捣鼓些小脚本, 用于读取PDF信息和归类.

环境

python3.5.2
需要安装: PyPDF2

sudo pip install PyPDF2

读取PDF中的标题信息(复制操作)

import os
from PyPDF2 import PdfFileWriter, PdfFileReader
from shutil import copy2

src_dir = 'papers_oldDir'               #源文件目录地址
des_dir = 'papers_newDir'               #新文件目录地址
num = 0

if not os.path.exists(des_dir):		#如果没有目标文件夹,新建一个目标文件夹进行存储
    os.makedirs(des_dir)

if os.path.exists(src_dir):
    dirs = os.listdir(src_dir)          #获取源文件的目录地址
    for dirc in dirs:                   #对于目录下的每一个文件
        pdf_reader = PdfFileReader(open(os.path.join(src_dir, dirc), 'rb'))      #打开并建立一个PDF文件对象
        paper_title = pdf_reader.getDocumentInfo().title                         #获取PDF标题
        print("num : %s" % num , paper_title)                                    #终端显示处理到第几个文件
        num += 1
        paper_title = str(paper_title)                                           #标题字符化
        
        if paper_title.find('/') != -1:       #对于'/'无法写入文件名的情况,将其用'_'代替
            new_paper_title = paper_title.replace('/','_')
            paper_title = new_paper_title
            copy2(os.path.join(src_dir, dirc), os.path.join(des_dir, paper_title) + '.pdf')
        else:
            copy2(os.path.join(src_dir, dirc), os.path.join(des_dir, paper_title) + '.pdf')
        
else:
    print("该路径下不存在所查找的目录!")

对文件名进行归类(剪贴操作)

import os
import shutil

face_dir = '人脸检测'
file_dir = 'papers_new'

if not os.path.exists(face_dir):
	os.makedirs(face_dir)

for root, dirs, files in os.walk(face_dir):
	print(root)             #查看根目录
	print(dirs)             #查看包含的文件夹名称
	for file in files:      #对于目录下的每一个文件
		file_name = str(file)       #取文件名
		oldAddre = file_dir + '/' + file_name       #获取文件源地址
		newAddre = text_dir + '/' + file_name       #获取文件转移地址
		
		if file_name.find('face') != -1:            #当发现匹配字符时,对文件进行转移
			shutil.move(oldAddre, newAddre)

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