# encoding: utf-8 2 import os 3 import sys 4 5 import openpyxl 6 import tkinter as tk 7 from tkinter import filedialog 8 import tkinter as tk 9 from tkinter import filedialog 10 import tkinter.messagebox as msgbox 11 import tkinter as tk, time 12 13 count = 1 14 def t(pa,expa,she): 15 print(f'\'{pa}\'') 16 print(f'\'{expa}\'') 17 print(f'\'{she}\'') 18 path = f'{pa}'#文件所在文件夹 19 expath =f'{expa}'#名单所在文件夹 20 x1 = openpyxl.load_workbook(expath)#读取excel 21 22 sheet1 = x1[f'{she}']#读取sheet1 23 24 xwsr = 2#修改 25 filelist = os.listdir(path)#读取文件目录 26 for files in filelist:#遍历文件目录 27 xr = sheet1.cell(row=xwsr, column=3)#可变 28 xc = sheet1.cell(row=xwsr, column=4)#可变 29 #可填加 30 Olddir = os.path.join(path,files)#旧的文件位置 31 #os.renames(Olddir,os.path.join(path,str(xylist[count]+".jpg"))#新的文件位置 32 os.rename(Olddir,os.path.join(path,str(xr.value+xc.value)+".jpeg")) #str里可加 33 xwsr = xwsr+1 34 filelist1 = os.listdir(path)#读取文件目录 35 filelist1.sort() 36 sys.exit() 37 def xuan(): 38 '''打开选择文件夹对话框''' 39 root = tk.Tk() 40 root.withdraw() 41 msgbox.showinfo('提示','选择要修改命名的图片的存放文件夹') 42 Folderpath = filedialog.askdirectory() #获得选择好的文件夹 43 msgbox.showinfo('提示','选择要腾讯表格') 44 Filepath = filedialog.askopenfilename() #获得选择好的文件 45 msgbox.showinfo('提示','输入sheet1的内容') 46 window = tk.Tk() 47 window.title("输入") 48 e = tk.Entry(window) 49 e.pack() 50 def get(): 51 str =e.get() 52 print(f'\'{Folderpath}\'') 53 print('Folderpath:', Folderpath) 54 print('Folderpath:', Filepath) 55 window.destroy() 56 root.destroy() 57 t(Folderpath,Filepath,str) 58 b = tk.Button(window, text="确认", command=get) 59 b.pack() 60 window.mainloop() 61 xuan()
根据自己需要,可以添加多条信息进行重命名。可在
xr = sheet1.cell(row=xwsr, column=3)#可变 xc = sheet1.cell(row=xwsr, column=4)#可变 后面添加同样的内容。row,无需改变,column为列。添加后,在
os.rename(Olddir,os.path.join(path,str(xr.value+xc.value)+".jpeg")) 在
str(xr.value+xc.value)中添加你刚才添加的变量的值 例子:
xr = sheet1.cell(row=xwsr, column=3)#可变 xc = sheet1.cell(row=xwsr, column=4)#可变 xb =sheet1.cell(row=xwsr, column=5)#可变 则:
os.rename(Olddir,os.path.join(path,str(xr.value+xc.value+xb.value)+".jpeg")) 即可实现图片重命名。 *说明:缺点:未能实现点击tk窗体右上角的关闭时,无法真正关闭,一直运行。只有选择全部文件和内容的时候,可以完全退出