xlsx批量重命名
仅作学习记录
import os
import pandas as pd
import re
#这个函数是把每个人的学号和名字从excel文件里提取出来,以便下面改名
def excel_to_list(name_list):
df = pd.read_excel(name_list, usecols=[0,2],names=None)
#读取项目名称列
#usecols=[1,2]表明取第二列和第三列,也就是学号和名字。
#names=None表明不要列名
df_li = df.values.tolist()
#print(df_li)
return df_li #return的就是学号和名字
if __name__ == '__main__':
name_list = "企业清单.xlsx" #花名册地址
path = 'Z:\\邮件\\'
path1 = 'Z:\\邮件已修改\\'
num_name_list = excel_to_list(name_list)
count = 0;
class_name='类别'
for file in os.listdir(path):
try:
type = str(file).split('.')[-1]
type = '.' + type
print("检测到此次类型为",type,"类型")
except:
type = None
print("检测到此次类型为文件夹类型")
ini_taskname = str(file).split('.')[0]
# words = ['107-2']
for nn in num_name_list:
num = str(nn[0])
na = nn[1]
try:
if re.findall(str(na)[0:7],ini_taskname):
count = count+1;
if type is None:
os.rename(os.path.join(path,file),os.path.join(path1,str(num)+'-'+class_name+'-'+str(na)+type))
else:
os.rename(os.path.join(path,file),os.path.join(path1,str(num)+'-'+class_name+'-'+str(na)+type))
except:
continue
print("本次共计处理",count,"个文件")
print('process finished,please check')