Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -2,7 +2,10 @@ import pandas as pd
|
|
| 2 |
import json
|
| 3 |
|
| 4 |
def parse_excel_file(file_path):
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
column_mapping = {
|
| 8 |
"کد": ["کد فهرست بها", "کد"],
|
|
@@ -20,12 +23,17 @@ def parse_excel_file(file_path):
|
|
| 20 |
|
| 21 |
materials = []
|
| 22 |
for _, row in df.iterrows():
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
material = {
|
| 26 |
"code": str(row[resolved_columns["کد"]]).strip(),
|
| 27 |
"description": str(row[resolved_columns["عنوان"]]).strip(),
|
| 28 |
-
"quantity":
|
| 29 |
"unit": str(row[resolved_columns["واحد"]]).strip()
|
| 30 |
}
|
| 31 |
materials.append(material)
|
|
@@ -33,5 +41,8 @@ def parse_excel_file(file_path):
|
|
| 33 |
return materials
|
| 34 |
|
| 35 |
def save_to_json(data, output_path="material_db.json"):
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
|
| 4 |
def parse_excel_file(file_path):
|
| 5 |
+
try:
|
| 6 |
+
df = pd.read_excel(file_path, header=2)
|
| 7 |
+
except Exception as e:
|
| 8 |
+
raise ValueError(f"❌ خطا در خواندن فایل اکسل: {str(e)}")
|
| 9 |
|
| 10 |
column_mapping = {
|
| 11 |
"کد": ["کد فهرست بها", "کد"],
|
|
|
|
| 23 |
|
| 24 |
materials = []
|
| 25 |
for _, row in df.iterrows():
|
| 26 |
+
try:
|
| 27 |
+
quantity = float(row[resolved_columns["تعداد"]])
|
| 28 |
+
if pd.isna(quantity) or quantity <= 0:
|
| 29 |
+
continue
|
| 30 |
+
except:
|
| 31 |
+
continue
|
| 32 |
+
|
| 33 |
material = {
|
| 34 |
"code": str(row[resolved_columns["کد"]]).strip(),
|
| 35 |
"description": str(row[resolved_columns["عنوان"]]).strip(),
|
| 36 |
+
"quantity": quantity,
|
| 37 |
"unit": str(row[resolved_columns["واحد"]]).strip()
|
| 38 |
}
|
| 39 |
materials.append(material)
|
|
|
|
| 41 |
return materials
|
| 42 |
|
| 43 |
def save_to_json(data, output_path="material_db.json"):
|
| 44 |
+
try:
|
| 45 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
| 46 |
+
json.dump(data, f, indent=2, ensure_ascii=False)
|
| 47 |
+
except Exception as e:
|
| 48 |
+
raise ValueError(f"❌ خطا در ذخیره فایل JSON: {str(e)}")
|