File size: 1,522 Bytes
d2251a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pdfplumber
import pandas as pd
import gradio as gr

# Define function to extract data
def extract_data(pdf_file):
    data = []
    columns = ["SI No", "Material Description", "Unit", "Quantity", "Dely Qty", "Dely Date", "Unit Rate", "Value"]
    
    start_si, end_si = 10, 1150

    with pdfplumber.open(pdf_file) as pdf:
        for page in pdf.pages:
            text = page.extract_text().splitlines()
            for line in text:
                parts = line.split()
                try:
                    si_no = int(parts[0])
                    if start_si <= si_no <= end_si:
                        material_desc = " ".join(parts[1:3])
                        unit = parts[3]
                        quantity = int(parts[4])
                        dely_qty = int(parts[5])
                        dely_date = parts[6]
                        unit_rate = float(parts[7])
                        value = float(parts[8])
                        data.append([si_no, material_desc, unit, quantity, dely_qty, dely_date, unit_rate, value])
                except (ValueError, IndexError):
                    continue

    df = pd.DataFrame(data, columns=columns)
    excel_path = "/tmp/Extracted_Purchase_Order_Data.xlsx"
    df.to_excel(excel_path, index=False)
    return excel_path

# Set up Gradio interface
iface = gr.Interface(
    fn=extract_data,
    inputs=gr.File(label="Upload PDF"),
    outputs=gr.File(label="Download Excel"),
    title="PDF Data Extractor"
)

# Launch the app
iface.launch()