forked from victorchall/EveryDream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_replace_tool.py
76 lines (56 loc) · 2.76 KB
/
text_replace_tool.py
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
import os
import re
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Text Replace")
self.geometry("350x200")
self.create_widgets()
def create_widgets(self):
self.directory_label = tk.Label(self, text="Directory:")
self.directory_label.grid(row=0, column=0, padx=5, pady=5)
self.directory_entry = tk.Entry(self, width=30)
self.directory_entry.grid(row=0, column=1, padx=5, pady=5)
self.browse_button = tk.Button(self, text="Browse", command=self.browse_directory)
self.browse_button.grid(row=0, column=2, padx=5, pady=5)
self.find_label = tk.Label(self, text="Find:")
self.find_label.grid(row=1, column=0, padx=5, pady=5)
self.find_entry = tk.Entry(self, width=30)
self.find_entry.grid(row=1, column=1, padx=5, pady=5)
self.replace_label = tk.Label(self, text="Replace:")
self.replace_label.grid(row=2, column=0, padx=5, pady=5)
self.replace_entry = tk.Entry(self, width=30)
self.replace_entry.grid(row=2, column=1, padx=5, pady=5)
self.rename_button = tk.Button(self, text="Rename", command=self.rename_files)
self.rename_button.grid(row=3, column=1, padx=5, pady=5)
self.progress = ttk.Progressbar(self, orient=tk.HORIZONTAL, length=200, mode='determinate')
self.progress.grid(row=4, column=0, columnspan=3, padx=5, pady=5)
def browse_directory(self):
directory = filedialog.askdirectory()
self.directory_entry.delete(0, tk.END)
self.directory_entry.insert(0, directory)
def rename_files(self):
directory = self.directory_entry.get()
find_text = self.find_entry.get()
replace_text = self.replace_entry.get()
if not all((directory, find_text, replace_text)):
messagebox.showwarning("Warning", "Please fill in all fields.")
return
text_files = [f for f in os.listdir(directory) if f.endswith('.txt')]
total_files = len(text_files)
self.progress['maximum'] = total_files
for i, filename in enumerate(text_files, start=1):
file_path = os.path.join(directory, filename)
with open(file_path, 'r') as file:
file_contents = file.read()
new_contents = re.sub(find_text, replace_text, file_contents)
with open(file_path, 'w') as file:
file.write(new_contents)
self.progress['value'] = i
self.update_idletasks()
messagebox.showinfo("Done", "Text replacement completed.")
if __name__ == "__main__":
app = App()
app.mainloop()