1.需求如下:
像这样的:第三列是下载链接,第二列是所对应的名字。
需求是批量下载的视频根据这个xlsx的表进行批量修改视频名字。
2.思路:
把第三列作为键,第二列作为值,生成字典,根据字典查询,找到就os修改名字。
3.代码:
import os
import xlrd
def re_name():
src = r"C:\Users\Administrator\Desktop\电影视频_\已转换\视频_新汇总.xlsx"
book = xlrd.open_workbook(src)
# 找到sheet页
table = book.sheet_by_name("Sheet1")
keys = table.col_values(2) # 这是第3列数据,作为字典的key值
vals = table.col_values(1) # 作为值
keyt = []
for key in keys:
str = key.split("/")[-1]
keyt.append(str)
dic = dict(zip(keyt, vals))
print(dic)
# 开始遍历文件
path = r"C:\TempD\DemoVideo\video" # 视频下载的文件夹
dirs = os.listdir(path)
count = 0
for file in dirs:
for key in dic:
# if file == "assets_"+key + ".mp4":
# if file == key + ".mp4":
if file == key:
print("找到有同名--"+file)
try:
os.rename(path+"/"+file, path+"/"+dic[key]+".mp4")
except Exception as e:
pass
if __name__ == "__main__":
re_name()
4.备注:
转载请说明出处!!!