关于#Pythonl#的问题,如何解决?



import os
import xlrd
def listdir(path, list_name):  # 传入存储的list
    for file in os.listdir(path):
        # 排除临时的文件
        if '~$' in file:
            continue
        # 取得照片清单
        if ".jpg" in file:
            file_path = os.path.join(path, file)
            list_name.append(file_path)
    print(list_name)
def getinfo(new_name):  # 获取人员姓名和编号
    date = xlrd.open_workbook('百佳超市-反馈记录.xlsx')
    sheet = date.sheets()[0]
    nrows = sheet.nrows
    ncols = sheet.ncols
    # 查找姓名和编号的列
    empl_name = ""
    empl_numb = ""
    ename_col = 0
    enumb_col = 0
    ephoto_col = 0
    print("最大列数--->" + str(ncols))
    for col in range(0, ncols + 1):
        if sheet.cell_value(0,col) == "您的上半身照片?(只需上传一张)":
            ephoto_col = col
            print("图片的列--->" + str(col))
        if sheet.cell_value(0,col) == "您的姓名?":
            ename_col = col
            print("名字的列--->" + str(col))
        if sheet.cell_value(0,col) == "您的手机号码?":
            enumb_col = col
            print("手机号的列-->"+ str(col))
    # 取行中的姓名和编号
    for row in range(1, nrows + 1):
        empl_name = str(sheet.cell_value(row,ename_col))
        empl_numb = str(sheet.cell_value(row,enumb_col))
        global empl_name
        global empl_numb
        empl_photo = str(sheet.cell_value(row,ephoto_col))
        file_name = (empl_photo).split('.')[0]  # 新的名字
        print(file_name)
        new_name.append(file_name)
    print(new_name)
def change_name(file_path, new_name, list_name):
    # 逐个处理照片
    for filename in list_name:
        print("旧文件名" + filename)
        old_name = (os.path.basename(filename)).split('.')[0]
        # 查找新名字清单中是否有此姓名
        for nfile in new_name:
            if old_name in nfile:
                nfname = empl_name+'-'+empl_numb  +".jpg"
                print("新文件名" + nfname)
                os.rename(filename, nfname)
                break
def main():
    file_path = input('输入文件夹路径:')  # 文件夹位置
    try:
        # 读取文件夹下的所有文件
        List_files = []
        index_file = listdir(file_path, List_files)
        # 读取员工姓名和员工号,组成新的文件名
        new_name = []
        getinfo(new_name)
        # 修改文件名字
        change_name(file_path, new_name, List_files)
    except Exception as e:
        # 打印异常信息
        print(e)
if __name__ == '__main__':
    main()

麻烦看看这个程序哪里有问题
注:这个是一个批量改文件名称的程序,文件的旧的名字,在excel的G列,如图:

img

文件在一个文件夹中,都是.jpg和.jpeg的格式,如图:

img

需要把这些名字都改成 和excel里对应的 名字-电话号码.jpg 的格式,麻烦直接修改一份新的代码写在回答里

很多小问题,getinfo会出错,empl_name和empl_numb全局变量应该放到最上面申明,要么就不要不要申明为全局的。下标也越界了
xlrd读取xls,读取不了xlsx。xlsx用openpyxl来读。

将xlsx存储为xls后用下面测试是正常的(注意要将手机列数据格式改为文本,要不是数字,手机号很长会变为科学计数法

import os
import xlrd

kvOldNew={}##旧新文件字典

def listdir(path):
    for file in os.listdir(path):
        if '~$' in file:
            continue
        if ".jpg" in file:
            kvOldNew[file]=""
def getinfo():  # 获取人员姓名和编号
    date = xlrd.open_workbook('百佳超市-反馈记录.xls')
    sheet = date.sheets()[0]
    nrows = sheet.nrows
    ncols = sheet.ncols
    ename_col = 0
    enumb_col = 0
    ephoto_col = 0
    print("最大列数--->" + str(ncols))
    for col in range(0, ncols):# + 1):###这里不需要+1,越界了
        if sheet.cell_value(0,col) == "您的上半身照片?(只需上传一张)":
            ephoto_col = col
            print("图片的列--->" + str(col))
        if sheet.cell_value(0,col) == "您的姓名?":
            ename_col = col
            print("名字的列--->" + str(col))
        if sheet.cell_value(0,col) == "您的手机号码?":
            enumb_col = col
            print("手机号的列-->"+ str(col))
    for row in range(1, nrows):# + 1):##这里同理,不要+1,越界了
        empl_name = str(sheet.cell_value(row,ename_col))
        empl_numb = str(int(sheet.cell_value(row,enumb_col)))#转为数字去掉0,再转字符串,设置excel数字为文本也没用。。
        empl_photo = str(sheet.cell_value(row,ephoto_col))
 
        #更新字典旧图片键值为新图片名
        if empl_photo in kvOldNew:
            kvOldNew[empl_photo]=empl_name+'-'+empl_numb  +".jpg"
def main():
    file_path = input('输入文件夹路径:')
    listdir(file_path)
    getinfo()
    #直接遍历字典换名即可
    #print(kvOldNew)
    for key,value in kvOldNew.items():
        if value=="":#这里判断代码执行出错,已经命名过一些文件,再次执行getinfo无法设置键值,则继续
            continue
        oldfile=os.path.join(file_path,key)
        newfile=os.path.join(file_path,value)
        if not os.path.exists(newfile):#新文件不存在再命名
            print(f"{oldfile}==>{newfile}")
            os.rename(oldfile, newfile)
if __name__ == '__main__':
    main()
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632