基于EXCEL的批量重命名工具
import os,pandas,tkinter,tkinter.messagebox
from tkinter import *
class AR_GUI():
def __init__(self,main_window):
self.main_window = main_window
def filePathTransform(self):
filePath=self.filePath_Entry.get()
while '\\' in filePath:
filePath = filePath[:filePath.find('\\')] + "/" + filePath[filePath.find('\\') + 1:]
self.filePath=filePath
self.set_messagebox('已设置工作文件夹:' + self.filePath)
def readFileList(self):
file_filePath_list = os.listdir(self.filePath)
data_df = pandas.DataFrame(columns=['old_name', 'old_extension', 'new_name', 'new_extension'])
for i in file_filePath_list:
temp_data_dist = {'old_name': os.path.splitext(i)[0], 'old_extension': os.path.splitext(i)[1],
'new_name': os.path.splitext(i)[0], 'new_extension': os.path.splitext(i)[1]}
temp_data_df = pandas.DataFrame(temp_data_dist, index=[0])
data_df = data_df.append(temp_data_df, ignore_index=True)
data_df.to_excel(excel_writer=self.filePath + '/rename_excel.xls')
self.set_messagebox('已读取到文件' + str(len(data_df)) + '个')
def fileRename(self):
data = pandas.read_excel(io=self.filePath + '/rename_excel.xls', dtype=str)
for i in range(len(data)):
old_name = data['old_name'].iloc[i] + data['old_extension'].iloc[i]
new_name = data['new_name'].iloc[i] + data['new_extension'].iloc[i]
os.rename(os.path.join(self.filePath, old_name), os.path.join(self.filePath, new_name))
self.set_messagebox('批量重命名成功')
def openFileFolder(self):
os.startfile(self.filePath)
def openRenameExcel(self):
os.startfile(self.filePath + '/rename_excel.xls')
def set_main_window(self):
self.main_window.title("Auto File Rename Tool (Based on Excel)")
self.tips_Label1 = Label(self.main_window, text="第一步:设置工作文件夹")
self.tips_Label1.grid(row=1,column=1)
self.filePath_Entry = Entry(self.main_window,textvariable=StringVar(value='请输入文件夹路径'))
self.filePath_Entry.grid(row=1, column=2)
self.filePath_Button = Button(self.main_window, text='确定', command=self.filePathTransform)
self.filePath_Button.grid(row=1, column=3)
self.tips_Label2 = Label(self.main_window, text="第二步:读取文件列表")
self.tips_Label2.grid(row=2, column=1)
self.readFileList_Button= Button(self.main_window,text='读取文件列表',command=self.readFileList)
self.readFileList_Button.grid(row=2, column=2)
self.tips_Label3 = Label(self.main_window, text="第三步:编辑重命名序列Excel")
self.tips_Label3.grid(row=3, column=1)
self.openRenameExcel_Button = Button(self.main_window,text='打开Excel',command=self.openRenameExcel)
self.openRenameExcel_Button.grid(row=3, column=2)
self.tips_Label4 = Label(self.main_window, text="第四步:文件重命名")
self.tips_Label4.grid(row=4, column=1)
self.fileRename_Button = Button(self.main_window, text='文件重命名',command=self.fileRename)
self.fileRename_Button.grid(row=4, column=2)
self.openFileFolder_Button = Button(self.main_window,text='打开工作文件夹',command=self.openFileFolder)
self.openFileFolder_Button.grid(row=5, column=2)
def set_messagebox(self,tips):
tkinter.messagebox.showinfo('Tips',tips)
def GUI_start():
main_window = Tk()
GUI = AR_GUI(main_window)
GUI.set_main_window()
main_window.mainloop()
GUI_start()