Spaces:
Sleeping
Sleeping
| import pdfplumber | |
| def extract_text_from_pdf(file_path, max_pages=None): | |
| """ | |
| Extracts text from a PDF file. | |
| Parameters: | |
| file_path (str): Path to the PDF file. | |
| max_pages (int, optional): Max number of pages to extract (e.g., 2 for summaries). | |
| Returns: | |
| str: Extracted text from the PDF. | |
| """ | |
| text = "" | |
| try: | |
| with pdfplumber.open(file_path) as pdf: | |
| pages_to_read = pdf.pages if max_pages is None else pdf.pages[:max_pages] | |
| for page in pages_to_read: | |
| page_text = page.extract_text() | |
| if page_text: | |
| text += page_text.strip() + "\n" | |
| except Exception as e: | |
| print(f"[PDF Extraction Error] {e}") | |
| return "" | |
| return text.strip() | |