excelToJson.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 %}<end>",
  96. "出水氨氮连续检测":"<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 %}<end>",
  97. "出水总氮连续检测1":"<begin>\n碳源投加量: 当前值是{{tyjyl}},建议调整值是{{tyjyl_wentai}}。\n内回流⽐:当前值是{{r}},建议调整值是{{r_wentai}}。<end>",
  98. "出水总氮连续检测2":"<begin>\n碳源投加量: 当前值是{{tyjyl}},建议调整值是{{tyjyl_wentai}}。\n内回流⽐:当前值是{{r}},建议调整值是{{r_wentai}}。<end>",
  99. "出水总氮":"<begin>\n碳源投加量: 当前值是{{tyjyl}},建议调整值是{{tyjyl_wentai}}。\n内回流⽐:当前值是{{r}},建议调整值是{{r_wentai}}。<end>",
  100. "出水总磷":"<begin>\n除磷药剂投加量: 当前值是{{cltjl}},建议调整值是{{cltjl_wentai}}。\n干污泥量: 当前值是{{gwnl}},建议调整值是{{gwnl_wentai}}。<end>",
  101. "出水正磷酸盐":"<begin>\n除磷药剂投加量: 当前值是{{cltjl}},建议调整值是{{cltjl_wentai}}。\n干污泥量: 当前值是{{gwnl}},建议调整值是{{gwnl_wentai}}。<end>",
  102. "出水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 %}<end>",
  103. "出水SS":"<begin>\n{% for item in mlss_items -%}\n池{{loop.index}}生化池污泥浓度 MLSS:当前值{{item.MLSS}},建议调整值{{item.MLSS_wentai}}。\n{%- endfor %}<end>",
  104. "进水氨氮":"<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>",
  105. "进水总氮":"<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>",
  106. "进水总磷":"<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>",
  107. "进水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>",
  108. "进水SS":"<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}}。<end>",
  109. }
  110. data_desc= {
  111. "进水COD":'',
  112. "进水总磷":'',
  113. "进水总氮":'',
  114. "进水氨氮":'',
  115. "进水SS":'',
  116. "出水COD":'已知数据:出水COD标准值是40;历史7天进水COD数据{{COD_DATA}};历史7天进水数量{WATER_DATA}}; ',
  117. "出水总磷":'已知数据:出水总磷标准值是0.5;历史7天二沉池正磷酸盐数据:{{ZL_DATA}};历史7天进水总磷数据{{TP_DATA}};历史7天进水数量{{WATER_DATA}};',
  118. "出水总氮":'已知数据:出水总氮标准值是15;历史7天进水总氮数据{{TN_DATA}};历史7天进水数量{{WATER_DATA}};',
  119. "出水氨氮":'已知数据:出水氨氮标准值是4;历史7天进水氨氮数据{{andan_DATA}};历史7天进水数量{{WATER_DATA}}; ',
  120. "出水SS":'已知数据:出水SS标准值是10;历史7天进水SS数据{{SS_DATA}};历史7天进水数量{{WATER_DATA}}; ',
  121. "出水总氮连续检测1":'已知数据:出水总氮标准值是15;历史7天进水总氮数据{{TN_DATA}};历史7天进水数量{{WATER_DATA}};',
  122. "出水总氮连续检测2":'已知数据:出水总氮标准值是15;历史7天进水总氮数据{{TN_DATA}};历史7天进水数量{{WATER_DATA}};',
  123. "出水氨氮连续检测":'已知数据:出水氨氮标准值是4;历史7天进水氨氮数据{{andan_DATA}};历史7天进水数量{{WATER_DATA}}; ',
  124. "出水正磷酸盐":'已知数据:出水总磷标准值是0.5;历史7天二沉池正磷酸盐数据:{{ZL_DATA}};历史7天进水总磷数据{{TP_DATA}};历史7天进水数量{{WATER_DATA}};'
  125. }
  126. nameMap = {
  127. "进水COD":'js_cod',
  128. "进水总磷":'js_tp',
  129. "进水总氮":'js_tn',
  130. "进水氨氮":'js_andan',
  131. "进水SS":'js_ss',
  132. "出水COD":'cs_cod',
  133. "出水总磷":'cs_tp',
  134. "出水总氮":'cs_tn',
  135. "出水氨氮":'cs_andan',
  136. "出水SS":'cs_ss',
  137. "出水总氮连续检测1":'cs_tn_lxjc1',
  138. "出水总氮连续检测2":'cs_tn_lxjc2',
  139. "出水氨氮连续检测":'cs_andan_lxjc1',
  140. "出水正磷酸盐":'cs_tp_lxjc'
  141. }
  142. def parse_excel(sheet):
  143. cols = sheet.ncols
  144. call_tables = []
  145. system = ''
  146. boot=[]
  147. n=0
  148. for col in range(cols):
  149. if sheet.cell(0, col).value in ['rule', 'system']:
  150. system = sheet.cell(1, col).value
  151. n=col
  152. continue
  153. col_data = sheet.col_values(col)
  154. for idx, cell_value in enumerate(col_data):
  155. if idx == 0 or cell_value == '':
  156. continue
  157. id_val = f"{col}_{idx + 1}"
  158. if col== n+1:
  159. boot.append(id_val)
  160. main_type = "alert" if "alert" in cell_value else "export" if "export" in cell_value else "text"
  161. options = []
  162. is_merged, merged_start_row, merged_end_row = check_merged(sheet, idx, col)
  163. if is_merged and "export" not in cell_value:
  164. options_cells = sheet.col_slice(col + 1, start_rowx=merged_start_row, end_rowx=merged_end_row)
  165. two_datas = [cell_value.value for cell_value in options_cells]
  166. for indTwo, item in enumerate(two_datas):
  167. if not item.strip():
  168. continue
  169. item_two = {
  170. "option": re.sub(r'{{(?:export|alert)}}\n', "", item.strip()),
  171. "next": []
  172. }
  173. is_two_merged, merged_start, merged_end = check_merged(sheet, indTwo + merged_start_row, col + 1)
  174. if is_two_merged:
  175. threed_cells = sheet.col_slice(col + 2, start_rowx=merged_start, end_rowx=merged_end)
  176. threed_datas = [new_cell.value for new_cell in threed_cells]
  177. for ind_three, item_three in enumerate(threed_datas):
  178. if not item_three.strip():
  179. continue
  180. item_two['next'].append(f"{col + 2}_{ind_three + merged_start + 1}")
  181. else:
  182. item_two['next'].append(f"{col + 2}_{indTwo + merged_start_row + 1}")
  183. options.append(item_two)
  184. item = {
  185. "id": id_val,
  186. "mainType": main_type,
  187. "mainContent": re.sub(r'{{(?:export|alert)}}', "", cell_value.strip()).strip(),
  188. "options": options
  189. }
  190. call_tables.append(item)
  191. result = {
  192. "norm": nameMap[sheet.name],
  193. "system": system,
  194. "prompt": prompt[sheet.name],
  195. 'data_desc': data_desc[sheet.name],
  196. "boot": boot,
  197. "questions": call_tables
  198. }
  199. return result
  200. def getAllSheets(file_path):
  201. data = xlrd.open_workbook(file_path)
  202. sheets = data.sheets()
  203. directory = "./excel/json/"
  204. for item in sheets:
  205. sheet_name = nameMap[item.name]
  206. parsed_data = parse_excel(item)
  207. out_file_path = os.path.join(directory, str(sheet_name+'.json'))
  208. with open(out_file_path, "w", encoding="utf-8") as json_file:
  209. json.dump(parsed_data, json_file, ensure_ascii=False, indent=4)
  210. getAllSheets("./excel/juece2.0/报警决策流_new.xlsx")