File size: 1,053 Bytes
d2e606b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
32
33
34
35
36
37


import os

import gradio as gr
import csv

def pdf_to_csv(pdf_file):
    # Open the uploaded PDF file (pdf_file is a TemporaryFile)
    # pdf_reader = PyPDF2.PdfReader(pdf_file.name)
    text_lines = []

    file_name = os.path.basename(pdf_file.name)

    text_lines.append(f"File Name: {file_name}")
    csv_filename = "extracted_text.csv"
    # Write each line into the CSV file (each line in its own row)
    with open(csv_filename, "w", newline="", encoding="utf-8") as csvfile:
        writer = csv.writer(csvfile)
        for line in text_lines:
            writer.writerow([line])
 
    # Return the CSV file path so Gradio can offer it as a download
    return csv_filename

# Create a simple single-page Gradio interface
demo = gr.Interface(
    fn=pdf_to_csv,
    inputs=gr.File(label="Upload PDF", file_types=[".pdf"]),
    outputs=gr.File(label="Download CSV"),
    title="PDF to CSV Converter",
    description="Upload a PDF file, extract its text line-by-line, and download a CSV."
)

demo.launch()