This repository has been archived by the owner on May 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bf7f3c
commit f486d00
Showing
1 changed file
with
23 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|