pdf-to-excel / bhel.py
neerajkalyank's picture
Create bhel.py
69d8542 verified
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()