Spaces:
Runtime error
Runtime error
neerajkalyank
commited on
Commit
•
af70b59
1
Parent(s):
229b210
Update app.py
Browse files
app.py
CHANGED
@@ -1,108 +1,53 @@
|
|
1 |
-
import pdfplumber
|
2 |
import pandas as pd
|
3 |
import gradio as gr
|
4 |
import re
|
5 |
-
import tempfile
|
6 |
|
7 |
-
# Define function to extract data
|
8 |
-
def extract_data(
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
try:
|
45 |
-
# Match lines that start with an SI number (e.g., "10", "20")
|
46 |
-
si_no_match = re.match(r'^(\d+)\s', line)
|
47 |
-
if si_no_match:
|
48 |
-
parts = line.split()
|
49 |
-
|
50 |
-
# Extract SI No
|
51 |
-
si_no = parts[0]
|
52 |
-
|
53 |
-
# Extract Material Number and format the Material Description
|
54 |
-
material_number = parts[2] if len(parts) > 2 else "Unknown"
|
55 |
-
material_desc = f"BPS 017507\nMaterial Number: {material_number}\nHSN Code: 8310\nIGST: 18%"
|
56 |
-
|
57 |
-
# Extract Unit, Quantity, Dely Qty, Dely Date, Unit Rate, and Value
|
58 |
-
unit = parts[3] if len(parts) > 3 else "NO" # Default to "NO" if not found
|
59 |
-
quantity = int(parts[4]) if len(parts) > 4 else 0
|
60 |
-
dely_qty = int(parts[5]) if len(parts) > 5 else 0
|
61 |
-
dely_date = parts[6] if len(parts) > 6 else "Unknown"
|
62 |
-
unit_rate = float(parts[7]) if len(parts) > 7 else 0.0
|
63 |
-
value = float(parts[8]) if len(parts) > 8 else 0.0
|
64 |
-
|
65 |
-
# Append extracted data in the specified order
|
66 |
-
data.append([
|
67 |
-
purchase_order_no,
|
68 |
-
purchase_order_date,
|
69 |
-
si_no,
|
70 |
-
material_desc,
|
71 |
-
unit,
|
72 |
-
quantity,
|
73 |
-
dely_qty,
|
74 |
-
dely_date,
|
75 |
-
unit_rate,
|
76 |
-
value
|
77 |
-
])
|
78 |
-
except (ValueError, IndexError) as e:
|
79 |
-
print(f"Error processing line: {line} - {e}")
|
80 |
-
continue # Skip lines that do not match the expected format
|
81 |
-
|
82 |
-
# Convert data to DataFrame and save as Excel
|
83 |
-
df = pd.DataFrame(data, columns=columns)
|
84 |
-
|
85 |
-
# Generate a temporary file path for the Excel file
|
86 |
-
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp_file:
|
87 |
-
excel_path = tmp_file.name
|
88 |
-
df.to_excel(excel_path, index=False)
|
89 |
-
|
90 |
-
except Exception as e:
|
91 |
-
print(f"An error occurred while processing the PDF: {e}")
|
92 |
-
return None
|
93 |
-
|
94 |
-
# Log warning if data was not found for Purchase Order No or Date
|
95 |
-
if purchase_order_no == "Not Found" or purchase_order_date == "Not Found":
|
96 |
-
print("Warning: 'Purchase Order No' or 'Date' was not found in the PDF.")
|
97 |
|
98 |
-
return
|
99 |
|
100 |
# Set up Gradio interface
|
101 |
iface = gr.Interface(
|
102 |
fn=extract_data,
|
103 |
-
inputs=gr.File(label="Upload
|
104 |
-
outputs=gr.File(label="Download Excel"),
|
105 |
-
title="
|
106 |
)
|
107 |
|
108 |
# Launch the app
|
|
|
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
3 |
import re
|
|
|
4 |
|
5 |
+
# Define function to extract data from Excel file
|
6 |
+
def extract_data(excel_file):
|
7 |
+
# Load the Excel file
|
8 |
+
df = pd.read_excel(excel_file)
|
9 |
+
|
10 |
+
# Attempt to extract 'Purchase Order No' and 'Date' from the first few rows
|
11 |
+
for _, row in df.iterrows():
|
12 |
+
# Search for Purchase Order No pattern in the row data
|
13 |
+
po_match = re.search(r'Purchase Order No[:\s]+(\w+)', str(row), re.IGNORECASE)
|
14 |
+
if po_match:
|
15 |
+
purchase_order_no = po_match.group(1)
|
16 |
+
|
17 |
+
# Search for Date pattern in the row data (e.g., "Date: 10.10.2023" or "10/10/2023")
|
18 |
+
date_match = re.search(r'Date[:\s]+(\d{2}[\./-]\d{2}[\./-]\d{4})', str(row), re.IGNORECASE)
|
19 |
+
if date_match:
|
20 |
+
purchase_order_date = date_match.group(1)
|
21 |
+
|
22 |
+
# Stop if both values are found
|
23 |
+
if purchase_order_no != "Not Found" and purchase_order_date != "Not Found":
|
24 |
+
break
|
25 |
+
|
26 |
+
# Required columns to keep
|
27 |
+
columns_to_keep = ["Purchase Order No", "Date", "SI No", "Material Description",
|
28 |
+
"Unit", "Quantity", "Dely Qty", "Dely Date", "Unit Rate", "Value"]
|
29 |
+
|
30 |
+
# Add Purchase Order No and Date columns to the DataFrame if they are missing
|
31 |
+
if "Purchase Order No" not in df.columns:
|
32 |
+
df["Purchase Order No"] = purchase_order_no
|
33 |
+
if "Date" not in df.columns:
|
34 |
+
df["Date"] = purchase_order_date
|
35 |
+
|
36 |
+
# Filter the DataFrame to only include relevant columns
|
37 |
+
df_filtered = df[columns_to_keep]
|
38 |
+
|
39 |
+
# Save the filtered data to a new Excel file
|
40 |
+
output_path = "/tmp/Filtered_Purchase_Order_Data.xlsx"
|
41 |
+
df_filtered.to_excel(output_path, index=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
return output_path
|
44 |
|
45 |
# Set up Gradio interface
|
46 |
iface = gr.Interface(
|
47 |
fn=extract_data,
|
48 |
+
inputs=gr.File(label="Upload Excel File"),
|
49 |
+
outputs=gr.File(label="Download Filtered Excel"),
|
50 |
+
title="Excel Data Extractor"
|
51 |
)
|
52 |
|
53 |
# Launch the app
|