Skip to content

Commit

Permalink
create GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
hirusha-adi committed Jan 24, 2024
1 parent c466b8f commit f54cb59
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 114 deletions.
99 changes: 0 additions & 99 deletions docx_search.py

This file was deleted.

122 changes: 107 additions & 15 deletions search.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,116 @@
import argparse
import os
from docx_search import docx_search
import logging
from docx import Document
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
import tkinter as tk
from tkinter import filedialog

# Configure logger
if not os.path.isdir('logs'):
os.mkdir('logs')

def main():
parser = argparse.ArgumentParser(description='Search for a word in .docx files in a specified directory.')
parser.add_argument('--dir', dest='target_dir', type=str, default=os.getcwd(),
help='The target directory to search for .docx files. Defaults to the current working directory.')
parser.add_argument('--word', dest='target_word', type=str,
help='The word to search for in the documents. If not provided, the user will be prompted.')
args = parser.parse_args()
log_format = '(%(asctime)s) [%(levelname)s] %(message)s'
log_file_name = datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + '.log'
log_file_path = os.path.join(os.getcwd(), "logs", log_file_name)

target_dir = args.target_dir
target_word = args.target_word
logging.basicConfig(level=logging.DEBUG, format=log_format, handlers=[
logging.FileHandler(log_file_path, encoding='utf-8'),
logging.StreamHandler()
])

if not target_word:
target_word = input("Enter the word to search for: ")
logger = logging.getLogger(__name__)

docx_search(target_dir=target_dir, target_word=target_word)
def __check(fpath, target):
try:
doc = Document(fpath)
for paragraph in doc.paragraphs:
if target in paragraph.text:
return True
return False
except Exception as e:
logger.error("Error processing %s: %s" % (fpath, e))
return False

def __process_file(file):
fname, target = file
fpath = os.path.join(os.getcwd(), fname)
if __check(fpath, target):
logger.info("'%s' found in %s" % (target, fname))
return fname
else:
logger.debug("'%s' not found in %s" % (target, fname))
return None

def perform_search(target_dir=None, target_word=None):
if target_word is None or target_word == '':
raise ValueError("target_word cannot be None or an empty string. Please pass in a valid value.")

if target_dir is None:
target_dir = os.getcwd()

file_list = [(fname, target_word) for fname in os.listdir(target_dir) if fname.endswith(".docx")]

results = []
with ThreadPoolExecutor() as executor:
results = list(executor.map(__process_file, file_list))

return [result for result in results if result is not None]

class GUIFrontend:
def __init__(self, master):
self.master = master
self.master.title("Document Search App")

self.target_word_label = tk.Label(master, text="Target Word:")
self.target_word_label.pack()

self.target_word_entry = tk.Entry(master)
self.target_word_entry.pack()

self.browse_button = tk.Button(master, text="Browse", command=self.browse_directory)
self.browse_button.pack()

self.search_button = tk.Button(master, text="Search", command=self.search_documents)
self.search_button.pack()

self.result_listbox = tk.Listbox(master)
self.result_listbox.pack()
self.result_listbox.bind("<Double-Button-1>", self.open_file)

# Initialize target_directory
self.target_directory = None

def browse_directory(self):
directory = filedialog.askdirectory()
if directory:
self.target_directory = directory

def search_documents(self):
target_word = self.target_word_entry.get()
if not target_word:
tk.messagebox.showerror("Error", "Target word cannot be empty.")
return

if self.target_directory is None:
self.target_directory = os.getcwd()

results = perform_search(target_dir=self.target_directory, target_word=target_word)
self.update_result_list(results)

def update_result_list(self, results):
self.result_listbox.delete(0, tk.END)
for result in results:
self.result_listbox.insert(tk.END, result)

def open_file(self, event):
selected_item = self.result_listbox.curselection()
if selected_item:
file_name = self.result_listbox.get(selected_item)
file_path = os.path.join(self.target_directory, file_name)
os.startfile(file_path)

if __name__ == "__main__":
main()
root = tk.Tk()
app = GUIFrontend(root)
root.mainloop()

0 comments on commit f54cb59

Please sign in to comment.