File size: 762 Bytes
ff08e35
 
 
 
 
 
 
 
6928eef
ff08e35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fitz  # PyMuPDF
import gradio as gr

def extract_text_from_pdf(file):
    if file is None:
        return "No file uploaded."
    
    try:
        doc = fitz.open(file.name)  # Use file path directly
        full_text = ""
        for page_num in range(len(doc)):
            page = doc.load_page(page_num)
            text = page.get_text()
            full_text += f"\n\n--- Page {page_num + 1} ---\n\n{text}"
        return full_text
    except Exception as e:
        return f"Error: {str(e)}"

gr.Interface(
    fn=extract_text_from_pdf,
    inputs=gr.File(label="Upload PDF", file_types=[".pdf"]),
    outputs="text",
    title="PDF to Text Extractor",
    description="Upload a PDF file and get all the extracted text from each page.",
).launch()