python重命名(从excel中读取命名数据)

根据姓名读取学号并重命名

import xlrd
import xlwt
import os

path='.\\'
data = xlrd.open_workbook('data.xlsx')
table = data.sheet_by_name(data.sheet_names()[0])

for file in os.listdir(path):
#判断是否是文件
    if os.path.isfile(os.path.join(path,file))==True:
        if '.doc' in file and '_222' not in file:
            for i in range(1,len(table.col_values(0))):
                if table.col_values(2)[i] in file:
                    num = str(table.col_values(1)[i])[:-2]
                    break
                else:
                    num = '0'
            if num != '0':
                temp = file.split('.')
                new_name = temp[0] + '_' + num + '.' + temp[1]
                os.rename(os.path.join(path,file),os.path.join(path,new_name))
    原文作者:weixin_43964993
    原文地址: https://blog.csdn.net/weixin_43964993/article/details/111700428
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞