Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,34 +1,21 @@
|
|
| 1 |
-
import json
|
| 2 |
import pandas as pd
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
def build_db_from_excel(file_path, description_key):
|
| 8 |
df = pd.read_excel(file_path)
|
| 9 |
-
df = df.fillna("-")
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
for _, row in df.iterrows():
|
| 13 |
-
|
| 14 |
-
"
|
| 15 |
-
"
|
| 16 |
-
"quantity": float(row
|
| 17 |
-
"unit": str(row
|
| 18 |
}
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
return {description_key: items}
|
| 22 |
-
|
| 23 |
-
def save_material_db(data):
|
| 24 |
-
with open(DB_FILE, "w", encoding="utf-8") as f:
|
| 25 |
-
json.dump(data, f, ensure_ascii=False, indent=2)
|
| 26 |
-
|
| 27 |
-
def load_material_db():
|
| 28 |
-
if not os.path.exists(DB_FILE):
|
| 29 |
-
return {}
|
| 30 |
-
with open(DB_FILE, "r", encoding="utf-8") as f:
|
| 31 |
-
return json.load(f)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
return db.get(description, [])
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
|
|
|
| 2 |
|
| 3 |
+
def parse_excel_file(file_path):
|
|
|
|
|
|
|
| 4 |
df = pd.read_excel(file_path)
|
|
|
|
| 5 |
|
| 6 |
+
required_columns = ["کد فهرست بها", "شرح", "مقدار", "واحد"]
|
| 7 |
+
for col in required_columns:
|
| 8 |
+
if col not in df.columns:
|
| 9 |
+
raise ValueError(f"ستون «{col}» در فایل پیدا نشد.")
|
| 10 |
+
|
| 11 |
+
materials = []
|
| 12 |
for _, row in df.iterrows():
|
| 13 |
+
material = {
|
| 14 |
+
"code": str(row["کد فهرست بها"]).strip(),
|
| 15 |
+
"description": str(row["شرح"]).strip(),
|
| 16 |
+
"quantity": float(row["مقدار"]),
|
| 17 |
+
"unit": str(row["واحد"]).strip()
|
| 18 |
}
|
| 19 |
+
materials.append(material)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
return materials
|
|
|