Spaces:
Sleeping
Sleeping
DSatishchandra
commited on
Commit
•
ea15866
1
Parent(s):
e0d3587
Create Federal Electric
Browse files- Federal Electric +122 -0
Federal Electric
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pdfplumber
|
2 |
+
import re
|
3 |
+
import pandas as pd
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
def extract_po_data(pdf_file):
|
7 |
+
"""
|
8 |
+
Extracts Purchase Order data with enhanced multi-line Material Description handling,
|
9 |
+
and cleans unwanted text or symbols.
|
10 |
+
"""
|
11 |
+
data = []
|
12 |
+
purchase_order_no = None
|
13 |
+
purchase_order_date = None
|
14 |
+
|
15 |
+
with pdfplumber.open(pdf_file) as pdf:
|
16 |
+
for page in pdf.pages:
|
17 |
+
# Extract text from page
|
18 |
+
lines = page.extract_text().split("\n")
|
19 |
+
temp_row = None # Temporary row to handle multi-line descriptions
|
20 |
+
|
21 |
+
# Extract Purchase Order Number and Date (Assume it's on the first page)
|
22 |
+
if purchase_order_no is None: # Only extract once
|
23 |
+
po_no_match = re.search(r"Purchase Order No[:\s]+(\S+)", "\n".join(lines))
|
24 |
+
po_date_match = re.search(r"Purchase Order Date[:\s]+(\S+)", "\n".join(lines))
|
25 |
+
|
26 |
+
if po_no_match:
|
27 |
+
purchase_order_no = po_no_match.group(1)
|
28 |
+
if po_date_match:
|
29 |
+
purchase_order_date = po_date_match.group(1)
|
30 |
+
|
31 |
+
# Process each line to extract data
|
32 |
+
for line in lines:
|
33 |
+
# Regex pattern for rows (excluding multi-line descriptions)
|
34 |
+
pattern = r"^\s*(\d+)\s+(\d+)\s+([A-Z0-9_(),\- ]+?)\s+(\d+)\s+(\w+)\s+([\d.]+)\s+([\d\-A-Za-z]+)\s+([\d.]+)\s+([\d.]+)\s+([\d.]+)\s*$"
|
35 |
+
match = re.match(pattern, line)
|
36 |
+
|
37 |
+
if match:
|
38 |
+
# If there's a match, capture the full row
|
39 |
+
if temp_row: # Append the previous temp_row if it exists
|
40 |
+
data.append(temp_row)
|
41 |
+
temp_row = None
|
42 |
+
temp_row = {
|
43 |
+
"S. No": match[1],
|
44 |
+
"Material No": match[2],
|
45 |
+
"Material Description": match[3].strip(),
|
46 |
+
"Qty": int(match[4]),
|
47 |
+
"Unit": match[5],
|
48 |
+
"Price": float(match[6]),
|
49 |
+
"Delivery Date": match[7],
|
50 |
+
"Total Value": float(match[8]),
|
51 |
+
"Vat%": float(match[9]),
|
52 |
+
"Amount Incl. VAT": float(match[10]),
|
53 |
+
}
|
54 |
+
elif temp_row:
|
55 |
+
# If no match, treat it as a continuation of Material Description
|
56 |
+
temp_row["Material Description"] += f" {line.strip()}"
|
57 |
+
|
58 |
+
# Append the last row
|
59 |
+
if temp_row:
|
60 |
+
data.append(temp_row)
|
61 |
+
|
62 |
+
# Create DataFrame
|
63 |
+
df = pd.DataFrame(data)
|
64 |
+
|
65 |
+
# Insert Purchase Order No and Purchase Order Date at the beginning
|
66 |
+
if purchase_order_no and purchase_order_date:
|
67 |
+
df.insert(0, "Purchase Order No", purchase_order_no)
|
68 |
+
df.insert(1, "Purchase Order Date", purchase_order_date)
|
69 |
+
|
70 |
+
# Filter unwanted text from Material Description
|
71 |
+
def clean_description(description):
|
72 |
+
# Define unwanted patterns
|
73 |
+
unwanted_patterns = [
|
74 |
+
r"This document is electronically approved", # Matches exact phrase
|
75 |
+
r"does not require any signature or stamp", # Matches approval notes
|
76 |
+
r"Total Amount Excl\. VAT.*", # Matches totals
|
77 |
+
r"TWO THOUSAND.*ONLY", # Matches written totals
|
78 |
+
r"&", # Removes stray symbols like `&`
|
79 |
+
r"\.+$", # Removes trailing periods
|
80 |
+
]
|
81 |
+
for pattern in unwanted_patterns:
|
82 |
+
description = re.sub(pattern, "", description, flags=re.IGNORECASE).strip()
|
83 |
+
return description
|
84 |
+
|
85 |
+
df["Material Description"] = df["Material Description"].apply(clean_description)
|
86 |
+
|
87 |
+
# Strip extra spaces
|
88 |
+
df["Material Description"] = df["Material Description"].str.strip()
|
89 |
+
|
90 |
+
return df
|
91 |
+
|
92 |
+
def process_and_save(pdf_file, output_format):
|
93 |
+
"""
|
94 |
+
Processes the uploaded PDF and saves the extracted data as an Excel or CSV file.
|
95 |
+
"""
|
96 |
+
df = extract_po_data(pdf_file.name)
|
97 |
+
|
98 |
+
# Save the file in the desired format
|
99 |
+
output_file = f"output.{output_format}"
|
100 |
+
if output_format == "csv":
|
101 |
+
df.to_csv(output_file, index=False)
|
102 |
+
elif output_format == "xlsx":
|
103 |
+
df.to_excel(output_file, index=False, engine="openpyxl")
|
104 |
+
|
105 |
+
return output_file
|
106 |
+
|
107 |
+
# Gradio interface function
|
108 |
+
def gradio_interface(pdf_file, output_format):
|
109 |
+
output_file = process_and_save(pdf_file, output_format)
|
110 |
+
return output_file
|
111 |
+
|
112 |
+
# Gradio app interface
|
113 |
+
iface = gr.Interface(
|
114 |
+
fn=gradio_interface,
|
115 |
+
inputs=[gr.File(label="Upload PDF"), gr.Radio(["csv", "xlsx"], label="Output Format")],
|
116 |
+
outputs=gr.File(label="Download Output"),
|
117 |
+
title="Enhanced PO Data Extractor",
|
118 |
+
description="Extract data from Purchase Orders, including multi-line descriptions, and clean unwanted text or symbols. Download as CSV or Excel."
|
119 |
+
)
|
120 |
+
|
121 |
+
if __name__ == "__main__":
|
122 |
+
iface.launch()
|