[Python][小工具]自动分件

功能:根据EXCEL文件中录入的档号、分类号、页数等信息,对案卷目录下的图片进行分类并重命名。

分件前目录结构:

数据目录
    └───卷级目录
                       ├───0001.jpg
                       ├───0002.jpg
                       ├───0003.jpg
                       ├───……

分件后目录结构:

数据目录
    └───卷级目录
                       ├───件级目录
                       │               ├───0001.jpg
                       │               ├───0002.jpg
                       ├───件级目录
                                        ├───0001.jpg
                                        ├───……

#! C:\ProgramData\Miniconda3\
#!/usr/bin/python3
# -*- encoding: utf-8 -*-
'''
@File		:	分件.py
@Time		:	2020/06/26 15:18:58
@Author		:	GonerY
@Version	:	1.0
@Contact	:	[email protected]
@WebSite	:	https://blog.csdn.net/gonery/
'''
# Start typing your code from here
import tkinter.messagebox, tkinter.filedialog
import xlrd
import os
import sys
import shutil

# 获取目录名
DirPath = tkinter.filedialog.askdirectory(title="选择分件文件夹")
# 获取文件名
FileName = tkinter.filedialog.askopenfilename(title="选择Excel文件",
                                              filetype=[('*.xlsx', '*.xlsx'),
                                                        ('*.xls', '*.xls')])
if DirPath == "" or FileName == "":
    tkinter.messagebox.showerror("错误","请选择分件目录及EXCEL文件!")
    sys.exit(0)
DirPath = DirPath + "/"
xr = xlrd.open_workbook(FileName)
sheet1 = xr.sheet_by_name("卷内级目录数据库")
i = 1
while i < sheet1.nrows:
    rowlist = sheet1.row_values(i)
    DH = rowlist[8] + '-' + rowlist[9] + '-' + rowlist[10]
    YS = rowlist[3]
    LH = rowlist[7]
    JuanPath = DirPath + DH + '/'
    JianPath = DirPath + '分件/' + DH + '/' + LH + '/'

    if os.path.exists(JianPath):
        # 弹出异常对话框
        tkinter.messagebox.showerror("错误", DH + "\n类别号重复,请检查!")
        sys.exit(0)
    else:
        #pass
        os.makedirs(JianPath)

    i += 1
    j = 1
    while j <= int(YS):
        for pic in os.listdir(JuanPath):
            dnp = os.path.join(JuanPath, pic)  #dnp:dir and pic 图片完全路径
            nn = str(10000 + int(j))[-4:]
            #tkinter.messagebox.showinfo("",nn)
            shutil.move(dnp, JianPath + nn + '.jpg')
            j += 1
            break

# 删除分件后的空文件夹
for root, dirs, files in os.walk(DirPath):
    if not os.listdir(root):
        os.rmdir(root)

tkinter.messagebox.showinfo("信息", "分件完成。")

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章