控制网复测平面基准归算程序(包含控制网复测平面基准计算,平面控制网稳定性计算,水准测段高差稳定计算三个程序功能)
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

GC.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import openpyxl
  2. import sqlite3
  3. import os
  4. import pandas as pd
  5. from openpyxl import Workbook
  6. from PySide6.QtWidgets import QApplication, QMessageBox
  7. # 添加文件转换函数
  8. def convert_xls_to_xlsx(file_path):
  9. # 使用 pandas 读取 .xls 文件
  10. df = pd.read_excel(file_path, engine='xlrd')
  11. # 创建一个新的 .xlsx 文件路径
  12. xlsx_file_path = file_path.replace('.xls', '.xlsx')
  13. # 将数据保存为 .xlsx 文件
  14. df.to_excel(xlsx_file_path, index=False)
  15. return xlsx_file_path
  16. # 弹窗提示用户 .xls 文件将转换为 .xlsx 文件
  17. def confirm_xls_to_xlsx_conversion(file_name):
  18. response = QMessageBox.question(None, "确认转换",
  19. f".xls文件将默认转换为.xlsx文件进行计算,系统会重新生成一个.xlsx文件副本用以计算,不会删除您本来的.xls文件,点击“OK”开始上传文件,点击“Cancel”取消上传文件",
  20. QMessageBox.Ok | QMessageBox.Cancel)
  21. return response == QMessageBox.Ok
  22. # 读取指定单元格的数据
  23. def read_cells_from_excel(cells, sheet):
  24. """
  25. 读取指定单元格的数据并返回字典。
  26. """
  27. cell_values = {}
  28. for cell in cells:
  29. cell_values[cell] = sheet[cell].value
  30. return cell_values
  31. # 获取最后一行有数字的单元格值
  32. def get_last_numeric_cell_value(sheet, column):
  33. """
  34. 获取指定列的最后一行有数字的单元格的值。
  35. """
  36. last_row = sheet.max_row
  37. for row in range(last_row, 0, -1):
  38. cell_value = sheet[f"{column}{row}"].value
  39. if isinstance(cell_value, (int, float)):
  40. return cell_value
  41. return None
  42. # 计算指定列中数值型数据的总和
  43. def calculate_column_sum(sheet, column):
  44. """
  45. 计算指定列中数值型数据的总和。
  46. """
  47. total_sum = 0
  48. for row in range(1, sheet.max_row + 1):
  49. cell_value = sheet[f"{column}{row}"].value
  50. if isinstance(cell_value, (int, float)):
  51. total_sum += cell_value
  52. return total_sum
  53. # 读取Excel文件并计算公式结果
  54. def load_and_calculate_excel(file_path):
  55. # 加载Excel文件并计算单元格H1的公式结果。
  56. workbook = openpyxl.load_workbook(file_path, data_only=True)
  57. sheet = workbook.active
  58. h1_value = sheet['H1'].value
  59. return h1_value
  60. # 弹窗提示用户是否更新数据
  61. def ask_for_update(file_name):
  62. response = QMessageBox.question(None, "提示", f"检测到数据库中存在相同文件 '{file_name}',是(Yes)否(No)更新数据?",
  63. QMessageBox.Yes | QMessageBox.No)
  64. return response == QMessageBox.Yes
  65. # 数据库插入函数,插入数据到GC_Input_Param中
  66. def insert_into_database(file_name, cell_values, ms_station, last_station_count, new_station_count, last_sum_hdiff,
  67. new_sum_hdiff,
  68. last_sum_rlen, new_sum_rlen, db_path, file_name_utf8):
  69. """
  70. 将文件名和结果名称插入或更新到数据库的GC_Input_Param表中。
  71. """
  72. database = db_path
  73. conn = sqlite3.connect(database=database)
  74. cursor = conn.cursor()
  75. try:
  76. # 查询是否已存在相同的记录
  77. cursor.execute(
  78. "SELECT * FROM GC_Input_Param WHERE TableName = ?",
  79. (file_name_utf8,)
  80. )
  81. existing_record = cursor.fetchone()
  82. if existing_record:
  83. # 弹窗询问用户是否更新数据
  84. if not ask_for_update(file_name):
  85. print("用户选择不更新数据")
  86. return
  87. # 更新现有记录
  88. cursor.execute(
  89. """
  90. UPDATE GC_Input_Param
  91. SET Last_ResultName = ?, ObservationLevel = ?, New_ResultName = ?, Ms_Station = ?,
  92. Last_StationCount = ?, New_StationCount = ?, Last_SumHDiff = ?, New_SumHDiff = ?,
  93. Last_SumRLen = ?, New_SumRLen = ?
  94. WHERE TableName = ?
  95. """,
  96. (cell_values['D1'], cell_values['C1'], cell_values['I1'], ms_station, last_station_count,
  97. new_station_count, last_sum_hdiff, new_sum_hdiff, last_sum_rlen, new_sum_rlen, file_name_utf8)
  98. )
  99. QMessageBox.information(None, "提示",
  100. f"文件 '{file_name}' 已成功更新到本地数据库中,点击'OK'以关闭此对话框。")
  101. else:
  102. # 插入新记录
  103. cursor.execute(
  104. """
  105. INSERT INTO GC_Input_Param (TableName, Last_ResultName, ObservationLevel, New_ResultName, Ms_Station,
  106. Last_StationCount, New_StationCount, Last_SumHDiff, New_SumHDiff,
  107. Last_SumRLen, New_SumRLen)
  108. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  109. """,
  110. (
  111. file_name_utf8, cell_values['D1'], cell_values['C1'], cell_values['I1'], ms_station,
  112. last_station_count,
  113. new_station_count, last_sum_hdiff, new_sum_hdiff, last_sum_rlen, new_sum_rlen)
  114. )
  115. QMessageBox.information(None, "提示",
  116. f"文件 '{file_name}' 已成功添加到本地数据库中,点击'OK'以关闭此对话框。")
  117. conn.commit()
  118. except sqlite3.Error as e:
  119. print(f"插入或更新文件名时发生错误: {e}")
  120. except Exception as e:
  121. print(f"处理文件时发生错误: {e}")
  122. finally:
  123. conn.close()
  124. # 遍历获取序号的数据
  125. def get_data_from_excel(file_path):
  126. """
  127. 从 Excel 文件中获取对应列的数据。
  128. """
  129. workbook = openpyxl.load_workbook(file_path)
  130. sheet = workbook.active
  131. data = []
  132. for row in range(3, sheet.max_row + 1):
  133. a_value = sheet[f"A{row}"].value
  134. b_value = sheet[f"B{row}"].value
  135. c_value = sheet[f"C{row}"].value
  136. d_value = sheet[f"D{row}"].value
  137. e_value = sheet[f"E{row}"].value
  138. f_value = sheet[f"F{row}"].value
  139. g_value = sheet[f"G{row}"].value
  140. h_value = sheet[f"H{row}"].value
  141. i_value = sheet[f"I{row}"].value
  142. j_value = sheet[f"J{row}"].value
  143. if a_value is not None or f_value is not None:
  144. data.append((a_value, b_value, c_value, d_value, e_value, f_value, g_value, h_value, i_value, j_value))
  145. return data
  146. # 插入点数据到GC_Input_Point中
  147. def insert_data_into_database(data, file_name, cell_values, db_path, file_name_utf8):
  148. """
  149. 将数据插入到数据库表 GC_Input_Point 中。如果 TableName 相同,则清空表,
  150. 然后插入新的数据。
  151. """
  152. database = db_path
  153. conn = sqlite3.connect(database=database)
  154. cursor = conn.cursor()
  155. try:
  156. # 检查是否已存在相同的 TableName
  157. cursor.execute(
  158. "SELECT * FROM GC_Input_Point WHERE TableName = ?",
  159. (file_name_utf8,)
  160. )
  161. existing_record = cursor.fetchone()
  162. if existing_record:
  163. # 如果存在相同的 TableName,清空表
  164. cursor.execute("DELETE FROM GC_Input_Point WHERE TableName = ?", (file_name_utf8,))
  165. conn.commit()
  166. # 插入新数据
  167. for a_value, b_value, c_value, d_value, e_value, f_value, g_value, h_value, i_value, j_value in data:
  168. cursor.execute(
  169. """
  170. INSERT INTO GC_Input_Point (Last_ID, Last_SPName, Last_EPName,Last_HDiff,Last_RLen, New_ID, New_SPName, New_EPName,New_HDiff,New_RLen, TableName, Last_ResultName, New_ResultName)
  171. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  172. """,
  173. (a_value, b_value, c_value, d_value, e_value, f_value, g_value, h_value, i_value, j_value,
  174. file_name_utf8,
  175. cell_values['D1'],
  176. cell_values['I1'])
  177. )
  178. conn.commit()
  179. except sqlite3.Error as e:
  180. print(f"插入或更新点数据时发生错误: {e}")
  181. except Exception as e:
  182. print(f"处理文件时发生错误: {e}")
  183. finally:
  184. conn.close()
  185. # 读取指定单元格的数据(编辑)
  186. def read_cells_from_excel1(list1):
  187. cell_values = {}
  188. cell_values['D1'] = list1[0][1]
  189. cell_values['C1'] = list1[0][11]
  190. cell_values['I1'] = list1[0][6]
  191. return cell_values
  192. # 获取最后一行有数字的单元格值(编辑)
  193. def get_last_numeric_cell_value1(list1, index):
  194. ii = 0
  195. while ii < len(list1):
  196. if list1[ii][index] == '' or list1[ii][index] == ' ' or list1[ii][index] == None:
  197. ii = ii + 1
  198. break
  199. else:
  200. ii = ii + 1
  201. return ii
  202. # 计算指定列中数值型数据的总和(编辑)
  203. def calculate_column_sum1(list1, index):
  204. sum = 0
  205. for li in list1:
  206. if li[index] == '' or li[index] == ' ' or li[index] == None:
  207. break
  208. else:
  209. sum = sum + float(li[index])
  210. return sum
  211. # 遍历获取序号的数据(修改数据)
  212. def get_data_from_excel1(list1):
  213. data = []
  214. for row in range(len(list1)):
  215. a_value = list1[row][0]
  216. b_value = list1[row][2]
  217. c_value = list1[row][3]
  218. try:
  219. d_value = float(list1[row][4])
  220. e_value = float(list1[row][5])
  221. except:
  222. d_value = None
  223. e_value = None
  224. f_value = list1[row][0]
  225. g_value = list1[row][7]
  226. h_value = list1[row][8]
  227. try:
  228. i_value = float(list1[row][9])
  229. j_value = float(list1[row][10])
  230. except:
  231. i_value = None
  232. j_value = None
  233. if a_value is not None or f_value is not None:
  234. data.append((a_value, b_value, c_value, d_value, e_value, f_value, g_value, h_value, i_value, j_value))
  235. return data
  236. # 主函数 读取excel文件
  237. def main_function(file_path, db_path):
  238. # 检查文件扩展名
  239. if file_path.endswith('.xls'):
  240. # 弹窗提示用户 .xls 文件将转换为 .xlsx 文件
  241. if not confirm_xls_to_xlsx_conversion(os.path.basename(file_path)):
  242. print("用户取消了转换操作")
  243. return
  244. # 转换 .xls 文件为 .xlsx 文件
  245. file_path = convert_xls_to_xlsx(file_path)
  246. # 调用读取Excel文件的函数
  247. file_name = os.path.basename(file_path)
  248. file_name_utf8 = file_name.encode('utf-8')
  249. # 加载工作簿
  250. workbook = openpyxl.load_workbook(file_path)
  251. sheet = workbook.active
  252. # 计算单元格 H1 的公式结果
  253. ms_station = load_and_calculate_excel(file_path)
  254. # 读取指定单元格的数据
  255. cells_to_read = ['D1', 'C1', 'I1']
  256. cell_values = read_cells_from_excel(cells_to_read, sheet)
  257. # 获取最后一行有数字的单元格值
  258. last_station_count = get_last_numeric_cell_value(sheet, 'A')
  259. new_station_count = get_last_numeric_cell_value(sheet, 'F')
  260. # 计算 D 列和 I 列中数值型数据的总和 总高差
  261. last_sum_hdiff = calculate_column_sum(sheet, 'D')
  262. new_sum_hdiff = calculate_column_sum(sheet, 'I')
  263. # 总路线长
  264. last_sum_rlen = calculate_column_sum(sheet, 'E')
  265. new_sum_rlen = calculate_column_sum(sheet, 'J')
  266. # 插入点的数组
  267. data = get_data_from_excel(file_path)
  268. # 调用插入数据库的函数
  269. if file_name:
  270. insert_into_database(file_name, cell_values, ms_station, last_station_count, new_station_count, last_sum_hdiff,
  271. new_sum_hdiff, last_sum_rlen, new_sum_rlen, db_path, file_name_utf8)
  272. insert_data_into_database(data, file_name, cell_values, db_path, file_name_utf8)
  273. else:
  274. print("没有数据可插入数据库")