rmy 3 месяцев назад
Родитель
Сommit
409ca9c318
4 измененных файлов: 170 добавлений и 47 удалений
  1. 32
    2
      Back/GC/GC.py
  2. 20
    0
      Back/GC/GCExport.py
  3. 7
    0
      Back/GC/GCcompute.py
  4. 111
    45
      Back/GC/GCshow.py

+ 32
- 2
Back/GC/GC.py Просмотреть файл

@@ -1,10 +1,30 @@
1 1
 import openpyxl
2 2
 import sqlite3
3 3
 import os
4
+import pandas as pd
4 5
 from openpyxl import Workbook
5 6
 from PySide6.QtWidgets import QApplication, QMessageBox
6 7
 
7 8
 
9
+# 添加文件转换函数
10
+def convert_xls_to_xlsx(file_path):
11
+    # 使用 pandas 读取 .xls 文件
12
+    df = pd.read_excel(file_path, engine='xlrd')
13
+    # 创建一个新的 .xlsx 文件路径
14
+    xlsx_file_path = file_path.replace('.xls', '.xlsx')
15
+    # 将数据保存为 .xlsx 文件
16
+    df.to_excel(xlsx_file_path, index=False)
17
+    return xlsx_file_path
18
+
19
+
20
+# 弹窗提示用户 .xls 文件将转换为 .xlsx 文件
21
+def confirm_xls_to_xlsx_conversion(file_name):
22
+    response = QMessageBox.question(None, "确认转换",
23
+                                    f".xls文件将默认转换为.xlsx文件进行计算,系统会重新生成一个.xlsx文件副本用以计算,不会删除您本来的.xls文件,点击“OK”开始上传文件,点击“Cancel”取消上传文件",
24
+                                    QMessageBox.Ok | QMessageBox.Cancel)
25
+    return response == QMessageBox.Ok
26
+
27
+
8 28
 # 读取指定单元格的数据
9 29
 def read_cells_from_excel(cells, sheet):
10 30
     """
@@ -106,8 +126,9 @@ def insert_into_database(file_name, cell_values, ms_station, last_station_count,
106 126
                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
107 127
                 """,
108 128
                 (
109
-                file_name_utf8, cell_values['D1'], cell_values['C1'], cell_values['I1'], ms_station, last_station_count,
110
-                new_station_count, last_sum_hdiff, new_sum_hdiff, last_sum_rlen, new_sum_rlen)
129
+                    file_name_utf8, cell_values['D1'], cell_values['C1'], cell_values['I1'], ms_station,
130
+                    last_station_count,
131
+                    new_station_count, last_sum_hdiff, new_sum_hdiff, last_sum_rlen, new_sum_rlen)
111 132
             )
112 133
             QMessageBox.information(None, "提示",
113 134
                                     f"文件 '{file_name}' 已成功添加到本地数据库中,点击'OK'以关闭此对话框。对话框关闭后,请点击“计算”以计算数据")
@@ -243,6 +264,15 @@ def get_data_from_excel1(list1):
243 264
 
244 265
 # 主函数 读取excel文件
245 266
 def main_function(file_path, db_path):
267
+    # 检查文件扩展名
268
+    if file_path.endswith('.xls'):
269
+        # 弹窗提示用户 .xls 文件将转换为 .xlsx 文件
270
+        if not confirm_xls_to_xlsx_conversion(os.path.basename(file_path)):
271
+            print("用户取消了转换操作")
272
+            return
273
+
274
+        # 转换 .xls 文件为 .xlsx 文件
275
+        file_path = convert_xls_to_xlsx(file_path)
246 276
     # 调用读取Excel文件的函数
247 277
     file_name = os.path.basename(file_path)
248 278
     file_name_utf8 = file_name.encode('utf-8')

+ 20
- 0
Back/GC/GCExport.py Просмотреть файл

@@ -272,8 +272,26 @@ def export_initial_data(ui, utf_en, db_path, export_folder):
272 272
         else:
273 273
             QMessageBox.critical(ui, '错误', f'导出初始数据过程中发生错误: {str(e)}')
274 274
 
275
+def process_utf_en(utf_en):
276
+    # 确保 utf_en 是字节字符串
277
+    if not isinstance(utf_en, bytes):
278
+        utf_en = utf_en.encode('utf-8')
279
+
280
+    # 将字节字符串解码为普通字符串
281
+    file_name = utf_en.decode('utf-8')
282
+
283
+    # 检查文件后缀名并进行相应处理
284
+    if file_name.endswith('.xls'):
285
+        # 去掉 .xls 后缀并添加 .xlsx 后缀
286
+        file_name = file_name[:-4] + '.xlsx'
287
+
288
+    # 将处理后的字符串重新编码为字节字符串
289
+    utf_en = file_name.encode('utf-8')
290
+
291
+    return utf_en
275 292
 
276 293
 def main_function_initial(ui, utf_en, db_path):
294
+
277 295
     # 获取应用的安装目录
278 296
     app_install_dir = os.path.dirname(os.path.abspath(__file__))  # 假设脚本文件位于应用的安装目录下
279 297
     export_folder = os.path.join(app_install_dir, 'Export')
@@ -287,6 +305,8 @@ def main_function_initial(ui, utf_en, db_path):
287 305
 
288 306
 
289 307
 def main_function_result(ui, utf_en, db_path):
308
+    # 处理 utf_en
309
+    utf_en = process_utf_en(utf_en)
290 310
     # 获取应用的安装目录
291 311
     app_install_dir = os.path.dirname(os.path.abspath(__file__))  # 假设脚本文件位于应用的安装目录下
292 312
     export_folder = os.path.join(app_install_dir, 'Export')

+ 7
- 0
Back/GC/GCcompute.py Просмотреть файл

@@ -278,6 +278,12 @@ def main_function(file_path, db_path):
278 278
         file_name = os.path.basename(file_path)
279 279
         file_name_utf8 = file_name.encode('utf-8')
280 280
 
281
+        # 检查文件后缀名
282
+        if file_name.endswith('.xls'):
283
+            # 去掉 .xls 后缀并添加 .xlsx 后缀
284
+            file_name = file_name[:-4] + '.xlsx'
285
+            file_name_utf8 = file_name.encode('utf-8')
286
+
281 287
         # 处理查询结果
282 288
         abs_last_hdiffs = calculate_absolute_height_differences(db_path, file_name_utf8, "Last_HDiff")
283 289
         abs_new_hdiffs = calculate_absolute_height_differences(db_path, file_name_utf8, "New_HDiff")
@@ -359,3 +365,4 @@ def main_function(file_path, db_path):
359 365
 
360 366
     except Exception as e:
361 367
         QMessageBox.critical(None, "错误", f"文件 '{file_name}' 计算失败!错误信息: {str(e)}")
368
+        print(f"文件 '{file_name}' 计算失败!错误信息: {str(e)}")

+ 111
- 45
Back/GC/GCshow.py Просмотреть файл

@@ -5,11 +5,29 @@ from PySide6.QtGui import QStandardItemModel, QStandardItem
5 5
 from PySide6.QtCore import QItemSelectionModel
6 6
 
7 7
 
8
-def main_function(ui, db_path, utf_en):
8
+def process_utf_en(utf_en):
9 9
     # 确保 utf_en 是字节字符串
10 10
     if not isinstance(utf_en, bytes):
11 11
         utf_en = utf_en.encode('utf-8')
12 12
 
13
+    # 将字节字符串解码为普通字符串
14
+    file_name = utf_en.decode('utf-8')
15
+
16
+    # 检查文件后缀名并进行相应处理
17
+    if file_name.endswith('.xls'):
18
+        # 去掉 .xls 后缀并添加 .xlsx 后缀
19
+        file_name = file_name[:-4] + '.xlsx'
20
+
21
+    # 将处理后的字符串重新编码为字节字符串
22
+    utf_en = file_name.encode('utf-8')
23
+
24
+    return utf_en
25
+
26
+
27
+def main_function(ui, db_path, utf_en):
28
+    # 处理 utf_en
29
+    utf_en = process_utf_en(utf_en)
30
+
13 31
     # 设置 QTabWidget 的可见性和标签文本
14 32
     tabs_to_show = [(0, '水准测段高差计算成果表')]
15 33
     for index in range(ui.tabWidget.count()):
@@ -23,46 +41,67 @@ def main_function(ui, db_path, utf_en):
23 41
     db1.text_factory = lambda x: str(x, 'utf-8')
24 42
     cursor1 = db1.cursor()
25 43
 
26
-    # 查询 GC_Output_Point 表中的 TableName 字段
27
-    query_table_names = "SELECT DISTINCT TableName FROM GC_Output_Point"
28
-    cursor1.execute(query_table_names)
29
-
44
+    # 查询 GC_Output_Point 表中的数据
30 45
     query = """
31 46
     SELECT New_ID, New_ResultName, New_SPName, New_EPName, New_HDiff, New_RLen, Correct_Factor, Period_Diff, Dis_Ass
32 47
     FROM GC_Output_Point
33 48
     WHERE TableName = ?
34 49
     """
35 50
     cursor1.execute(query, (utf_en,))
36
-    result = cursor1.fetchall()
51
+    output_result = cursor1.fetchall()
52
+
53
+    # 查询 GC_Input_Param 表中的 Correct_Factor 字段
54
+    param_query_correct_factor = """
55
+    SELECT Correct_Factor
56
+    FROM GC_Input_Param
57
+    WHERE TableName = ?
58
+    """
59
+    cursor1.execute(param_query_correct_factor, (utf_en,))
60
+    correct_factor_result = cursor1.fetchall()
61
+
37 62
     cursor1.close()
38 63
     db1.close()
39 64
 
40 65
     # 创建 QStandardItemModel 实例
41 66
     model = QStandardItemModel()
42
-    model.setColumnCount(len(result[0]))
67
+    model.setColumnCount(len(output_result[0]) + 1)  # 增加一列
43 68
 
44 69
     # 设置表头
45
-    headers = ['序号', '结果期数', '起点', '终点', '高差', '路线长', '修正数', '期间差异', '变形判定']
70
+    headers = ['序号', '结果期数', '起点', '终点', '高差', '路线长', '修正数', '期间差异', '变形判定', '修正系数']
46 71
     model.setHorizontalHeaderLabels(headers)
47 72
 
48
-    for row_data in result:
73
+    # 使用 itertools.zip_longest 处理长度不一致的情况
74
+    for row_data, correct_factor_data in itertools.zip_longest(output_result, correct_factor_result,
75
+                                                               fillvalue=(None, None)):
49 76
         items = []
50 77
         for i, item in enumerate(row_data):
51
-            if i == 4 or i == 5:  # 索引4(高差)
52
-                item = f"{item:.6f}"  # 格式化为6位小数
53
-            elif i == 6:  # 索引 6(修正数)
54
-                item = f"{item:.2f}"
55
-            elif i == 7:  # 假设 Period_Diff 在索引 7
56
-                if item is not None and isinstance(item, (int, float)):
57
-                    item = f"{item:.2f}"  # 格式化为2位小数
58
-                else:
59
-                    item = ""  # 如果是 None 或非数字类型,显示空字符串
60
-            elif i == 8:  # 假设 Dis_Ass 在索引 8
61
-                if item is not None and isinstance(item, str):
62
-                    item = item  # 显示字符串内容
63
-                else:
64
-                    item = ""  # 如果是 None,显示空字符串
78
+            if item is None:
79
+                item = ""  # 如果是 None,显示空字符串
80
+            else:
81
+                if i == 4 or i == 5:  # 索引4(高差)和索引5(路线长)
82
+                    item = f"{item:.6f}"  # 格式化为6位小数
83
+                elif i == 6:  # 索引 6(修正数)
84
+                    item = f"{item:.2f}"
85
+                elif i == 7:  # 假设 Period_Diff 在索引 7
86
+                    if item is not None and isinstance(item, (int, float)):
87
+                        item = f"{item:.2f}"  # 格式化为2位小数
88
+                    else:
89
+                        item = ""  # 如果是 None 或非数字类型,显示空字符串
90
+                elif i == 8:  # 假设 Dis_Ass 在索引 8
91
+                    if item is not None and isinstance(item, str):
92
+                        item = item  # 显示字符串内容
93
+                    else:
94
+                        item = ""  # 如果是 None,显示空字符串
65 95
             items.append(QStandardItem(str(item)))
96
+
97
+        # 添加修正系数列数据
98
+        if correct_factor_data is not None and correct_factor_data[0] is not None:
99
+            correct_factor = f"{correct_factor_data[0]:.10f}"  # 格式化为10位小数
100
+        else:
101
+            correct_factor = ""
102
+
103
+        items.append(QStandardItem(str(correct_factor)))
104
+
66 105
         model.appendRow(items)
67 106
 
68 107
     # 设置并展示表格
@@ -101,7 +140,7 @@ def search_show_function(ui, db_path, utf_en):
101 140
 
102 141
     # 查询 GC_Input_Param 表中的数据
103 142
     param_query = """
104
-    SELECT ObservationLevel, Ms_Station, Last_StationCount, Last_SumHDiff, Last_SumRLen, New_StationCount, New_SumHDiff, New_SumRLen
143
+    SELECT ObservationLevel, Ms_Station, Last_StationCount, Last_SumHDiff, Last_SumRLen, New_StationCount, New_SumHDiff, New_SumRLen, Correct_Factor
105 144
     FROM GC_Input_Param
106 145
     WHERE TableName = ?
107 146
     """
@@ -122,7 +161,8 @@ def search_show_function(ui, db_path, utf_en):
122 161
     input_model.setHorizontalHeaderLabels(input_headers)
123 162
 
124 163
     # 使用 itertools.zip_longest 处理长度不一致的情况
125
-    for row_data, param_data in itertools.zip_longest(input_result, param_result, fillvalue=(None, None, None, None, None, None, None, None)):
164
+    for row_data, param_data in itertools.zip_longest(input_result, param_result,
165
+                                                      fillvalue=(None, None, None, None, None, None, None, None, None)):
126 166
         items = []
127 167
         for i, item in enumerate(row_data):
128 168
             if item is None:
@@ -134,9 +174,9 @@ def search_show_function(ui, db_path, utf_en):
134 174
 
135 175
         # 添加新的列数据
136 176
         if param_data is not None:
137
-            observation_level, ms_station, last_station_count, last_sum_hdiff, last_sum_rlen, new_station_count, new_sum_hdiff, new_sum_rlen = param_data
177
+            observation_level, ms_station, last_station_count, last_sum_hdiff, last_sum_rlen, new_station_count, new_sum_hdiff, new_sum_rlen, correct_factor = param_data
138 178
         else:
139
-            observation_level, ms_station, last_station_count, last_sum_hdiff, last_sum_rlen, new_station_count, new_sum_hdiff, new_sum_rlen = "", "", "", "", "", "", "", ""
179
+            observation_level, ms_station, last_station_count, last_sum_hdiff, last_sum_rlen, new_station_count, new_sum_hdiff, new_sum_rlen, correct_factor = "", "", "", "", "", "", "", "", ""
140 180
 
141 181
         # 确保 observation_level 和 ms_station 是字符串
142 182
         if observation_level is None:
@@ -165,6 +205,8 @@ def search_show_function(ui, db_path, utf_en):
165 205
             new_sum_rlen = ""
166 206
         else:
167 207
             new_sum_rlen = f"{new_sum_rlen:.6f}"
208
+        if correct_factor is None:
209
+            correct_factor = ""
168 210
 
169 211
         items.append(QStandardItem(str(observation_level)))
170 212
         items.append(QStandardItem(f"{ms_station:.2f}" if isinstance(ms_station, (int, float)) else ""))
@@ -193,32 +235,57 @@ def search_show_function(ui, db_path, utf_en):
193 235
     cursor1.execute(output_query, (utf_en,))
194 236
     output_result = cursor1.fetchall()
195 237
 
238
+    # 查询 GC_Input_Param 表中的 Correct_Factor 字段
239
+    param_query_correct_factor = """
240
+    SELECT Correct_Factor
241
+    FROM GC_Input_Param
242
+    WHERE TableName = ?
243
+    """
244
+    cursor1.execute(param_query_correct_factor, (utf_en,))
245
+    correct_factor_result = cursor1.fetchall()
246
+
196 247
     # 创建 QStandardItemModel 实例
197 248
     output_model = QStandardItemModel()
198
-    output_model.setColumnCount(len(output_result[0]))
249
+    output_model.setColumnCount(len(output_result[0]) + 1)  # 增加一列
199 250
 
200 251
     # 设置表头
201
-    output_headers = ['序号', '结果期数', '起点', '终点', '高差', '路线长', '修正数', '期间差异', '变形判定']
252
+    output_headers = [
253
+        '序号', '结果期数', '起点', '终点', '高差', '路线长', '修正数', '期间差异', '变形判定', '修正系数'
254
+    ]
202 255
     output_model.setHorizontalHeaderLabels(output_headers)
203 256
 
204
-    for row_data in output_result:
257
+    # 使用 itertools.zip_longest 处理长度不一致的情况
258
+    for row_data, correct_factor_data in itertools.zip_longest(output_result, correct_factor_result,
259
+                                                               fillvalue=(None, None)):
205 260
         items = []
206 261
         for i, item in enumerate(row_data):
207
-            if i == 4 or i == 5:  # 索引4(高差)和索引5(路线长)
208
-                item = f"{item:.6f}"  # 格式化为6位小数
209
-            elif i == 6:  # 索引 6(修正数)
210
-                item = f"{item:.2f}"
211
-            elif i == 7:  # 假设 Period_Diff 在索引 7
212
-                if item is not None and isinstance(item, (int, float)):
213
-                    item = f"{item:.2f}"  # 格式化为2位小数
214
-                else:
215
-                    item = ""  # 如果是 None 或非数字类型,显示空字符串
216
-            elif i == 8:  # 假设 Dis_Ass 在索引 8
217
-                if item is not None and isinstance(item, str):
218
-                    item = item  # 显示字符串内容
219
-                else:
220
-                    item = ""  # 如果是 None,显示空字符串
262
+            if item is None:
263
+                item = ""  # 如果是 None,显示空字符串
264
+            else:
265
+                if i == 4 or i == 5:  # 索引4(高差)和索引5(路线长)
266
+                    item = f"{item:.6f}"  # 格式化为6位小数
267
+                elif i == 6:  # 索引 6(修正数)
268
+                    item = f"{item:.2f}"
269
+                elif i == 7:  # 假设 Period_Diff 在索引 7
270
+                    if item is not None and isinstance(item, (int, float)):
271
+                        item = f"{item:.2f}"  # 格式化为2位小数
272
+                    else:
273
+                        item = ""  # 如果是 None 或非数字类型,显示空字符串
274
+                elif i == 8:  # 假设 Dis_Ass 在索引 8
275
+                    if item is not None and isinstance(item, str):
276
+                        item = item  # 显示字符串内容
277
+                    else:
278
+                        item = ""  # 如果是 None,显示空字符串
221 279
             items.append(QStandardItem(str(item)))
280
+
281
+        # 添加修正系数列数据
282
+        if correct_factor_data is not None and correct_factor_data[0] is not None:
283
+            correct_factor = f"{correct_factor_data[0]:.10f}"  # 格式化为6位小数
284
+        else:
285
+            correct_factor = ""
286
+
287
+        items.append(QStandardItem(str(correct_factor)))
288
+
222 289
         output_model.appendRow(items)
223 290
 
224 291
     # 设置并展示输出数据表格
@@ -237,4 +304,3 @@ def search_show_function(ui, db_path, utf_en):
237 304
 
238 305
     # 隐藏 QLabel
239 306
     ui.default_remind1.hide()
240
-

Загрузка…
Отмена
Сохранить