Spaces:
Build error
Build error
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -2,8 +2,16 @@ import pandas as pd
|
|
| 2 |
import json
|
| 3 |
|
| 4 |
def parse_excel_file(file_path):
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
| 7 |
column_mapping = {
|
| 8 |
"کد": ["کد فهرست بها", "کد"],
|
| 9 |
"عنوان": ["عنوان", "شرح کالا", "نام کالا"],
|
|
@@ -20,12 +28,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 +46,11 @@ 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 |
+
"""
|
| 6 |
+
خواندن فایل اکسل و استخراج اطلاعات مواد از ردیف سوم.
|
| 7 |
+
فقط موادی با تعداد معتبر (بزرگتر از صفر) نگه داشته میشوند.
|
| 8 |
+
"""
|
| 9 |
+
try:
|
| 10 |
+
df = pd.read_excel(file_path, header=2)
|
| 11 |
+
except Exception as e:
|
| 12 |
+
raise ValueError(f"❌ خطا در خواندن فایل اکسل: {str(e)}")
|
| 13 |
|
| 14 |
+
# نگاشت نام ستونها
|
| 15 |
column_mapping = {
|
| 16 |
"کد": ["کد فهرست بها", "کد"],
|
| 17 |
"عنوان": ["عنوان", "شرح کالا", "نام کالا"],
|
|
|
|
| 28 |
|
| 29 |
materials = []
|
| 30 |
for _, row in df.iterrows():
|
| 31 |
+
try:
|
| 32 |
+
quantity = float(row[resolved_columns["تعداد"]])
|
| 33 |
+
if quantity <= 0 or pd.isna(quantity):
|
| 34 |
+
continue
|
| 35 |
+
except:
|
| 36 |
+
continue
|
| 37 |
+
|
| 38 |
material = {
|
| 39 |
"code": str(row[resolved_columns["کد"]]).strip(),
|
| 40 |
"description": str(row[resolved_columns["عنوان"]]).strip(),
|
| 41 |
+
"quantity": quantity,
|
| 42 |
"unit": str(row[resolved_columns["واحد"]]).strip()
|
| 43 |
}
|
| 44 |
materials.append(material)
|
|
|
|
| 46 |
return materials
|
| 47 |
|
| 48 |
def save_to_json(data, output_path="material_db.json"):
|
| 49 |
+
"""
|
| 50 |
+
ذخیره دادهها به صورت فایل JSON.
|
| 51 |
+
"""
|
| 52 |
+
try:
|
| 53 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
| 54 |
+
json.dump(data, f, indent=2, ensure_ascii=False)
|
| 55 |
+
except Exception as e:
|
| 56 |
+
raise ValueError(f"❌ خطا در ذخیره فایل JSON: {str(e)}")
|