Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fitz # PyMuPDF
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import tempfile
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
def extract_po_to_excel(pdf_file):
|
| 8 |
+
# Regular expressions to match Toshiba PO fields
|
| 9 |
+
toshiba_item_pattern = re.compile(r'Pos\.\s*Item Code\s*Unit\s*Delivery Date\s*Quantity\s*Basic Price\s*Discount\s*Cur\.\s*Amount', re.IGNORECASE)
|
| 10 |
+
toshiba_data_pattern = re.compile(r'(\d+)\s+(\d+)\s+(\w+)\s+(\d{4}-\d{2}-\d{2})\s+([\d.]+)\s+([\d.]+)\s+([\d.]+)\s+(\w+)\s+([\d.]+)')
|
| 11 |
+
|
| 12 |
+
# Regular expressions to match BHEL PO fields (different format)
|
| 13 |
+
bhel_data_pattern = re.compile(r'Sl No\s+Material Description\s+Unit\s+Quantity\s+Dely Qty\s+Dely Date\s+Unit Rate\s+Value', re.IGNORECASE)
|
| 14 |
+
bhel_data_line_pattern = re.compile(r'(\d+)\s+([A-Za-z0-9 ]+)\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d{2}-\d{2}-\d{4})\s+([\d.]+)\s+([\d.]+)')
|
| 15 |
+
|
| 16 |
+
# Initialize list to store extracted data
|
| 17 |
+
extracted_data = []
|
| 18 |
+
|
| 19 |
+
# Load PDF
|
| 20 |
+
with fitz.open(pdf_file.name) as pdf:
|
| 21 |
+
for page_num in range(pdf.page_count):
|
| 22 |
+
page = pdf[page_num]
|
| 23 |
+
text = page.get_text("text")
|
| 24 |
+
|
| 25 |
+
# Check for Toshiba PO format and extract data
|
| 26 |
+
if toshiba_item_pattern.search(text):
|
| 27 |
+
matches = toshiba_data_pattern.findall(text)
|
| 28 |
+
for match in matches:
|
| 29 |
+
pos, item_code, unit, delivery_date, quantity, basic_price, discount, currency, amount = match
|
| 30 |
+
extracted_data.append({
|
| 31 |
+
"Position": pos,
|
| 32 |
+
"Item Code": item_code,
|
| 33 |
+
"Unit": unit,
|
| 34 |
+
"Delivery Date": delivery_date,
|
| 35 |
+
"Quantity": quantity,
|
| 36 |
+
"Basic Price": basic_price,
|
| 37 |
+
"Discount": discount,
|
| 38 |
+
"Currency": currency,
|
| 39 |
+
"Amount": amount
|
| 40 |
+
})
|
| 41 |
+
|
| 42 |
+
# Check for BHEL PO format and extract data
|
| 43 |
+
elif bhel_data_pattern.search(text):
|
| 44 |
+
matches = bhel_data_line_pattern.findall(text)
|
| 45 |
+
for match in matches:
|
| 46 |
+
sl_no, material_desc, unit, qty, dely_qty, dely_date, unit_rate, value = match
|
| 47 |
+
extracted_data.append({
|
| 48 |
+
"Sl No": sl_no,
|
| 49 |
+
"Material Description": material_desc,
|
| 50 |
+
"Unit": unit,
|
| 51 |
+
"Quantity": qty,
|
| 52 |
+
"Dely Qty": dely_qty,
|
| 53 |
+
"Dely Date": dely_date,
|
| 54 |
+
"Unit Rate": unit_rate,
|
| 55 |
+
"Value": value
|
| 56 |
+
})
|
| 57 |
+
|
| 58 |
+
# Create DataFrame
|
| 59 |
+
df = pd.DataFrame(extracted_data)
|
| 60 |
+
|
| 61 |
+
# Save DataFrame to a temporary Excel file
|
| 62 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
|
| 63 |
+
df.to_excel(temp_file.name, index=False)
|
| 64 |
+
temp_file.close()
|
| 65 |
+
|
| 66 |
+
return temp_file.name
|
| 67 |
+
|
| 68 |
+
def main(pdf_file):
|
| 69 |
+
excel_file_path = extract_po_to_excel(pdf_file)
|
| 70 |
+
return excel_file_path
|
| 71 |
+
|
| 72 |
+
# Gradio interface
|
| 73 |
+
interface = gr.Interface(
|
| 74 |
+
fn=main,
|
| 75 |
+
inputs=gr.File(label="Upload PO PDF"),
|
| 76 |
+
outputs=gr.File(label="Download Excel File"),
|
| 77 |
+
title="PO PDF to Excel Converter",
|
| 78 |
+
description="Upload a PO PDF file to extract and download it as an Excel sheet."
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
interface.launch()
|