excel批量改名字(含识别区分)

excel批量改名字(含识别区分),第1张

该代码修改来源为基于python脚本的批量自动更改文件名,增加名字区分,仅作自用留存。

基于python脚本的批量自动更改文件名
作者为:ddatalent

1、首先先进行路径定义
#涉及的路径
root_path ='D:/AAA邮件下载'#邮件主题的文件夹,该路径不能包含文件
new_path ='D:/AAA重命名'#对下载的文件进行指定命名
fail_path='D:/AAA重命名失败'#对指定命名失败的文件的文件去处
name_list = "D:/AAA统计清单.xlsx"#名字的清单
2、这个函数是把每个人的学号和名字从excel文件里提取出来
#这个函数是把每个人的学号和名字从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的就是学号和名字
    
3、定义文件的名字函数
#定义文件的名字函数
num_name_list = excel_to_list(name_list)
count = 0; #数数一共有几份作业,看看齐了没
class_name='AAA'
words=['107-2']
4、 循环读取
for nn in num_name_list:
    num=str(nn[0])#清单内的序号
    na=nn[1]#清单内企业名字
    for file in os.listdir(root_path):
        try:
            if re.findall(str(na)[0:5],file):
               type=str(file).split('.')[-1]
               type='.'+type
               if'107-2' in file:
                   two_name=class_name+'-'+str(num)+'-'+'2'+'-'+str(na)+type
                   os.rename(os.path.join(root_path,file), os.path.join(new_path, two_name))
               elif '107-1' in file:
                   first_name=class_name+'-'+str(num)+'-'+'1'+'-'+str(na)+type
                   os.rename(os.path.join(root_path,file), os.path.join(new_path, first_name))
               else:
                   own_name=class_name+'-'+str(num)+'-'+'未区分'+'-'+str(na)+type
                   os.rename(os.path.join(root_path,file), os.path.join(new_path, own_name))
                #print("本次共计处理",count,"个文件") #看看有多少份
        except:
            continue
print('process finished,please check') 

完整代码

import os
import pandas as pd 
import re 
#涉及的路径
root_path ='D:/AAA邮件下载'#邮件主题的文件夹,该路径不能包含文件
new_path ='D:/AAA重命名'#对下载的文件进行指定命名
fail_path='D:/AAA重命名失败'#对指定命名失败的文件的文件去处
name_list = "D:/AAA统计清单.xlsx"#名字的清单

#这个函数是把每个人的学号和名字从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的就是学号和名字
    
  
#定义文件的名字函数
num_name_list = excel_to_list(name_list)
count = 0; #数数一共有几份作业,看看齐了没
class_name='AAA'
words=['107-2']


for nn in num_name_list:
    num=str(nn[0])#清单内的序号
    na=nn[1]#清单内企业名字
    for file in os.listdir(root_path):
        try:
            if re.findall(str(na)[0:5],file):
               type=str(file).split('.')[-1]
               type='.'+type
               if'107-2' in file:
                   two_name=class_name+'-'+str(num)+'-'+'2'+'-'+str(na)+type
                   os.rename(os.path.join(root_path,file), os.path.join(new_path, two_name))
               elif '107-1' in file:
                   first_name=class_name+'-'+str(num)+'-'+'1'+'-'+str(na)+type
                   os.rename(os.path.join(root_path,file), os.path.join(new_path, first_name))
               else:
                   own_name=class_name+'-'+str(num)+'-'+'未区分'+'-'+str(na)+type
                   os.rename(os.path.join(root_path,file), os.path.join(new_path, own_name))
                #print("本次共计处理",count,"个文件") #看看有多少份
        except:
            continue
print('process finished,please check') 


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/918169.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-16
下一篇2022-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存