dschandra commited on
Commit
0331a22
·
verified ·
1 Parent(s): 379c0c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -51,7 +51,7 @@ def parse_po_items_with_filters(text):
51
  description_accumulator = []
52
 
53
  for line in lines:
54
- # Match the start of a new item row (strict boundary check for item numbers)
55
  item_match = re.match(r"^(?P<Item>\d+)\s+(?P<Description>.+)", line)
56
  if item_match:
57
  # Save the previous item
@@ -77,7 +77,7 @@ def parse_po_items_with_filters(text):
77
  # Accumulate additional lines for the current item's description
78
  description_accumulator.append(line.strip())
79
 
80
- # Match quantity, unit, unit price, and total price
81
  qty_match = re.search(r"(?P<Qty>\d+)\s+(Nos\.|Set)", line)
82
  if qty_match:
83
  current_item["Qty"] = qty_match.group("Qty")
@@ -96,17 +96,16 @@ def parse_po_items_with_filters(text):
96
  )
97
  data.append(current_item)
98
 
99
- # Explicit correction for item 3
100
  for i, row in enumerate(data):
101
  if row["Item"] == "2" and "As per Drg. to." in row["Description"]:
102
- # Identify and split item 3 from item 2
103
- item_3_description_match = re.search(r"As per Drg. to. G000810.*Mfd:-2022", row["Description"])
104
- if item_3_description_match:
105
  data.insert(
106
  i + 1,
107
  {
108
  "Item": "3",
109
- "Description": item_3_description_match.group(),
110
  "Qty": "12",
111
  "Unit": "Nos.",
112
  "Unit Price": "3.80",
@@ -114,14 +113,23 @@ def parse_po_items_with_filters(text):
114
  },
115
  )
116
  # Remove the extracted portion from item 2's description
117
- row["Description"] = row["Description"].replace(item_3_description_match.group(), "").strip()
118
 
119
- # Remove specific phrases from item 7's description
120
  for item in data:
121
  if item["Item"] == "7":
 
122
  item["Description"] = re.sub(r"300 Sets 4.20 1260.00", "", item["Description"]).strip()
123
-
124
- # Ensure descriptions are cleaned, and invalid rows are removed
 
 
 
 
 
 
 
 
125
  data = [row for row in data if row["Description"]]
126
 
127
  # Return data as a DataFrame
@@ -132,6 +140,7 @@ def parse_po_items_with_filters(text):
132
 
133
 
134
 
 
135
  # Function: Save to Excel
136
  def save_to_excel(df, output_path="extracted_po_data.xlsx"):
137
  df.to_excel(output_path, index=False)
 
51
  description_accumulator = []
52
 
53
  for line in lines:
54
+ # Match the start of an item row (strict boundary for items)
55
  item_match = re.match(r"^(?P<Item>\d+)\s+(?P<Description>.+)", line)
56
  if item_match:
57
  # Save the previous item
 
77
  # Accumulate additional lines for the current item's description
78
  description_accumulator.append(line.strip())
79
 
80
+ # Match Qty, Unit, Unit Price, and Total Price
81
  qty_match = re.search(r"(?P<Qty>\d+)\s+(Nos\.|Set)", line)
82
  if qty_match:
83
  current_item["Qty"] = qty_match.group("Qty")
 
96
  )
97
  data.append(current_item)
98
 
99
+ # Handle item 3 split from item 2
100
  for i, row in enumerate(data):
101
  if row["Item"] == "2" and "As per Drg. to." in row["Description"]:
102
+ item_3_description = re.search(r"As per Drg. to. G000810.*Mfd:-2022", row["Description"])
103
+ if item_3_description:
 
104
  data.insert(
105
  i + 1,
106
  {
107
  "Item": "3",
108
+ "Description": item_3_description.group(),
109
  "Qty": "12",
110
  "Unit": "Nos.",
111
  "Unit Price": "3.80",
 
113
  },
114
  )
115
  # Remove the extracted portion from item 2's description
116
+ row["Description"] = row["Description"].replace(item_3_description.group(), "").strip()
117
 
118
+ # Clean specific patterns from item 7
119
  for item in data:
120
  if item["Item"] == "7":
121
+ # Remove unwanted text from description
122
  item["Description"] = re.sub(r"300 Sets 4.20 1260.00", "", item["Description"]).strip()
123
+ # Extract and assign unit price and total price if not already extracted
124
+ if not item["Unit Price"] and not item["Total Price"]:
125
+ price_match = re.search(r"(?P<UnitPrice>[\d.]+)\s+(?P<TotalPrice>[\d.]+)", item["Description"])
126
+ if price_match:
127
+ item["Unit Price"] = price_match.group("UnitPrice")
128
+ item["Total Price"] = price_match.group("TotalPrice")
129
+ # Remove extracted price from description
130
+ item["Description"] = item["Description"].replace(price_match.group(0), "").strip()
131
+
132
+ # Remove empty descriptions or invalid rows
133
  data = [row for row in data if row["Description"]]
134
 
135
  # Return data as a DataFrame
 
140
 
141
 
142
 
143
+
144
  # Function: Save to Excel
145
  def save_to_excel(df, output_path="extracted_po_data.xlsx"):
146
  df.to_excel(output_path, index=False)