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