Spaces:
Sleeping
Sleeping
File size: 882 Bytes
0c0884c | 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 | import os
import fitz
def load_pdf_text(file_path: str) -> str:
"""
Reads a PDF file using PyMuPDF (fitz), extracts all text page by page,
and returns the combined text.
Args:
file_path (str): Absolute or relative path to the PDF file.
Returns:
str: Extracted raw text.
"""
if not os.path.exists(file_path):
raise FileNotFoundError(f"PDF file not found: {file_path}")
try:
doc = fitz.open(file_path)
text_list = []
for page_num in range(len(doc)):
page = doc.load_page(page_num)
text_list.append(page.get_text())
doc.close()
return "\n".join(text_list)
except Exception as e:
print(f"Error reading PDF {file_path}: {str(e)}")
raise RuntimeError(f"Failed to load PDF {file_path}: {str(e)}")
|