SimpleSam commited on
Commit
37f7e27
·
verified ·
1 Parent(s): adb0cac

Update tools/pdfquery

Browse files
Files changed (1) hide show
  1. tools/pdfquery +44 -27
tools/pdfquery CHANGED
@@ -1,37 +1,54 @@
1
- """ first install pypdf using pip install pypdf"""
2
  import pypdf
3
 
4
- def search_word_in_pdf(pdf_path, search_word):
5
  """
6
- Searches for a specific word in a PDF file and prints the pages where it's found.
7
  """
8
- # Open the PDF file
9
- with open(pdf_path, 'rb') as file:
10
- # Create a PDF reader object
11
- reader = pypdf.PdfReader(file)
12
-
13
- # Track if the word was found at all
14
- found_matches = False
15
-
16
- print(f"Searching for '{search_word}' in '{pdf_path}'...\n")
17
-
18
- # Loop through each page (0-indexed, so we add 1 for actual page number)
19
- for page_num, page in enumerate(reader.pages, start=1):
20
- # Extract text from the page
21
- text = page.extract_text()
 
 
22
 
23
- # Perform case-insensitive search
24
- if text and search_word.lower() in text.lower():
25
- print(f"✅ Found on Page {page_num}")
26
- found_matches = True
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- if not found_matches:
29
- print("❌ Word not found in the document.")
 
 
30
 
31
  # Example Usage
32
  if __name__ == "__main__":
33
- # Replace with your actual PDF file path and target word
34
- pdf_file = "sample.pdf"
35
- word_to_find = "invoice"
36
 
37
- search_word_in_pdf(pdf_file, word_to_find)
 
1
+ import os
2
  import pypdf
3
 
4
+ def search_word_in_folder(folder_path, search_word):
5
  """
6
+ Searches for a word across all PDF files in a given folder.
7
  """
8
+ # Normalize the search word for case-insensitive matching
9
+ search_word_lower = search_word.lower()
10
+
11
+ # Check if the folder exists
12
+ if not os.path.exists(folder_path):
13
+ print(f"❌ Error: The folder '{folder_path}' does not exist.")
14
+ return
15
+
16
+ print(f"Scanning folder '{folder_path}' for '{search_word}'...\n")
17
+ overall_matches = 0
18
+
19
+ # Loop through all files in the directory
20
+ for filename in os.listdir(folder_path):
21
+ # Only process files that end with .pdf
22
+ if filename.lower().endswith('.pdf'):
23
+ pdf_path = os.path.join(folder_path, filename)
24
 
25
+ try:
26
+ with open(pdf_path, 'rb') as file:
27
+ reader = pypdf.PdfReader(file)
28
+ file_has_matches = False
29
+
30
+ # Search page by page
31
+ for page_num, page in enumerate(reader.pages, start=1):
32
+ text = page.extract_text()
33
+
34
+ if text and search_word_lower in text.lower():
35
+ # Print the filename and page number where found
36
+ print(f"✅ Found in [{filename}] on Page {page_num}")
37
+ file_has_matches = True
38
+ overall_matches += 1
39
+
40
+ except Exception as e:
41
+ print(f"⚠️ Could not read file {filename}: {e}")
42
 
43
+ if overall_matches == 0:
44
+ print("❌ Word not found in any PDF files inside this folder.")
45
+ else:
46
+ print(f"\nSearch complete. Total matches found: {overall_matches}")
47
 
48
  # Example Usage
49
  if __name__ == "__main__":
50
+ # Replace with the path to your folder
51
+ my_folder = "./my_pdf_documents"
52
+ word_to_find = "contract"
53
 
54
+ search_word_in_folder(my_folder, word_to_find)