Spaces:
Sleeping
Sleeping
Download src/utils/pdf_loader.py from Mehreen44/PK_LegalAssitant: direct link, hf CLI and curl.
- Browser
- Download file 882 Bytes
-
https://huggingface.co/spaces/Mehreen44/PK_LegalAssitant/resolve/main/src/utils/pdf_loader.py
- Command line
-
hf download hf://spaces/Mehreen44/PK_LegalAssitant/src/utils/pdf_loader.py
-
curl -L -o pdf_loader.py https://huggingface.co/spaces/Mehreen44/PK_LegalAssitant/resolve/main/src/utils/pdf_loader.py
882 Bytes
| 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)}") | |