Update app.py
Browse files
app.py
CHANGED
|
@@ -20,47 +20,47 @@ def extract_text_from_pdf(pdf_file):
|
|
| 20 |
return text
|
| 21 |
|
| 22 |
|
| 23 |
-
def
|
| 24 |
"""
|
| 25 |
-
|
| 26 |
Args:
|
| 27 |
lines (list): List of text lines from the PDF.
|
| 28 |
Returns:
|
| 29 |
-
list:
|
| 30 |
"""
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
for line in lines:
|
| 35 |
-
if re.match(r"^\d+\s", line): #
|
| 36 |
-
if
|
| 37 |
-
|
| 38 |
-
|
| 39 |
else:
|
| 40 |
-
|
| 41 |
|
| 42 |
-
if
|
| 43 |
-
|
| 44 |
|
| 45 |
-
return
|
| 46 |
|
| 47 |
|
| 48 |
-
def parse_po_items(
|
| 49 |
"""
|
| 50 |
-
Parses purchase order items from
|
| 51 |
Args:
|
| 52 |
-
|
| 53 |
Returns:
|
| 54 |
DataFrame: Extracted purchase order data.
|
| 55 |
"""
|
| 56 |
data = []
|
| 57 |
|
| 58 |
-
for
|
| 59 |
try:
|
| 60 |
-
#
|
| 61 |
match = re.match(
|
| 62 |
-
r"^(?P<Item>\d+)\s+(?P<Description>.+?)\s+(?P<Qty>\d+)\s+(?P<Unit>\S+)\s+(?P<UnitPrice>\d
|
| 63 |
-
|
| 64 |
)
|
| 65 |
if match:
|
| 66 |
data.append(
|
|
@@ -74,9 +74,9 @@ def parse_po_items(lines):
|
|
| 74 |
}
|
| 75 |
)
|
| 76 |
else:
|
| 77 |
-
print(f"Skipped
|
| 78 |
except Exception as e:
|
| 79 |
-
print(f"Error parsing
|
| 80 |
|
| 81 |
if not data:
|
| 82 |
return None, "No valid data found in the provided text."
|
|
@@ -107,10 +107,12 @@ def process_pdf(file):
|
|
| 107 |
try:
|
| 108 |
# Extract text from the uploaded PDF
|
| 109 |
text = extract_text_from_pdf(file)
|
| 110 |
-
#
|
| 111 |
-
lines =
|
| 112 |
-
#
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
if df is not None:
|
| 115 |
output_path = save_to_excel(df)
|
| 116 |
return output_path, status
|
|
|
|
| 20 |
return text
|
| 21 |
|
| 22 |
|
| 23 |
+
def reconstruct_rows(lines):
|
| 24 |
"""
|
| 25 |
+
Reconstructs rows by grouping multi-line descriptions into single rows.
|
| 26 |
Args:
|
| 27 |
lines (list): List of text lines from the PDF.
|
| 28 |
Returns:
|
| 29 |
+
list: List of reconstructed rows.
|
| 30 |
"""
|
| 31 |
+
rows = []
|
| 32 |
+
current_row = []
|
| 33 |
|
| 34 |
for line in lines:
|
| 35 |
+
if re.match(r"^\d+\s", line): # If the line starts with an item number
|
| 36 |
+
if current_row:
|
| 37 |
+
rows.append(" ".join(current_row)) # Add the current row
|
| 38 |
+
current_row = [line.strip()] # Start a new row
|
| 39 |
else:
|
| 40 |
+
current_row.append(line.strip()) # Append to the current row
|
| 41 |
|
| 42 |
+
if current_row:
|
| 43 |
+
rows.append(" ".join(current_row)) # Add the last row
|
| 44 |
|
| 45 |
+
return rows
|
| 46 |
|
| 47 |
|
| 48 |
+
def parse_po_items(rows):
|
| 49 |
"""
|
| 50 |
+
Parses purchase order items from reconstructed rows.
|
| 51 |
Args:
|
| 52 |
+
rows (list): List of reconstructed rows.
|
| 53 |
Returns:
|
| 54 |
DataFrame: Extracted purchase order data.
|
| 55 |
"""
|
| 56 |
data = []
|
| 57 |
|
| 58 |
+
for row in rows:
|
| 59 |
try:
|
| 60 |
+
# Match ITEM, DESCRIPTION, QTY, UNIT, UNIT PRICE, TOTAL PRICE
|
| 61 |
match = re.match(
|
| 62 |
+
r"^(?P<Item>\d+)\s+(?P<Description>.+?)\s+(?P<Qty>\d+)\s+(?P<Unit>\S+)\s+(?P<UnitPrice>[\d.]+)\s+(?P<TotalPrice>[\d.]+)$",
|
| 63 |
+
row,
|
| 64 |
)
|
| 65 |
if match:
|
| 66 |
data.append(
|
|
|
|
| 74 |
}
|
| 75 |
)
|
| 76 |
else:
|
| 77 |
+
print(f"Skipped row: {row}") # Debugging: Log skipped rows
|
| 78 |
except Exception as e:
|
| 79 |
+
print(f"Error parsing row: {row}, Error: {e}")
|
| 80 |
|
| 81 |
if not data:
|
| 82 |
return None, "No valid data found in the provided text."
|
|
|
|
| 107 |
try:
|
| 108 |
# Extract text from the uploaded PDF
|
| 109 |
text = extract_text_from_pdf(file)
|
| 110 |
+
# Split text into lines
|
| 111 |
+
lines = text.splitlines()
|
| 112 |
+
# Reconstruct rows
|
| 113 |
+
rows = reconstruct_rows(lines)
|
| 114 |
+
# Parse reconstructed rows
|
| 115 |
+
df, status = parse_po_items(rows)
|
| 116 |
if df is not None:
|
| 117 |
output_path = save_to_excel(df)
|
| 118 |
return output_path, status
|