Skip to content
This repository has been archived by the owner on May 26, 2024. It is now read-only.

Commit

Permalink
use argparse to get word
Browse files Browse the repository at this point in the history
  • Loading branch information
hirusha-adi committed Jan 20, 2024
1 parent 9bf7f3c commit f486d00
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions search.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
import os
import argparse
from docx import Document
from concurrent.futures import ThreadPoolExecutor

def search(fpath, target):
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:
print(e)
print(f"Error processing {fpath}: {e}")
return False

def process_file(file):
fname, target = file
fpath = os.path.join(os.getcwd(), fname)
if search(fpath=fpath, target=target):
print("Found in ", fname)
if check(fpath, target):
print(f"'{target}' found in {fname}")
else:
print("Not found in ", fname)
print(f"'{target}' not found in {fname}")

def main():
target = "hirusha"
all_files = [(fname, target) for fname in os.listdir() if fname.endswith(".docx")]

with ThreadPoolExecutor() as executor:
executor.map(process_file, all_files)
parser = argparse.ArgumentParser(description='Search for a word in .docx files in the current directory.')
parser.add_argument('word', type=str, help='The word to search for')
args = parser.parse_args()

target = args.word
fall = [(fname, target) for fname in os.listdir() if fname.endswith(".docx")]

with ThreadPoolExecutor() as executor:
executor.map(process_file, fall)

if __name__ == "__main__":
main()
import time
start_time = time.time()
main()
end_time = time.time()
execution_time = end_time - start_time
print(f"Execution Time: {execution_time} seconds")

0 comments on commit f486d00

Please sign in to comment.