ایجاد تبدیل اکسل به درخت
This commit is contained in:
+108
-37
@@ -1,59 +1,130 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from config_base import *
|
||||
from general_functions import *
|
||||
from text_normalizer import TextNormalizer
|
||||
# from excel_helper import ExcelHelper
|
||||
import pandas as pd
|
||||
import requests
|
||||
import json
|
||||
from text_normalizer import TextNormalizer
|
||||
from config_base import *
|
||||
|
||||
text_normalizer = TextNormalizer()
|
||||
#
|
||||
from general_functions import *
|
||||
|
||||
|
||||
ADD_LIST_ROUTE = "project/add"
|
||||
ADD_ITEM_ROUTE = "subject/add"
|
||||
GET_H = lambda x: x["HIERARCHY"]
|
||||
# TITLE = ""
|
||||
|
||||
def insertProejct(title):
|
||||
# ایجاد فهرست جدید
|
||||
|
||||
ADD_LIST_QUERY = {
|
||||
"title": title,
|
||||
"type": 3,
|
||||
"item_state": 1,
|
||||
"ispublic" : 1,
|
||||
}
|
||||
def read_excel_data(file_path ,sheet_name):
|
||||
df = pd.read_excel(file_path , sheet_name=sheet_name)
|
||||
data = df.to_dict(orient="records")
|
||||
|
||||
requests.post(BASE_URL + 'list/' + ADD_LIST_ROUTE, data=ADD_LIST_QUERY, headers = HEADERS)
|
||||
projectid = 6
|
||||
text_normal = TextNormalizer()
|
||||
for row in data:
|
||||
row["CAPTION"] = text_normal.normalize_qanon(row["عنوان"])
|
||||
row["ID"] = int(row["ردیف"])
|
||||
row["F_PARENTID"] = int(row["سطح"]) - 1 if row["سطح"] > 1 else 0
|
||||
row["CODE"] = row.get("کد", "")
|
||||
return data
|
||||
|
||||
def insert_project():
|
||||
# ADD_LIST_QUERY = {
|
||||
# "title": TITLE,
|
||||
# "type": 3,
|
||||
# "item_state": 1,
|
||||
# "ispublic": 1,
|
||||
# }
|
||||
|
||||
# requests.post(BASE_URL + 'list/' + ADD_LIST_ROUTE, data=ADD_LIST_QUERY, headers=HEADERS)
|
||||
projectid = 7 # فرض کنید projectid ثابت است
|
||||
return projectid
|
||||
|
||||
|
||||
def import_excel(elastic, file_excel):
|
||||
pass
|
||||
def insert_real(rows, projectid, IDS_DICT, childs):
|
||||
remains = []
|
||||
|
||||
for row in rows:
|
||||
try:
|
||||
f_parentid = row["F_PARENTID"]
|
||||
row_id = row["ID"]
|
||||
caption = row["CAPTION"]
|
||||
except KeyError:
|
||||
print("Missing data in row:", row)
|
||||
continue
|
||||
|
||||
# تعیین والد
|
||||
if f_parentid == 0:
|
||||
parent = 0
|
||||
elif str(f_parentid) in IDS_DICT:
|
||||
parent = IDS_DICT[str(f_parentid)]["id"]
|
||||
else:
|
||||
print(f"❗️ Parent not found for F_PARENTID={f_parentid}")
|
||||
remains.append(row)
|
||||
continue
|
||||
|
||||
# محاسبه شماره فرزند
|
||||
number = childs.get(parent, 0) + 1
|
||||
childs[parent] = number
|
||||
|
||||
# آمادهسازی درخواست
|
||||
ADD_ITEM_QUERY = {
|
||||
"title": caption,
|
||||
"projectid": projectid,
|
||||
"meta": row.get("CODE", ""),
|
||||
"childorder": number,
|
||||
"listtype": 0,
|
||||
"parent": parent,
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
BASE_URL + 'list/' + ADD_ITEM_ROUTE,
|
||||
data=ADD_ITEM_QUERY,
|
||||
headers=HEADERS
|
||||
)
|
||||
response.raise_for_status()
|
||||
res_json = response.json()
|
||||
new_id = res_json["data"][0]["id"]
|
||||
print(f"✅ {row_id}: {caption} -> ID={new_id}")
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to insert {caption}: {e}")
|
||||
remains.append(row)
|
||||
continue
|
||||
|
||||
IDS_DICT[str(row_id)] = {
|
||||
"id": new_id,
|
||||
**ADD_ITEM_QUERY
|
||||
}
|
||||
|
||||
return remains
|
||||
|
||||
|
||||
|
||||
|
||||
def process_excel_data(file_path , sheet_name):
|
||||
data = read_excel_data(file_path , sheet_name)
|
||||
data = sorted(data, key=lambda x: x["ID"])
|
||||
print(f"❗️ Parent not found for F_PARENTID={data}")
|
||||
IDS_DICT = {}
|
||||
childs = {}
|
||||
|
||||
projectid = insert_project()
|
||||
if not projectid:
|
||||
return
|
||||
|
||||
print("🚀 شروع درج دادهها")
|
||||
remains = insert_real(data, projectid, IDS_DICT, childs)
|
||||
|
||||
count = 1
|
||||
while remains and count < 3:
|
||||
print(f"\n🔁 تلاش مجدد {count}: باقیمانده {len(remains)} آیتم")
|
||||
remains = insert_real(remains, projectid, IDS_DICT, childs)
|
||||
count += 1
|
||||
|
||||
insertProejct('آموزش و پرورش')
|
||||
output_file = "real_ids_farhang_honar.json"
|
||||
with open(output_file, "w", encoding="utf-8") as file:
|
||||
json.dump(IDS_DICT, file, ensure_ascii=False, indent=4)
|
||||
|
||||
# load_dotenv(dotenv_path='config.env')
|
||||
print(f"\n✅ تمام دادهها ثبت شدند و در {output_file} ذخیره شدند.")
|
||||
|
||||
# host=os.getenv('HOST_160')
|
||||
# password=os.getenv('HOST_160_PASS')
|
||||
|
||||
# # host=os.getenv('HOST_112')
|
||||
# # password=os.getenv('HOST_112_PASS')
|
||||
|
||||
|
||||
# path_base_map = "G:/Project2/gateway2_main/repo/_other/"
|
||||
# elastic = ElasticHelper(host, password, "elastic", path_base_map)
|
||||
|
||||
|
||||
# import_excel(elastic, "./data/plan.xlsx")
|
||||
if __name__ == "__main__":
|
||||
file_path = "H:\\personal\\Plan.xlsx"
|
||||
sheet_name = 'آموزش و پرورش'
|
||||
# sheet_name = 'مناطق آزاد'
|
||||
# sheet_name = 'فرهنگ و هنر'
|
||||
process_excel_data(file_path , sheet_name)
|
||||
|
||||
Reference in New Issue
Block a user