File size: 1,718 Bytes
415afc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import gradio as gr
import pandas as pd
import pdfplumber  # Or any other PDF extraction library
import io

def extract_columns_from_pdf(file, columns):
    # Read the PDF and extract tables
    try:
        pdf_data = []
        with pdfplumber.open(file) as pdf:
            for page in pdf.pages:
                tables = page.extract_tables()
                for table in tables:
                    # Assume tables are correctly extracted; convert them to DataFrames
                    df = pd.DataFrame(table[1:], columns=table[0])
                    pdf_data.append(df)
        
        # Combine all tables into one DataFrame
        full_data = pd.concat(pdf_data, ignore_index=True)

        # Process column input
        required_columns = [col.strip() for col in columns.split(",")]

        # Filter data based on required columns
        filtered_data = full_data[required_columns]

        # Save to Excel
        output = io.BytesIO()
        with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
            filtered_data.to_excel(writer, index=False)
        
        output.seek(0)
        return output
    except Exception as e:
        return f"Error: {str(e)}"

def main():
    # Define Gradio interface
    column_input = gr.Textbox(label="Enter Column Names (comma-separated)")
    file_input = gr.File(label="Upload PDF", file_types=[".pdf"])
    output_file = gr.File(label="Download Extracted Excel File")

    # Create Gradio interface
    interface = gr.Interface(
        fn=extract_columns_from_pdf,
        inputs=[file_input, column_input],
        outputs=output_file,
        live=False
    )

    # Launch app
    interface.launch()

if __name__ == "__main__":
    main()