DSatishchandra commited on
Commit
2e50318
·
verified ·
1 Parent(s): 93b784c

Update bhel.py

Browse files
Files changed (1) hide show
  1. bhel.py +102 -75
bhel.py CHANGED
@@ -1,83 +1,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)}"
 
1
  import pdfplumber
2
  import pandas as pd
3
  import re
4
+ import gradio as gr
5
 
6
+ def extract_data(pdf_file):
7
+ """
8
+ Extract data from the uploaded PDF for dynamic ranges (e.g., 10 to n).
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
+ full_text = page.extract_text() # Get the text content for the page
19
+ lines = full_text.splitlines() if full_text else []
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
+ # Convert data to DataFrame and save as Excel
56
+ df = pd.DataFrame(data, columns=columns)
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
+ Extract Material Description, including Material Number, HSN Code, and IGST, using unique patterns.
 
64
  """
65
+ material_desc = ""
66
+
67
+ # Match the specific SI No row to extract details
68
+ si_no_pattern = rf"{si_no}\s+(BPS\s+\d+).*?Material\s+Number:\s+(\d+)"
69
+ match = re.search(si_no_pattern, full_text, re.DOTALL)
70
+ if match:
71
+ bps_code = match.group(1)
72
+ material_number = match.group(2)
73
+ material_desc += f"{bps_code}\nMaterial Number: {material_number}\n"
74
+
75
+ # Extract HSN Code
76
+ hsn_code_match = re.search(r"HSN\s+Code:\s*(\d+)", full_text)
77
+ if hsn_code_match:
78
+ hsn_code = hsn_code_match.group(1)
79
+ material_desc += f"HSN Code: {hsn_code}\n"
80
+ else:
81
+ material_desc += "HSN Code: Not Found\n"
82
+
83
+ # Extract IGST
84
+ igst_match = re.search(r"IGST\s*:\s*(\d+)\s*%", full_text)
85
+ if igst_match:
86
+ igst = igst_match.group(1)
87
+ material_desc += f"IGST: {igst} %"
88
+ else:
89
+ material_desc += "IGST: Not Found"
90
+
91
+ return material_desc.strip()
92
+
93
+ # Gradio Interface
94
+ def gradio_interface(pdf_file):
95
+ """
96
+ Interface function for Gradio to process the PDF and return the Excel file.
97
+ """
98
+ return extract_data(pdf_file.name)
99
+
100
+ # Define Gradio interface
101
+ interface = gr.Interface(
102
+ fn=gradio_interface,
103
+ inputs=gr.File(label="Upload PDF"),
104
+ outputs=gr.File(label="Download Extracted Excel"),
105
+ title="Dynamic BHEL PO Data Extractor",
106
+ description="Upload a PDF to extract accurate Material Numbers and related data dynamically into an Excel file."
107
+ )
108
+
109
+ if __name__ == "__main__":
110
+ interface.launch()