Spaces:
Running
Running
File size: 4,126 Bytes
0f97f06 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | # # pdf_preprocessor.py
# import os
# from pypdf import PdfReader
# from pdf2image import convert_from_path
# import pytesseract
# # Optional: Set Tesseract path manually on Windows
# # pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
# def extract_text_from_pdf(file_path: str) -> str:
# """
# Extract text from both text-based and image-based PDFs.
# Falls back to OCR using pytesseract if no embedded text is found.
# """
# text_output = []
# reader = PdfReader(file_path)
# total_pages = len(reader.pages)
# print(f"π Processing PDF: {file_path} ({total_pages} pages)")
# for page_num, page in enumerate(reader.pages, start=1):
# try:
# # Try normal text extraction
# extracted_text = page.extract_text()
# if extracted_text and extracted_text.strip():
# text_output.append(extracted_text)
# print(f"β
Page {page_num}: Extracted embedded text.")
# else:
# # Run OCR if no text found
# print(f"π Page {page_num}: No text found, running OCR...")
# images = convert_from_path(
# file_path, first_page=page_num, last_page=page_num
# )
# ocr_text = ""
# for img in images:
# ocr_text += pytesseract.image_to_string(img, lang="eng", config="--psm 6")
# if ocr_text.strip():
# text_output.append(ocr_text)
# print(f"π§ Page {page_num}: OCR extraction complete.")
# else:
# print(f"β οΈ Page {page_num}: OCR found no readable text.")
# except Exception as e:
# print(f"β Error processing page {page_num}: {e}")
# full_text = "\n".join(text_output)
# if not full_text.strip():
# print("β οΈ Warning: No text extracted from this PDF at all.")
# else:
# print(f"β
Done! Extracted {len(full_text.split())} words total.")
# return full_text
# pdf_preprocessor.py
import os
from pypdf import PdfReader
from pdf2image import convert_from_path
import pytesseract
# Optional: Set Tesseract path manually on Windows
# pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
def extract_text_from_pdf(file_path: str) -> str:
"""
Extract text from both text-based and image-based PDFs.
Falls back to OCR using pytesseract if no embedded text is found.
"""
text_output = []
reader = PdfReader(file_path)
total_pages = len(reader.pages)
print(f"π Processing PDF: {file_path} ({total_pages} pages)")
for page_num, page in enumerate(reader.pages, start=1):
try:
# Try normal text extraction
extracted_text = page.extract_text()
if extracted_text and extracted_text.strip():
text_output.append(extracted_text)
print(f"β
Page {page_num}: Extracted embedded text.")
else:
# Run OCR if no text found
print(f"π Page {page_num}: No text found, running OCR...")
images = convert_from_path(
file_path, first_page=page_num, last_page=page_num
)
ocr_text = ""
for img in images:
ocr_text += pytesseract.image_to_string(img, lang="eng", config="--psm 6")
if ocr_text.strip():
text_output.append(ocr_text)
print(f"π§ Page {page_num}: OCR extraction complete.")
else:
print(f"β οΈ Page {page_num}: OCR found no readable text.")
except Exception as e:
print(f"β Error processing page {page_num}: {e}")
full_text = "\n".join(text_output)
if not full_text.strip():
print("β οΈ Warning: No text extracted from this PDF at all.")
else:
print(f"β
Done! Extracted {len(full_text.split())} words total.")
return full_text
|