-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNfoGeneratingAndRenameByExcel.py
More file actions
250 lines (199 loc) · 9.6 KB
/
NfoGeneratingAndRenameByExcel.py
File metadata and controls
250 lines (199 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# 请帮我写个中文的 Python 脚本,批注也是中文,但是变量参数不要是中文:
# 在脚本开始前询问我 excel 文件位置(默认为:d:\Works\Attachments\标准.xlsx)、写入文件夹位置(默认为:d:\Works\Ins)。
# 重命名即生成 nfo 文件:读取 excel 文件,第一行为表头(字段名)。此后每一行为一条记录。"原文件名"字段值对应着写入文件夹中到每一个文件名字。依次根据每一行到"原文件名"找到写入文件夹中到每一个文件,将其改名为同一行中的“现文件名”。根据“现文件名”生成同名“*.nfo”文件。nfo 文件内写入的内容:UTF-8编码,<?xml version="1.0" encoding="UTF-8" standalone="yes"?><movie> <title> </title></movie>,将该行中的“名字”字段填入“<title> </title>”内的“ ”。
# 完成后,反复循环。
# 导入模块
import os
import pandas as pd
import xml.etree.ElementTree as ET
from pathlib import Path
# ==================== 全局配置 ====================
DEFAULT_EXCEL_PATH = r"d:\Works\Attachments\标准.xlsx"
DEFAULT_WRITE_DIR = r"d:\Works\Ins"
# ==================== 辅助函数 ====================
def get_input_with_default(prompt_text, default_value):
"""获取带默认值的用户输入"""
user_input = input(f"{prompt_text} (默认: {default_value}): ").strip()
return user_input if user_input else default_value
def ensure_directory_exists(directory_path):
"""确保目录存在,如果不存在则创建"""
if not os.path.exists(directory_path):
os.makedirs(directory_path, exist_ok=True)
print(f"已创建目录: {directory_path}")
return directory_path
def find_file_in_directory(directory_path, filename):
"""在目录中查找文件,支持子目录查找"""
matches = []
for root, dirs, files in os.walk(directory_path):
for file in files:
if file == filename:
matches.append(os.path.join(root, file))
return matches
def create_nfo_file(filepath, title):
"""创建NFO文件"""
try:
# 创建XML结构
movie_elem = ET.Element("movie")
title_elem = ET.SubElement(movie_elem, "title")
title_elem.text = title
# 转换为XML字符串
xml_str = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n'
# 手动格式化,因为ElementTree的tostring没有格式化选项
xml_str += '<movie>\n'
xml_str += f' <title>{title}</title>\n'
xml_str += '</movie>'
# 写入文件
with open(filepath, 'w', encoding='utf-8') as f:
f.write(xml_str)
return True
except Exception as e:
print(f"创建NFO文件失败 {filepath}: {e}")
return False
# ==================== 主要功能 ====================
def rename_and_generate_nfo(excel_path, write_dir):
"""
重命名文件并生成NFO文件
参数:
excel_path: Excel文件路径
write_dir: 写入文件夹路径
"""
print("\n=== 开始重命名并生成NFO文件 ===")
# 检查Excel文件是否存在
if not os.path.exists(excel_path):
print(f"错误: Excel文件不存在 - {excel_path}")
return False
# 检查写入文件夹是否存在
if not os.path.exists(write_dir):
print(f"错误: 写入文件夹不存在 - {write_dir}")
return False
# 读取Excel文件
try:
df = pd.read_excel(excel_path, engine='openpyxl')
except Exception as e:
print(f"读取Excel文件失败: {e}")
return False
# 检查必要的字段是否存在
required_columns = ["原文件名", "现文件名", "名字"]
missing_columns = [col for col in required_columns if col not in df.columns]
if missing_columns:
print(f"错误: Excel文件中缺少以下必要字段: {', '.join(missing_columns)}")
return False
# 处理每一行
processed_count = 0
rename_failures = 0
nfo_failures = 0
for index, row in df.iterrows():
try:
# 获取字段值
original_filename = row.get("原文件名", "")
new_filename = row.get("现文件名", "")
name_value = row.get("名字", "")
# 跳过空值
if (pd.isna(original_filename) or pd.isna(new_filename) or
pd.isna(name_value) or
not original_filename or not new_filename or not name_value):
continue
# 转换为字符串并去除空白
original_filename = str(original_filename).strip()
new_filename = str(new_filename).strip()
name_value = str(name_value).strip()
# 如果原文件名和现文件名相同,跳过重命名
if original_filename == new_filename:
print(f"跳过第 {index+1} 行: 原文件名和现文件名相同")
# 仍然生成NFO文件
target_file = None
# 查找文件
found_files = find_file_in_directory(write_dir, original_filename)
if found_files:
target_file = found_files[0]
# 生成NFO文件
nfo_filename = os.path.splitext(original_filename)[0] + ".nfo"
nfo_filepath = os.path.join(os.path.dirname(target_file), nfo_filename)
if create_nfo_file(nfo_filepath, name_value):
print(f"已生成NFO文件: {nfo_filename}")
processed_count += 1
else:
nfo_failures += 1
else:
print(f"未找到文件: {original_filename}")
continue
# 查找源文件
found_files = find_file_in_directory(write_dir, original_filename)
if not found_files:
print(f"未找到文件: {original_filename}")
continue
if len(found_files) > 1:
print(f"找到多个同名文件,使用第一个: {original_filename}")
source_file = found_files[0]
# 重命名文件
try:
# 获取源文件目录
source_dir = os.path.dirname(source_file)
# 构建新文件路径
new_filepath = os.path.join(source_dir, new_filename)
# 检查新文件是否已存在
if os.path.exists(new_filepath):
print(f"目标文件已存在,跳过重命名: {new_filename}")
target_file = new_filepath
else:
# 重命名文件
os.rename(source_file, new_filepath)
print(f"已重命名: {original_filename} -> {new_filename}")
target_file = new_filepath
# 生成NFO文件
nfo_filename = os.path.splitext(new_filename)[0] + ".nfo"
nfo_filepath = os.path.join(source_dir, nfo_filename)
if create_nfo_file(nfo_filepath, name_value):
print(f"已生成NFO文件: {nfo_filename}")
processed_count += 1
else:
nfo_failures += 1
except Exception as e:
print(f"重命名文件失败 {original_filename}: {e}")
rename_failures += 1
except Exception as e:
print(f"处理第 {index+1} 行时出错: {e}")
continue
# 输出处理结果
print(f"\n处理完成!")
print(f"成功处理: {processed_count} 个文件")
print(f"重命名失败: {rename_failures} 个文件")
print(f"NFO文件生成失败: {nfo_failures} 个文件")
return processed_count > 0
# ==================== 主程序 ====================
def main():
"""主程序(循环执行,每次结束后自动回到配置输入,无需询问是否继续)"""
print("=" * 60)
print("重命名并生成NFO文件脚本")
print("功能: 根据Excel文件重命名文件并生成对应的NFO文件")
print("提示: 如需退出程序,请按 Ctrl+C 或在Excel路径输入 'exit' 或 'quit' 后回车")
print("=" * 60)
while True:
print("\n" + "=" * 60)
print("[配置参数]")
# 获取用户输入
excel_path = get_input_with_default("请输入Excel文件位置", DEFAULT_EXCEL_PATH)
# 检查退出指令
if excel_path.lower() in ("exit", "quit"):
print("程序退出。")
break
write_dir = get_input_with_default("请输入写入文件夹位置", DEFAULT_WRITE_DIR)
if write_dir.lower() in ("exit", "quit"):
print("程序退出。")
break
# 执行主要功能
success = rename_and_generate_nfo(excel_path, write_dir)
if success:
print("操作成功完成!")
else:
print("操作失败或未处理任何文件!")
# 不询问是否继续,自动进入下一轮循环
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\n程序被用户中断")
input("\n按回车键退出程序...")
except Exception as e:
print(f"\n程序运行出错: {e}")
input("\n按回车键退出程序...")