Spaces:
Sleeping
Sleeping
DSatishchandra
commited on
Update bhel.py
Browse files
bhel.py
CHANGED
@@ -1,110 +1,83 @@
|
|
1 |
import pdfplumber
|
2 |
import pandas as pd
|
3 |
import re
|
4 |
-
import gradio as gr
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
data = []
|
11 |
-
columns = ["SI No", "Material Description", "Unit", "Quantity", "Dely Qty", "Dely Date", "Unit Rate", "Value"]
|
12 |
-
|
13 |
-
start_si = 10 # Start from SI No 10
|
14 |
-
end_si = None # Dynamically detect the end SI No
|
15 |
-
|
16 |
-
with pdfplumber.open(pdf_file) as pdf:
|
17 |
for page in pdf.pages:
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
for line in lines:
|
22 |
-
try:
|
23 |
-
# Parse the first column for SI No
|
24 |
-
si_no_match = re.match(r"^\s*(\d+)\s", line)
|
25 |
-
if not si_no_match:
|
26 |
-
continue
|
27 |
-
|
28 |
-
si_no = int(si_no_match.group(1))
|
29 |
-
# Dynamically set the end SI No if higher SI Nos are found
|
30 |
-
if end_si is None or si_no > end_si:
|
31 |
-
end_si = si_no
|
32 |
-
|
33 |
-
if si_no < start_si:
|
34 |
-
continue # Skip rows below the start SI No
|
35 |
-
|
36 |
-
# Extract Material Description and details dynamically
|
37 |
-
material_desc = extract_material_description(full_text, si_no)
|
38 |
-
|
39 |
-
# Extract remaining fields
|
40 |
-
parts = line.split()
|
41 |
-
unit = parts[3]
|
42 |
-
quantity = int(parts[4])
|
43 |
-
dely_qty = int(parts[5])
|
44 |
-
dely_date = parts[6]
|
45 |
-
unit_rate = float(parts[7])
|
46 |
-
value = float(parts[8])
|
47 |
-
|
48 |
-
# Append row data
|
49 |
-
data.append([si_no, material_desc, unit, quantity, dely_qty, dely_date, unit_rate, value])
|
50 |
-
|
51 |
-
except (ValueError, IndexError):
|
52 |
-
# Skip invalid rows or rows with missing data
|
53 |
-
continue
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
excel_path = "/tmp/Extracted_PO_Data_Dynamic.xlsx"
|
58 |
-
df.to_excel(excel_path, index=False)
|
59 |
-
return excel_path
|
60 |
-
|
61 |
-
def extract_material_description(full_text, si_no):
|
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 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
#
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
)
|
108 |
-
|
109 |
-
if
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import pdfplumber
|
2 |
import pandas as pd
|
3 |
import re
|
|
|
4 |
|
5 |
+
# Function: Extract Text from PDF
|
6 |
+
def extract_text_from_pdf(pdf_file):
|
7 |
+
with pdfplumber.open(pdf_file.name) as pdf:
|
8 |
+
text = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
for page in pdf.pages:
|
10 |
+
text += page.extract_text()
|
11 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
# Function: Parse PO Items
|
14 |
+
def parse_po_items(text):
|
|
|
|
|
|
|
|
|
|
|
15 |
"""
|
16 |
+
Parses purchase order items from the extracted text.
|
17 |
+
Handles split descriptions across lines and filters unwanted text.
|
18 |
"""
|
19 |
+
lines = text.splitlines()
|
20 |
+
data = []
|
21 |
+
current_item = {}
|
22 |
+
description_accumulator = []
|
23 |
+
|
24 |
+
for line in lines:
|
25 |
+
# Match the start of an item row
|
26 |
+
item_match = re.match(r"^(?P<Item>\d+)\s+(?P<Description>.+)", line)
|
27 |
+
if item_match:
|
28 |
+
# Save the previous item and start a new one
|
29 |
+
if current_item:
|
30 |
+
current_item["Description"] = " ".join(description_accumulator).strip()
|
31 |
+
data.append(current_item)
|
32 |
+
description_accumulator = []
|
33 |
+
|
34 |
+
current_item = {
|
35 |
+
"Item": item_match.group("Item"),
|
36 |
+
"Description": "",
|
37 |
+
"Qty": "",
|
38 |
+
"Unit": "",
|
39 |
+
"Unit Price": "",
|
40 |
+
"Total Price": "",
|
41 |
+
}
|
42 |
+
description_accumulator.append(item_match.group("Description"))
|
43 |
+
elif current_item:
|
44 |
+
# Handle additional description lines or split descriptions
|
45 |
+
description_accumulator.append(line.strip())
|
46 |
+
|
47 |
+
# Match Qty, Unit, Unit Price, and Total Price
|
48 |
+
qty_match = re.search(r"(?P<Qty>\d+)\s+(Nos\.|Set)", line)
|
49 |
+
if qty_match:
|
50 |
+
current_item["Qty"] = qty_match.group("Qty")
|
51 |
+
current_item["Unit"] = qty_match.group(2)
|
52 |
+
|
53 |
+
price_match = re.search(r"(?P<UnitPrice>[\d.]+)\s+(?P<TotalPrice>[\d.]+)$", line)
|
54 |
+
if price_match:
|
55 |
+
current_item["Unit Price"] = price_match.group("UnitPrice")
|
56 |
+
current_item["Total Price"] = price_match.group("TotalPrice")
|
57 |
+
|
58 |
+
# Save the last item
|
59 |
+
if current_item:
|
60 |
+
current_item["Description"] = " ".join(description_accumulator).strip()
|
61 |
+
data.append(current_item)
|
62 |
+
|
63 |
+
if not data:
|
64 |
+
return None, "No items found. Please check the PDF file format."
|
65 |
+
df = pd.DataFrame(data)
|
66 |
+
return df, "Data extracted successfully."
|
67 |
+
|
68 |
+
# Function: Save to Excel
|
69 |
+
def save_to_excel(df, output_path="bhel_extracted_data.xlsx"):
|
70 |
+
df.to_excel(output_path, index=False)
|
71 |
+
return output_path
|
72 |
+
|
73 |
+
# Main function to process PDF
|
74 |
+
def process_pdf(file):
|
75 |
+
try:
|
76 |
+
text = extract_text_from_pdf(file)
|
77 |
+
df, status = parse_po_items(text)
|
78 |
+
if df is not None:
|
79 |
+
output_path = save_to_excel(df)
|
80 |
+
return output_path, status
|
81 |
+
return None, status
|
82 |
+
except Exception as e:
|
83 |
+
return None, f"Error during processing: {str(e)}"
|