pdf-to-excel / toshiba.py
neerajkalyank's picture
Create toshiba.py
ff7685a verified
raw
history blame
1.75 kB
import gradio as gr
import pdfplumber
import pandas as pd
import re
def extract_data(pdf_file):
data = []
purchase_order, order_date = None, None
with pdfplumber.open(pdf_file) as pdf:
for page in pdf.pages:
text = page.extract_text().splitlines()
if not purchase_order or not order_date:
for line in text:
po_match = re.search(r'Purchase Order\s*:\s*(P\d+)', line)
date_match = re.search(r'Order Date\s*:\s*([\d-]+)', line)
if po_match:
purchase_order = po_match.group(1)
if date_match:
order_date = date_match.group(1)
for line in text:
parts = line.split()
try:
pos = int(parts[0])
if 10 <= pos <= 450:
item_code = parts[1]
quantity = float(parts[4])
basic_price = float(parts[5])
sub_total = float(parts[-1])
data.append([purchase_order, order_date, pos, item_code, quantity, basic_price, sub_total])
except (ValueError, IndexError):
continue
df = pd.DataFrame(data, columns=["Purchase Order", "Order Date", "Pos", "Item Code", "Quantity", "Basic Price", "Sub Total"])
excel_path = "/tmp/Extracted_Purchase_Order_Data.xlsx"
df.to_excel(excel_path, index=False)
return excel_path
iface = gr.Interface(
fn=extract_data,
inputs=gr.File(label="Upload PDF"),
outputs=gr.File(label="Download Excel"),
title="PDF Data Extractor"
)
iface.launch()