Reza-galaxy21 commited on
Commit
cf202e9
·
verified ·
1 Parent(s): bd68915

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +18 -8
utils.py CHANGED
@@ -3,18 +3,28 @@ import pandas as pd
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
 
 
3
  def parse_excel_file(file_path):
4
  df = pd.read_excel(file_path)
5
 
6
+ # نگاشت نام‌های مختلف ستون‌ها برای انعطاف بیشتر
7
+ column_mapping = {
8
+ "کد فهرست بها": ["کد فهرست بها", "کد"],
9
+ "شرح": ["شرح"],
10
+ "مقدار": ["مقدار"],
11
+ "واحد": ["واحد"]
12
+ }
13
+
14
+ resolved_columns = {}
15
+ for key, options in column_mapping.items():
16
+ found = next((col for col in options if col in df.columns), None)
17
+ if not found:
18
+ raise ValueError(f"ستون «{key}» در فایل پیدا نشد.")
19
+ resolved_columns[key] = found
20
 
21
  materials = []
22
  for _, row in df.iterrows():
23
  material = {
24
+ "code": str(row[resolved_columns["کد فهرست بها"]]).strip(),
25
+ "description": str(row[resolved_columns["شرح"]]).strip(),
26
+ "quantity": float(row[resolved_columns["مقدار"]]),
27
+ "unit": str(row[resolved_columns["واحد"]]).strip()
28
  }
29
  materials.append(material)
30