代碼功能:獲取Excel指定內容用於重新命名文件。
# 導入庫 import xlrd import os # 指定文件路徑 path = '/Users/luoxiaolei/Desktop/test' fileList=os.listdir(path) n=0 # 遍歷目錄所有文件 for i in fileList: # 設置舊文件名(就是路徑+文件名) oldname=path+ os.sep + fileList[n] # os.sep添加系統分隔符 # 獲取文件對象 xlsx = xlrd.open_workbook(oldname) # 獲取第一個sheet對象 sheet = xlsx.sheets()[0] # 獲取第3行C列內容 gjbh = sheet.row(2)[2].value # 獲取第3行D列內容 gjmc = sheet.row(2)[4].value num = n+1 # 設置新的文件名稱 newname = path + os.sep +gjbh+'_No'+str(num)+'.xlsx' # 修改文件名 os.rename(oldname,newname) print(oldname,'======>',newname) n+=1