[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("信息", "分件完成。")

 

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