Spaces:
Build error
Build error
File size: 1,748 Bytes
ff7685a |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
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() |