|
| 1 | +import os |
| 2 | +import re |
| 3 | +import shutil |
| 4 | +import zipfile |
| 5 | +import pytest |
| 6 | +from docx import Document |
| 7 | + |
| 8 | +user_input = input("\nEnter search terms separated by commas (e.g. \\\sqrt, \\\pi): ") |
| 9 | +# Process the input into a list and clean up whitespace |
| 10 | +PATT_LIST = [item.strip() for item in user_input.split(",")] |
| 11 | + |
| 12 | + |
| 13 | +def test_search_docx_files_content(): |
| 14 | + base_to_dir = f"{os.getcwd()}/docx_search" |
| 15 | + |
| 16 | + # Ensure the destination directory exists |
| 17 | + if os.path.exists(base_to_dir): |
| 18 | + shutil.rmtree(base_to_dir) |
| 19 | + os.makedirs(base_to_dir) |
| 20 | + |
| 21 | + home_dir = os.path.expanduser("~") |
| 22 | + base_from_dir = f"{home_dir}/Downloads/" |
| 23 | + |
| 24 | + all_files = os.listdir(base_from_dir) |
| 25 | + |
| 26 | + files = [ |
| 27 | + x |
| 28 | + for x in all_files |
| 29 | + if x.startswith("openstax-osbooks") |
| 30 | + and x.lower().endswith(".zip") |
| 31 | + and not os.path.isdir(os.path.join(base_from_dir, x)) |
| 32 | + ] |
| 33 | + |
| 34 | + unzip_dirs = [] |
| 35 | + |
| 36 | + if len(files) > 0: |
| 37 | + for f_name in files: |
| 38 | + file_source = os.path.join(base_from_dir, f_name) |
| 39 | + file_dest = os.path.join(base_to_dir, f_name) |
| 40 | + |
| 41 | + shutil.copy(file_source, file_dest) |
| 42 | + # Use the directory containing the zip for extraction |
| 43 | + unzip_dirs.append(base_to_dir) |
| 44 | + else: |
| 45 | + pytest.fail(f"No zip files found in {base_from_dir}") |
| 46 | + |
| 47 | + # Deduplicate directory list and unzip |
| 48 | + for j in set(unzip_dirs): |
| 49 | + for file in os.listdir(j): |
| 50 | + file_path = os.path.join(j, file) |
| 51 | + if zipfile.is_zipfile(file_path): |
| 52 | + with zipfile.ZipFile(file_path) as item: |
| 53 | + # Extract into a folder named after the zip |
| 54 | + extract_path = os.path.join(j, file.replace(".zip", "")) |
| 55 | + item.extractall(extract_path) |
| 56 | + os.remove(file_path) |
| 57 | + |
| 58 | + found_anything = False |
| 59 | + |
| 60 | + # 1. Combine PATT_LIST into a single regex for speed: "term1|term2|term3" |
| 61 | + combined_pattern = "|".join(PATT_LIST) |
| 62 | + |
| 63 | + for root, _, filenames in os.walk(base_to_dir): |
| 64 | + docx_files = [ |
| 65 | + f for f in filenames if f.endswith(".docx") and not f.startswith("~$") |
| 66 | + ] |
| 67 | + |
| 68 | + for filename in docx_files: |
| 69 | + f_path = os.path.join(root, filename) |
| 70 | + doc = Document(f_path) |
| 71 | + |
| 72 | + # Check every paragraph against the combined regex |
| 73 | + for para in doc.paragraphs: |
| 74 | + match = re.search(combined_pattern, para.text) |
| 75 | + if match: |
| 76 | + found_anything = True |
| 77 | + # match.group() tells us exactly which keyword was found |
| 78 | + print( |
| 79 | + f"\nFOUND '{match.group()}': {para.text} \nLOCATION: {f_path}" |
| 80 | + ) |
| 81 | + |
| 82 | + if not found_anything: |
| 83 | + print( |
| 84 | + f"\n{'=' * 60}\nSEARCH COMPLETE: No matches found for {PATT_LIST}\n{'=' * 60}" |
| 85 | + ) |
0 commit comments