excelToJson.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # import xlrd
  2. # import json
  3. # import re
  4. # data1 = xlrd.open_workbook("../excel/进水总氮限制答案合并.xlsx")
  5. # sheet1 = data1.sheets()[0]
  6. # rows = sheet1.nrows
  7. # cols = sheet1.ncols
  8. # header = sheet1.row_values(0)
  9. # callTables = []
  10. # def mergefun(n,idx):
  11. # # 判断当前单元格是否属于合并单元格
  12. # is_merged = False
  13. # merged_start_row = merged_end_row = None
  14. # for rlo, rhi, clo, chi in sheet1.merged_cells:
  15. # if clo == n and rlo <= idx < rhi:
  16. # # print(rlo, rhi, clo, chi, n)
  17. # is_merged = True
  18. # merged_start_row = rlo
  19. # merged_end_row = rhi
  20. # break
  21. # return [is_merged,merged_start_row,merged_end_row]
  22. # for n in range(cols):
  23. # if n > 1:
  24. # # 获取第 n 列的所有值
  25. # col_data = sheet1.col_values(n)
  26. # # 循环每一行
  27. # for idx, cell_value in enumerate(col_data):
  28. # # 剔除为空的值和第一行的值
  29. # if idx == 0 or cell_value == '':
  30. # continue
  31. # # 获取当前单元格
  32. # cell = sheet1.cell(idx, n)
  33. # id_val = str(n) + '_' + str(idx + 1) # 使用列号和行号组合作为唯一ID
  34. # mainType = "alert" if "alert" in cell_value else "export" if "export" in cell_value else "text"
  35. # options = []
  36. # twoDatas = []
  37. # threedDatas=[]
  38. # # 判断当前单元格是否属于合并单元格
  39. # is_merged = False
  40. # merged_start_row = merged_end_row = None
  41. # for rlo, rhi, clo, chi in sheet1.merged_cells:
  42. # if clo == n and rlo <= idx+1 < rhi:
  43. # is_merged = True
  44. # merged_start_row = rlo
  45. # merged_end_row = rhi
  46. # break
  47. # # 处理合并单元格
  48. # if is_merged and "export" not in cell_value:
  49. # # print(f"第{n}列 第{idx+ 1}行 是合并单元格,合并范围:{merged_start_row + 1}行到{merged_end_row}行")
  50. # options_cells = sheet1.col_slice(n + 1, start_rowx=merged_start_row, end_rowx=merged_end_row)
  51. # twoDatas.extend([cell_value.value for cell_value in options_cells])
  52. # # print(twoDatas, n)
  53. # for indTwo, item in enumerate(twoDatas):
  54. # if not item.strip():
  55. # continue
  56. # itemTwo = {
  57. # "option": re.sub(r'{{(?:export|alert)}}\n', "", item),
  58. # "next": []
  59. # }
  60. # # 找到当前单元格的合并信息
  61. # isTwo_merged, merged_start, merged_end = mergefun(n+1, indTwo + merged_start_row)
  62. # if isTwo_merged:
  63. # threed_cells = sheet1.col_slice(n + 2, start_rowx=merged_start, end_rowx=merged_end)
  64. # threedDatas.clear()
  65. # threedDatas.extend([newCell.value for newCell in threed_cells])
  66. # for indThree, itemThree in enumerate(threedDatas):
  67. # if not itemThree.strip():
  68. # continue
  69. # itemTwo['next'].append(str(n + 2) + '_' + str(indThree + merged_start + 1))
  70. # else:
  71. # itemTwo['next'].append(str(n + 2) + '_' + str(indTwo + merged_start_row + 1))
  72. # options.append(itemTwo)
  73. # else:
  74. # options= []
  75. # item = {
  76. # "id": id_val,
  77. # "mainType": mainType,
  78. # "mainContent": re.sub(r'{{(?:export|alert)}}\n', "", cell_value),
  79. # "options": options
  80. # }
  81. # callTables.append(item)
  82. # # 将解析的数据保存为JSON文件
  83. # with open("parsed_dataTwo.json", "w", encoding="utf-8") as json_file:
  84. # json.dump(callTables, json_file, ensure_ascii=False, indent=4)
  85. import xlrd
  86. import json
  87. import re
  88. import os
  89. def check_merged(sheet, row, col):
  90. for rlo, rhi, clo, chi in sheet.merged_cells:
  91. if clo == col and rlo <= row < rhi:
  92. return True, rlo, rhi
  93. return False, None, None
  94. prompt = {
  95. "出水氨氮":"<begin>\n{% for item in doo_items -%}\n池{{loop.index}}好氧池溶解氧:当前值为{{item.DO_O}},建议调整值是{{item.DO_O_wentai}}。\n{%- endfor %}\n{% for item in mlss_items -%}\n池{{loop.index}}生化池污泥浓度 MLSS:当前值{{item.MLSS}},建议调整值是{{item.MLSS_wentai}}。\n{%- endfor %}\n进水水量:当前值是{{Q_in}},建议调整值是{{Q_in_wentai}}。<end>",
  96. "出水总氮":"<begin>\n{% for item in doo_items -%}\n好氧池{{loop.index}}溶解氧:当前值是{{item.DO_O}},建议调整值是{{item.DO_O_wentai}}。\n{%- endfor %}\n内回流⽐:当前值是{{r}},建议调整值是{{r_wentai}}。\n进水水量:当前值是{{Q_in}},建议调整值是{{Q_in_wentai}}。\n{% for item in mlss_items -%}\n池{{loop.index}}生化池污泥浓度 MLSS:当前值{{item.MLSS}},建议调整值是{{item.MLSS_wentai}}。\n{%- endfor %}\n碳源投加量: 当前值是{{tyjyl}},建议调整值是{{tyjyl_wentai}}。<end>",
  97. "出水总磷":"<begin>\n除磷药剂投加量: 当前值是{{cltjl}},建议调整值是{{cltjl_wentai}}。\n{% for item in doo_items -%}\n好氧池{{loop.index}}溶解氧:当前值是{{item.DO_O}},建议调整值是{{item.DO_O_wentai}}。\n{%- endfor %}\n{% for item in mlss_items -%}\n池{{loop.index}}生化池污泥浓度 MLSS:当前值{{item.MLSS}},建议调整值{{item.MLSS_wentai}}。\n{%- endfor %}\n进水水量:当前值是{{Q_in}},建议调整值是{{Q_in_wentai}}。\n干污泥量: 当前值是{{gwnl}},建议调整值是{{gwnl_wentai}}。<end>",
  98. "出水COD":"<begin>\n{% for item in doo_items -%}\n池{{loop.index}}好氧池溶解氧:当前值为{{item.DO_O}},建议调整值为{{item.DO_O_wentai}}。\n{%- endfor %}\n{% for item in mlss_items -%}\n池{{loop.index}}生化池污泥浓度 MLSS:当前值{{item.MLSS}},建议调整值为{{item.MLSS_wentai}}。\n{%- endfor %}\n进水水量:当前值是{{Q_in}},建议调整值是{{Q_in_wentai}}。<end>",
  99. "出水SS":"<begin>\n{% for item in mlss_items -%}\n池{{loop.index}}生化池污泥浓度 MLSS:当前值{{item.MLSS}},建议调整值{{item.MLSS_wentai}}。\n{%- endfor %}\n进水水量:当前值是{{Q_in}},建议调整值是{{Q_in_wentai}}。\n<end>",
  100. "进水氨氮":"<begin>\n{% for item in doo_items -%}\n好氧池{{loop.index}}溶解氧:当前值是{{item.DO_O}},建议调整值是{{item.DO_O_wentai}}。\n{%- endfor %}\n{% for item in mlss_items -%}\n池{{loop.index}}生化池污泥浓度 MLSS:当前值{{item.MLSS}},建议调整值{{item.MLSS_wentai}}。\n{%- endfor %}\n内回流⽐:当前值是{{r}},建议调整值是{{r_wentai}}。\n进水水量:当前值是{{Q_in}},建议调整值是{{Q_in_wentai}}。\n<end>",
  101. "进水总氮":"<begin>\n内回流⽐:当前值是{{r}},建议调整值是{{r_wentai}}。\n{% for item in mlss_items -%}\n池{{loop.index}}生化池污泥浓度 MLSS:当前值{{item.MLSS}},建议调整值{{item.MLSS_wentai}}。\n{%- endfor %}\n进水水量:当前值是{{Q_in}},建议调整值是{{Q_in_wentai}}。\n<end>",
  102. "进水总磷":"<begin>\n{% for item in mlss_items -%}\n池{{loop.index}}生化池污泥浓度 MLSS:当前值{{item.MLSS}},建议调整值{{item.MLSS_wentai}}。\n{%- endfor %}\n外回流⽐:当前值是{{R}},建议调整值是{{R_wentai}}。\n进水水量:当前值是{{Q_in}},建议调整值是{{Q_in_wentai}}。\n<end>",
  103. "进水COD":"<begin>\n{% for item in doo_items -%}\n好氧池{{loop.index}}溶解氧:当前值是{{item.DO_O}},建议调整值是{{item.DO_O_wentai}}。\n{%- endfor %}\n{% for item in mlss_items -%}\n池{{loop.index}}生化池污泥浓度 MLSS:当前值{{item.MLSS}},建议调整值{{item.MLSS_wentai}}。\n{%- endfor %}\n外回流⽐:当前值是{{R}},建议调整值是{{R_wentai}}。\n进水水量:当前值是{{Q_in}},建议调整值是{{Q_in_wentai}}。\n<end>",
  104. "进水SS":"<begin>\n{% for item in mlss_items -%}\n池{{loop.index}}生化池污泥浓度 MLSS:当前值{{item.MLSS}},建议调整值{{item.MLSS_wentai}}。\n{%- endfor %}\n进水水量:当前值是{{Q_in}},建议调整值是{{Q_in_wentai}}。\n外回流⽐:当前值是{{R}},稳态值是{{R_wentai}}。\n<end>",
  105. }
  106. def parse_excel(sheet):
  107. cols = sheet.ncols
  108. call_tables = []
  109. system = ''
  110. boot=[]
  111. n=0
  112. for col in range(cols):
  113. if sheet.cell(0, col).value in ['rule', 'system']:
  114. system = sheet.cell(1, col).value
  115. n=col
  116. continue
  117. col_data = sheet.col_values(col)
  118. for idx, cell_value in enumerate(col_data):
  119. if idx == 0 or cell_value == '':
  120. continue
  121. id_val = f"{col}_{idx + 1}"
  122. if col== n+1:
  123. boot.append(id_val)
  124. main_type = "alert" if "alert" in cell_value else "export" if "export" in cell_value else "text"
  125. options = []
  126. is_merged, merged_start_row, merged_end_row = check_merged(sheet, idx, col)
  127. if is_merged and "export" not in cell_value:
  128. options_cells = sheet.col_slice(col + 1, start_rowx=merged_start_row, end_rowx=merged_end_row)
  129. two_datas = [cell_value.value for cell_value in options_cells]
  130. for indTwo, item in enumerate(two_datas):
  131. if not item.strip():
  132. continue
  133. item_two = {
  134. "option": re.sub(r'{{(?:export|alert)}}\n', "", item.strip()),
  135. "next": []
  136. }
  137. is_two_merged, merged_start, merged_end = check_merged(sheet, indTwo + merged_start_row, col + 1)
  138. if is_two_merged:
  139. threed_cells = sheet.col_slice(col + 2, start_rowx=merged_start, end_rowx=merged_end)
  140. threed_datas = [new_cell.value for new_cell in threed_cells]
  141. for ind_three, item_three in enumerate(threed_datas):
  142. if not item_three.strip():
  143. continue
  144. item_two['next'].append(f"{col + 2}_{ind_three + merged_start + 1}")
  145. else:
  146. item_two['next'].append(f"{col + 2}_{indTwo + merged_start_row + 1}")
  147. options.append(item_two)
  148. item = {
  149. "id": id_val,
  150. "mainType": main_type,
  151. "mainContent": re.sub(r'{{(?:export|alert)}}', "", cell_value.strip()).strip(),
  152. "options": options
  153. }
  154. call_tables.append(item)
  155. result = {
  156. "norm": sheet.name,
  157. "system": system,
  158. "prompt": prompt[sheet.name],
  159. "boot":boot,
  160. "questions": call_tables
  161. }
  162. return result
  163. def getAllSheets(file_path):
  164. data = xlrd.open_workbook(file_path)
  165. sheets = data.sheets()
  166. directory = "./excel/json/"
  167. for item in sheets:
  168. sheetName = item.name
  169. parsed_data = parse_excel(item)
  170. file_path = os.path.join(directory, str(sheetName+'.json'))
  171. with open (file_path, "w", encoding="utf-8") as json_file:
  172. json.dump(parsed_data, json_file, ensure_ascii=False, indent=4)
  173. getAllSheets("./excel/juece2.0/报警决策流.xlsx")