File size: 789 Bytes
df95782
2005bec
df95782
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()