weixin_46492767 2022-12-09 12:37 采纳率: 50%
浏览 67
已结题

关于#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 的格式,麻烦直接修改一份新的代码写在回答里

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-12-09 13:14
    关注

    很多小问题,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()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月10日
  • 已采纳回答 12月9日
  • 创建了问题 12月9日

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题