|
import os
|
|
import shutil
|
|
import tkinter as tk
|
|
from tkinter import filedialog, messagebox
|
|
|
|
def process_files(words, output_directory):
|
|
|
|
output_filename = "_".join(words) + ".txt"
|
|
|
|
|
|
if output_filename.endswith("_.txt"):
|
|
output_filename = output_filename[:-5] + ".txt"
|
|
|
|
|
|
if not os.path.exists(output_directory):
|
|
os.makedirs(output_directory)
|
|
|
|
|
|
temp_output_filename = "temp_" + output_filename
|
|
with open(temp_output_filename, 'w', encoding='utf-8') as outfile:
|
|
|
|
for filename in os.listdir('.'):
|
|
|
|
if filename.endswith('.txt') and filename != temp_output_filename:
|
|
try:
|
|
with open(filename, 'r', encoding='utf-8') as infile:
|
|
|
|
for line in infile:
|
|
|
|
if all(word in line for word in words):
|
|
|
|
outfile.write(line)
|
|
except Exception as e:
|
|
print(f"無法讀取檔案 {filename}: {e}")
|
|
|
|
|
|
final_output_path = os.path.join(output_directory, output_filename)
|
|
shutil.move(temp_output_filename, final_output_path)
|
|
|
|
messagebox.showinfo("完成", f"符合條件的行已寫入檔案 {final_output_path}")
|
|
|
|
def on_process():
|
|
input_words = input_entry.get()
|
|
words = [word.strip() for word in input_words.split(",")]
|
|
|
|
|
|
if len(words) > 10:
|
|
messagebox.showerror("錯誤", "輸入的單詞不能超過十個")
|
|
return
|
|
|
|
output_directory = output_dir_entry.get().strip()
|
|
|
|
|
|
if not output_directory:
|
|
output_directory = r"I:\sd-forge\extensions\sd-dynamic-prompts\wildcards"
|
|
|
|
process_files(words, output_directory)
|
|
|
|
def browse_directory():
|
|
directory = filedialog.askdirectory()
|
|
if directory:
|
|
output_dir_entry.delete(0, tk.END)
|
|
output_dir_entry.insert(0, directory)
|
|
|
|
|
|
root = tk.Tk()
|
|
root.title("詞彙檢測工具")
|
|
|
|
|
|
tk.Label(root, text="請輸入一組單詞(最多十個單詞,用逗號分隔):").pack(pady=5)
|
|
input_entry = tk.Entry(root, width=50)
|
|
input_entry.pack(pady=5)
|
|
|
|
|
|
tk.Label(root, text="請輸入輸出目錄(如果不存在,將會自動創建):").pack(pady=5)
|
|
output_dir_entry = tk.Entry(root, width=50)
|
|
output_dir_entry.pack(pady=5)
|
|
|
|
|
|
browse_button = tk.Button(root, text="瀏覽...", command=browse_directory)
|
|
browse_button.pack(pady=5)
|
|
|
|
|
|
process_button = tk.Button(root, text="開始處理", command=on_process)
|
|
process_button.pack(pady=20)
|
|
|
|
|
|
root.mainloop()
|
|
|