| import os |
| import pypdf |
|
|
| def search_word_in_folder(folder_path, search_word): |
| """ |
| Searches for a word across all PDF files in a given folder. |
| """ |
| |
| search_word_lower = search_word.lower() |
| |
| |
| if not os.path.exists(folder_path): |
| print(f"❌ Error: The folder '{folder_path}' does not exist.") |
| return |
|
|
| print(f"Scanning folder '{folder_path}' for '{search_word}'...\n") |
| overall_matches = 0 |
|
|
| |
| for filename in os.listdir(folder_path): |
| |
| if filename.lower().endswith('.pdf'): |
| pdf_path = os.path.join(folder_path, filename) |
| |
| try: |
| with open(pdf_path, 'rb') as file: |
| reader = pypdf.PdfReader(file) |
| file_has_matches = False |
| |
| |
| for page_num, page in enumerate(reader.pages, start=1): |
| text = page.extract_text() |
| |
| if text and search_word_lower in text.lower(): |
| |
| print(f"✅ Found in [{filename}] on Page {page_num}") |
| file_has_matches = True |
| overall_matches += 1 |
| |
| except Exception as e: |
| print(f"⚠️ Could not read file {filename}: {e}") |
| |
| if overall_matches == 0: |
| print("❌ Word not found in any PDF files inside this folder.") |
| else: |
| print(f"\nSearch complete. Total matches found: {overall_matches}") |
|
|
| |
| if __name__ == "__main__": |
| |
| my_folder = "./my_pdf_documents" |
| word_to_find = "contract" |
| |
| search_word_in_folder(my_folder, word_to_find) |
|
|