123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- import openpyxl
- import xlrd
- import sqlite3
- import os
-
- from PySide6.QtWidgets import QMessageBox
- from openpyxl import Workbook
- import math
- import numpy as np
-
-
- def to_utf8(text):
- str1 = text.encode('utf-8')
- return str1
-
-
- # 弹窗提示用户是否更新数据
- def ask_for_update(file_name):
- response = QMessageBox.question(None, "提示", f"检测到数据库中存在相同文件 '{file_name}',是(Yes)否(No)更新数据?",
- QMessageBox.Yes | QMessageBox.No)
- return response == QMessageBox.Yes
-
-
- # 读取Excel文件并计算公式结果
- def load_and_calculate_excel(file_path, cell0):
- # 加载Excel文件并计算单元格的公式结果。
- workbook = openpyxl.load_workbook(file_path, data_only=True)
- sheet = workbook.active
- h1_value = sheet[cell0].value
- return h1_value
-
-
- # 数据库插入函数
- def insert_into_database(database, pastname, newname, excelname, listname1, listpastx1, listpasty1, listcgcs1,
- listnewx1, listnewy1, pjbc, fxzwc, zrbzwc, points, zbs, zfxs, sf, pjbcs, pjfxs):
- # 先把输入的录入数据库
- db = sqlite3.connect(database)
- # 获取游标
- cursor = db.cursor()
- # 字符类的都需要转换成字节存进去
- utf_pan = to_utf8(pastname)
- utf_nn = to_utf8(newname)
- utf_tn = to_utf8(excelname)
- try:
- # 查询是否已存在相同的记录
- cursor.execute(
- "SELECT * FROM GS_Input_Param WHERE TableName = ?",
- (utf_tn,)
- )
- existing_record = cursor.fetchone()
-
- if existing_record:
- # 弹窗询问用户是否更新数据
- if not ask_for_update(excelname):
- print("用户选择不更新数据")
- return
-
- # 更新现有记录GS_Input_Param
- cursor.execute(
- """
- UPDATE GS_Input_Param
- SET Last_ResultName=?,New_ResultName=?,Avg_SL=?,Ms_Dir=?,Ms_WSL=?,Pt_Count=?,SL_Count=?,Dir_Count=?,Scale_Value=?,Avg_MSL=?,Avg_Dir=?
- WHERE TableName = ?
- """,
- (utf_pan, utf_nn, pjbc, fxzwc, zrbzwc, points, zbs, zfxs, sf, pjbcs, pjfxs, utf_tn,)
- )
- # 更新现有记录GS_Input_Point
- # 删了已有的数据,重新插入
- cursor.execute(
- """
- DELETE FROM GS_Input_Point WHERE TableName = ?
- """,
- (utf_tn,)
- )
- rr = 0
- while rr < len(listname1):
- rr1 = listname1[rr]
- rr2 = listpastx1[rr]
- rr3 = listpasty1[rr]
- rr4 = listcgcs1[rr]
- rr5 = listnewx1[rr]
- rr6 = listnewy1[rr]
- utf_rr1 = to_utf8(rr1)
- cursor.execute(
- """
- INSERT INTO GS_Input_Point(TableName,Last_ResultName,New_ResultName,Last_X,Last_Y,New_X,New_Y,PointName,cgcs) VALUES (?,?,?,?,?,?,?,?,?)
- """,
- (utf_tn, utf_pan, utf_nn, rr2, rr3, rr5, rr6, utf_rr1, rr4,)
- )
- rr = rr + 1
- QMessageBox.information(None, "提示",
- f"文件 '{excelname}' 已成功更新到本地数据库中,点击'OK'以关闭此对话框。对话框关闭后,请点击“计算”以重新计算数据")
- else:
- # 插入新记录GS_Input_Param
- cursor.execute(
- """
- INSERT INTO GS_Input_Param (TableName,Last_ResultName,New_ResultName,Avg_SL,Ms_Dir,Ms_WSL,Pt_Count,SL_Count,Dir_Count,Scale_Value,Avg_MSL,Avg_Dir)
- VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
- """,
- (utf_tn, utf_pan, utf_nn, pjbc, fxzwc, zrbzwc, points, zbs, zfxs, sf, pjbcs, pjfxs,)
- )
- # 插入新记录GS_Input_Point
- rr = 0
- while rr < len(listname1):
- rr1 = listname1[rr]
- rr2 = listpastx1[rr]
- rr3 = listpasty1[rr]
- rr4 = listcgcs1[rr]
- rr5 = listnewx1[rr]
- rr6 = listnewy1[rr]
- utf_rr1 = to_utf8(rr1)
- cursor.execute(
- """
- INSERT INTO GS_Input_Point(TableName,Last_ResultName,New_ResultName,Last_X,Last_Y,New_X,New_Y,PointName,cgcs) VALUES (?,?,?,?,?,?,?,?,?)
- """,
- (utf_tn, utf_pan, utf_nn, rr2, rr3, rr5, rr6, utf_rr1, rr4,)
- )
- rr = rr + 1
- QMessageBox.information(None, "提示",
- f"文件 '{excelname}' 已成功添加到本地数据库中,点击'OK'以关闭此对话框。对话框关闭后,请点击“计算”以计算数据")
-
- db.commit()
- except sqlite3.Error as e:
- print(f"插入或更新文件名时发生错误: {e}")
- except Exception as e:
- print(f"处理文件时发生错误: {e}")
- finally:
- db.close()
-
-
- # 读取xls
- def xlrd_read(excelpath, dbpath):
- global gszbx
- global gszby
- listname1 = []
- listnewx1 = []
- listnewy1 = []
- listpastx1 = []
- listpasty1 = []
- listcgcs1 = []
-
- # excel表名就是表名
- excelname = os.path.basename(excelpath)
- # 读取excel
- xlr = xlrd.open_workbook(excelpath)
- tabler = xlr.sheets()[0]
- # 从第3行开始,3A为点号,3B为最新x,3C为最新y,3D为往期x
- # 3E为往期y
- # 先看点数
- rows = tabler.nrows
- row = 2
- points = rows - 2
- # 收集每一列的数据,方便后期计算
- listname = []
- listnewx = []
- listnewy = []
- listpastx = []
- listpasty = []
- listcgcs = []
- sumnewx = 0
- sumnewy = 0
- sumpastx = 0
- sumpasty = 0
- # 记录下最早的那几个开头
- # excelpath是输入表格,outpath是输出路径,pjbc是平均边长
- # fxzwc是方向中误差,zrbzwc是最弱边边长相对中误差,pjbcs是平均边数,pjfxs是平均方向数,sf是缩放值
- # 新增总边数zbs,总方向数zfxs
- # 先计算位移判断值
- pjbc = float(tabler.cell(0, 6).value)
- fxzwc = float(tabler.cell(1, 6).value)
- str1 = str(tabler.cell(2, 6).value)
- # 判断下是哪种情况,转化为数值
- if '.' in str1:
- # 直接float使用
- zrbzwc = float(str1)
- else:
- # 转化为分数
- # 10的6次方
- # ten_to_the_six = 10e6
- fzstr = float(str1.split('/', -1)[0])
- fmstr = float(str1.split('/', -1)[1])
- zrbzwc = float(fzstr / fmstr)
- zbs = float(tabler.cell(3, 6).value)
- zfxs = float(tabler.cell(4, 6).value)
- pjbcs = zbs / points
- pjfxs = zfxs / points
- sf = float(tabler.cell(5, 6).value)
- # 再拆细点
- wy1 = pjbc / 1000
- wy2 = wy1 * zrbzwc * 1e6
- wypd1 = wy2 * wy2 / pjbcs
- # 206.265是常数系数
- wy3 = pjbc * fxzwc / 206.265
- wypd2 = wy3 * wy3 / pjfxs
- wypd3 = math.sqrt(wypd1 + wypd2)
- wypd = round(wypd3 * 3, 4)
- wypd0 = wypd
- pastname = tabler.cell(0, 3).value
- newname = tabler.cell(0, 1).value
- # 如果第二次测量或者首次测量为空,则判断为新建
- while row < rows:
- pname = tabler.cell(row, 0).value
- pcgcs = 1
- try:
- pnewx = round(float(tabler.cell(row, 1).value), 4)
- except:
- pnewx = 0
- pcgcs = -2
- try:
- pnewy = round(float(tabler.cell(row, 2).value), 4)
- except:
- pnewy = 0
- pcgcs = -2
- try:
- ppastx = round(float(tabler.cell(row, 3).value), 4)
- except:
- ppastx = 0
- pcgcs = -2
- try:
- ppasty = round(float(tabler.cell(row, 4).value), 4)
- except:
- ppasty = 0
- pcgcs = -2
- sumpastx = sumpastx + ppastx
- sumnewy = sumnewy + pnewy
- sumnewx = sumnewx + pnewx
- sumpasty = sumpasty + ppasty
- listname1.append(pname)
- listnewx1.append(pnewx)
- listnewy1.append(pnewy)
- listpastx1.append(ppastx)
- listpasty1.append(ppasty)
- listcgcs1.append(pcgcs)
- if pcgcs == -2:
- points = points - 1
- row = row + 1
- # 插入数据库
- insert_into_database(dbpath, pastname, newname, excelname, listname1, listpastx1, listpasty1, listcgcs1, listnewx1,
- listnewy1, pjbc, fxzwc, zrbzwc, points, zbs, zfxs, sf, pjbcs, pjfxs)
- # 新建点不参与运算
- rr = 0
- while rr < len(listname1):
- rr1 = listname1[rr]
- rr2 = listpastx1[rr]
- rr3 = listpasty1[rr]
- rr4 = listcgcs1[rr]
- rr5 = listnewx1[rr]
- rr6 = listnewy1[rr]
- if rr4 == -2:
- rr = rr + 1
- else:
- listname.append(rr1)
- listpastx.append(rr2)
- listpasty.append(rr3)
- listcgcs.append(rr4)
- listnewx.append(rr5)
- listnewy.append(rr6)
- rr = rr + 1
-
-
- # 读取xlsx
- def openpyxl_read(excelpath, dbpath):
- global gszbx
- global gszby
- listname1 = []
- listnewx1 = []
- listnewy1 = []
- listpastx1 = []
- listpasty1 = []
- listcgcs1 = []
-
- # excel表名就是表名
- excelname = os.path.basename(excelpath)
- # 读取excel
- xlr = openpyxl.load_workbook(excelpath)
- tabler = xlr.worksheets[0]
- # 从第3行开始,3A为点号,3B为最新x,3C为最新y,3D为往期x
- # 3E为往期y
- # 先看点数
- rows = tabler.max_row
- row = 3
- points = rows - 2
- # 收集每一列的数据,方便后期计算
- listname = []
- listnewx = []
- listnewy = []
- listpastx = []
- listpasty = []
- listcgcs = []
- sumnewx = 0
- sumnewy = 0
- sumpastx = 0
- sumpasty = 0
- # 记录下最早的那几个开头
- # excelpath是输入表格,outpath是输出路径,pjbc是平均边长
- # fxzwc是方向中误差,zrbzwc是最弱边边长相对中误差,pjbcs是平均边数,pjfxs是平均方向数,sf是缩放值
- # 新增总边数zbs,总方向数zfxs
- # 先计算位移判断值
- pjbc = float(tabler['G1'].value)
- fxzwc = float(tabler['G2'].value)
- str1 = str(tabler['G3'].value)
- # 判断下是哪种情况,转化为数值
- if '.' in str1:
- # 直接float使用
- zrbzwc = float(str1)
- else:
- # 转化为分数
- # 10的6次方
- # ten_to_the_six = 10e6
- fzstr = float(str1.split('/', -1)[0])
- fmstr = float(str1.split('/', -1)[1])
- zrbzwc = float(fzstr / fmstr)
- try:
- zbs = float(tabler['G4'].value)
- except:
- zbs = load_and_calculate_excel(excelpath, 'G4')
- try:
- zfxs = float(tabler['G5'].value)
- except:
- zfxs = load_and_calculate_excel(excelpath, 'G5')
- pjbcs = zbs / points
- pjfxs = zfxs / points
- sf = float(tabler['G6'].value)
- # 再拆细点
- wy1 = pjbc / 1000
- wy2 = wy1 * zrbzwc * 1e6
- wypd1 = wy2 * wy2 / pjbcs
- # 206.265是常数系数
- wy3 = pjbc * fxzwc / 206.265
- wypd2 = wy3 * wy3 / pjfxs
- wypd3 = math.sqrt(wypd1 + wypd2)
- wypd = round(wypd3 * 3, 4)
- wypd0 = wypd
- pastname = tabler['D1'].value
- newname = tabler['B1'].value
- # 如果第二次测量或者首次测量为空,则判断为新建
- while row <= rows:
- pname = tabler.cell(row, 1).value
- pcgcs = 1
- try:
- pnewx = round(float(tabler.cell(row, 2).value), 4)
- except:
- pnewx = 0
- pcgcs = -2
- try:
- pnewy = round(float(tabler.cell(row, 3).value), 4)
- except:
- pnewy = 0
- pcgcs = -2
- try:
- ppastx = round(float(tabler.cell(row, 4).value), 4)
- except:
- ppastx = 0
- pcgcs = -2
- try:
- ppasty = round(float(tabler.cell(row, 5).value), 4)
- except:
- ppasty = 0
- pcgcs = -2
- sumpastx = sumpastx + ppastx
- sumnewy = sumnewy + pnewy
- sumnewx = sumnewx + pnewx
- sumpasty = sumpasty + ppasty
- listname1.append(pname)
- listnewx1.append(pnewx)
- listnewy1.append(pnewy)
- listpastx1.append(ppastx)
- listpasty1.append(ppasty)
- listcgcs1.append(pcgcs)
- if pcgcs == -2:
- points = points - 1
- row = row + 1
- # 插入数据库
- insert_into_database(dbpath, pastname, newname, excelname, listname1, listpastx1, listpasty1, listcgcs1, listnewx1,
- listnewy1, pjbc, fxzwc, zrbzwc, points, zbs, zfxs, sf, pjbcs, pjfxs)
- # 新建点不参与运算
- rr = 0
- while rr < len(listname1):
- rr1 = listname1[rr]
- rr2 = listpastx1[rr]
- rr3 = listpasty1[rr]
- rr4 = listcgcs1[rr]
- rr5 = listnewx1[rr]
- rr6 = listnewy1[rr]
- if rr4 == -2:
- rr = rr + 1
- else:
- listname.append(rr1)
- listpastx.append(rr2)
- listpasty.append(rr3)
- listcgcs.append(rr4)
- listnewx.append(rr5)
- listnewy.append(rr6)
- rr = rr + 1
-
-
- # 主函数 读取excel文件
- def main_function(file_path, dbpath):
- # 调用读取Excel文件的函数
- file_name = os.path.basename(file_path)
- # 两种加载方式,对于不同的文件
- if ".xlsx" in file_path:
- # 采用openpyxl
- openpyxl_read(file_path, dbpath)
- else:
- # 采用xlrd
- xlrd_read(file_path, dbpath)
|