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
5cafa91
commit fedaa37
Showing
1 changed file
with
2 additions
and
83 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,84 +1,3 @@ | ||
import os | ||
import argparse | ||
import logging | ||
from docx import Document | ||
from concurrent.futures import ThreadPoolExecutor | ||
from datetime import datetime | ||
from docx_search import docx_search | ||
|
||
# Configure logger | ||
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(), log_file_name) | ||
|
||
logging.basicConfig(level=logging.DEBUG, format=log_format, handlers=[ | ||
logging.FileHandler(log_file_path, encoding='utf-8'), | ||
logging.StreamHandler() | ||
]) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def check(fpath, target): | ||
""" | ||
Check if the target word is present in the paragraphs of a given Word document. | ||
@param fpath: The file path of the Word document. | ||
@type fpath: str | ||
@param target: The word to search for in the document. | ||
@type target: str | ||
@return: True if the word is found, False otherwise. | ||
@rtype: bool | ||
""" | ||
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): | ||
""" | ||
Process a Word document file, checking if a target word is present. | ||
@param file: A tuple containing the file name and the target word. | ||
@type file: tuple | ||
@return: None | ||
@rtype: None | ||
""" | ||
fname, target = file | ||
fpath = os.path.join(os.getcwd(), fname) | ||
if check(fpath, target): | ||
logger.info("'%s' found in %s" % (target, fname)) | ||
else: | ||
logger.debug("'%s' not found in %s" % (target, fname)) | ||
|
||
def main(): | ||
""" | ||
Main function to search for a word in .docx files in the current directory. | ||
@return: None | ||
@rtype: None | ||
""" | ||
parser = argparse.ArgumentParser(description='Search for a word in .docx files in the current directory.') | ||
parser.add_argument('word', nargs='?', type=str, help='The word to search for (optional if not provided, will prompt user)') | ||
args = parser.parse_args() | ||
|
||
if args.word is None: | ||
target = input("Enter the word to search for: ") | ||
else: | ||
target = args.word | ||
|
||
file_list = [(fname, target) for fname in os.listdir() if fname.endswith(".docx")] | ||
|
||
with ThreadPoolExecutor() as executor: | ||
executor.map(process_file, file_list) # හිරුෂඅදිකාරී | ||
|
||
if __name__ == "__main__": | ||
import time | ||
start_time = time.time() | ||
main() | ||
end_time = time.time() | ||
execution_time = end_time - start_time | ||
logger.debug("Execution Time: %s seconds" % execution_time) | ||
docx_search() |