excel中为各班级学生的信息,每个班级为一个sheet;
一级文件下有班级子文件,班级文件下为图片,即要修改名字的文件(其他文件类似)
在这里插入代码片
import os
import xlrd
count =0
first_path = "C:\\Users\\Administrator.DESKTOP-L29CIOJ\\Desktop\\班级"
expath = "C:\\Users\\Administrator.DESKTOP-L29CIOJ\\Desktop\\学生名册.xls"
x1 = xlrd.open_workbook(expath)
sheets=x1.sheet_names()
print(sheets)
idlist=[]
for sheet in sheets:
print(sheet)
print(x1.sheet_by_name(sheet).col_values(0))
idlist.extend(x1.sheet_by_name(sheet).col_values(0))
print(idlist)
first_list = os.listdir(first_path)
for first_files in first_list:
second_path = os.path.join(first_path, first_files)
second_list = os.listdir(second_path)
for second_files in second_list:
pictures_path=os.path.join(second_path,second_files)
print(pictures_path)
print(idlist[count])
os.renames(pictures_path, os.path.join(second_path, str(idlist[count]) + " " + ".jpg"))
count = count +1