import os import re import shutil import tkinter as tk from tkinter import filedialog, messagebox import pyperclip def process_files(files, positive_words, negative_words, output_directory): # 生成輸出檔名,移除非法字元 safe_name = "_".join([re.sub(r'[\\/*?:"<>|]', '', w.strip()) for w in positive_words if w.strip()]) output_filename = f"{safe_name}.txt" # 建立輸出目錄 os.makedirs(output_directory, exist_ok=True) temp_output_filename = "temp_" + output_filename # 累積符合行 with open(temp_output_filename, 'w', encoding='utf-8') as outfile: for filename in files: try: with open(filename, 'r', encoding='utf-8') as infile: for line in infile: line_stripped = line.strip() if not line_stripped: continue # 符合所有正面詞且不含反面詞 if all(word in line_stripped for word in positive_words) and not any(word in line_stripped for word in negative_words): outfile.write(line) except Exception as e: print(f"無法讀取檔案 {filename}: {e}") # 移動到目標資料夾 final_output_path = os.path.join(output_directory, output_filename) try: if os.path.exists(final_output_path): os.remove(final_output_path) shutil.move(temp_output_filename, final_output_path) except Exception as e: messagebox.showerror("錯誤", f"移動檔案時發生錯誤: {e}") return # 嘗試複製結果路徑到剪貼簿 try: pyperclip.copy(final_output_path) except Exception: print("無法複製路徑到剪貼簿。") messagebox.showinfo("完成", f"結果已輸出至:\n{final_output_path}\n路徑已複製至剪貼簿。") def on_process(): positive_input = positive_entry.get().strip() positive_words = [w.strip() for w in positive_input.split(",") if w.strip()] if not positive_words: messagebox.showerror("錯誤", "必須輸入至少一個正面單詞。") return if len(positive_words) > 10: messagebox.showerror("錯誤", "正面單詞不能超過十個。") return negative_input = negative_entry.get().strip() negative_words = [w.strip() for w in negative_input.split(",") if w.strip()] files_input = files_entry.get().strip() files = [f.strip() for f in files_input.split(",") if f.strip()] if not files: files = [f for f in os.listdir('.') if f.endswith('.txt')] output_dir = output_dir_entry.get().strip() if not output_dir: output_dir = r"I:\stable-diffusion-webui-reForge\extensions\sd-dynamic-prompts\wildcards" process_files(files, positive_words, negative_words, output_dir) def browse_files(): paths = filedialog.askopenfilenames(filetypes=[("Text files", "*.txt")]) if paths: files_entry.delete(0, tk.END) files_entry.insert(0, ",".join(paths)) def browse_directory(): path = filedialog.askdirectory() if path: output_dir_entry.delete(0, tk.END) output_dir_entry.insert(0, path) # GUI 介面 root = tk.Tk() root.title("詞彙檢測工具") tk.Label(root, text="正面單詞 (最多10個, 以逗號分隔):").pack(pady=5) positive_entry = tk.Entry(root, width=60) positive_entry.pack(pady=3) tk.Label(root, text="反面單詞 (可選, 以逗號分隔):").pack(pady=5) negative_entry = tk.Entry(root, width=60) negative_entry.pack(pady=3) tk.Label(root, text="輸入檔案 (可多個, 以逗號分隔):").pack(pady=5) files_entry = tk.Entry(root, width=60) files_entry.pack(pady=3) tk.Button(root, text="選擇檔案", command=browse_files).pack(pady=3) tk.Label(root, text="輸出目錄 (可留空自動建立):").pack(pady=5) output_dir_entry = tk.Entry(root, width=60) output_dir_entry.pack(pady=3) tk.Button(root, text="選擇資料夾", command=browse_directory).pack(pady=3) tk.Button(root, text="開始處理", command=on_process).pack(pady=10) root.mainloop()