Python课程作品设计——基于EXCEL的批量重命名工具

基于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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HerbertHu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值