import openpyxl import xlrd import sqlite3 import os from openpyxl import Workbook import tkinter as tk from tkinter import messagebox import math import numpy as np def to_utf8(text): str1 = text.encode('utf-8') return str1 # 弹窗提示用户是否更新数据 def ask_for_update(file_name): root = tk.Tk() root.withdraw() # 隐藏主窗口 response = messagebox.askyesno("提示", f"检测到数据库中存在相同文件名 '{file_name}',是否更新数据?") return response # 读取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,beforename,excelname,listname1,listpastx1,listpasty1,listcgcs1,pjbc,fxzwc,points,zbs,zfxs,sf,pjbcs,pjfxs,listbex,listbey,listnewx,listnewy): #先把输入的录入数据库 db = sqlite3.connect(database) #获取游标 cursor = db.cursor() #字符类的都需要转换成字节存进去 utf_pan = to_utf8(pastname) utf_nn = to_utf8(newname) utf_bn = to_utf8(beforename) utf_tn = to_utf8(excelname) try: # 查询是否已存在相同的记录 cursor.execute( "SELECT * FROM WD_Input_Param WHERE TableName = ?", (utf_tn,) ) existing_record = cursor.fetchone() if existing_record: # 弹窗询问用户是否更新数据 if not ask_for_update(excelname): print("用户选择不更新数据") return # 更新现有记录WD_Input_Param cursor.execute( """ UPDATE WD_Input_Param SET First_ResultName = ?,Last_ResultName=?,New_ResultName=?,Avg_SL=?,Ms_Dir=?,Pt_Count=?,SL_Count=?,Dir_Count=?,Scale_Value=?,Avg_MSL=?,Avg_Dir=? WHERE TableName = ? """, (utf_bn,utf_pan,utf_nn,pjbc,fxzwc,points,zbs,zfxs,sf,pjbcs,pjfxs,utf_tn,) ) # 更新现有记录WD_Input_Point #删了已有的数据,重新插入 cursor.execute( """ DELETE FROM WD_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 = listnewx[rr] rr6 = listnewy[rr] rr7 = listbex[rr] rr8 = listbey[rr] utf_rr1 = to_utf8(rr1) cursor.execute( """ INSERT INTO WD_Input_Point(TableName,First_ResultName,Last_ResultName,New_ResultName,Last_X,Last_Y,New_X,New_Y,PointName,Wight,First_X,First_Y) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) """, (utf_tn,utf_bn,utf_pan,utf_nn,rr2,rr3,rr5,rr6,utf_rr1,rr4,rr7,rr8,) ) rr = rr + 1 messagebox.showinfo("提示", f"文件 '{excelname}' 已成功更新到本地数据库中,点击确认以关闭此对话框。对话框关闭后,请点击“计算”以重新计算数据") else: # 插入新记录WD_Input_Param cursor.execute( """ INSERT INTO WD_Input_Param (TableName,First_ResultName,Last_ResultName,New_ResultName,Avg_SL,Ms_Dir,Pt_Count,SL_Count,Dir_Count,Scale_Value,Avg_MSL,Avg_Dir) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) """, (utf_tn,utf_bn,utf_pan,utf_nn,pjbc,fxzwc,points,zbs,zfxs,sf,pjbcs,pjfxs,) ) # 插入新记录WD_Input_Point rr = 0 while rr < len(listname1): rr1 = listname1[rr] rr2 = listpastx1[rr] rr3 = listpasty1[rr] rr4 = listcgcs1[rr] rr5 = listnewx[rr] rr6 = listnewy[rr] rr7 = listbex[rr] rr8 = listbey[rr] utf_rr1 = to_utf8(rr1) cursor.execute( """ INSERT INTO WD_Input_Point(TableName,First_ResultName,Last_ResultName,New_ResultName,Last_X,Last_Y,New_X,New_Y,PointName,Wight,First_X,First_Y) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) """, (utf_tn,utf_bn,utf_pan,utf_nn,rr2,rr3,rr5,rr6,utf_rr1,rr4,rr7,rr8,) ) rr = rr + 1 messagebox.showinfo("提示", f"文件 '{excelname}' 已成功添加到本地数据库中,点击确认以关闭此对话框。对话框关闭后,请点击“计算”以计算数据") 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 = [] 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,3F为成果次数,3G为最早x,3H最早y # 先看点数 rows = tabler.nrows row = 2 points = rows - 2 # 收集每一列的数据,方便后期计算 listname = [] listnewx = [] listnewy = [] listpastx = [] listpasty = [] # 成果次数 listcgcs = [] # 最早的 listbex = [] listbey = [] sumnewx = 0 sumnewy = 0 sumpastx = 0 sumpasty = 0 # 记录下最早的那几个开头 # excelpath是输入表格,outpath是输出路径,pjbc是平均边长 # fxzwc是方向中误差,pjfxs是平均方向数,sf是缩放值 # 先计算位移判断值 pjbc = float(tabler.cell(0, 9).value) fxzwc = float(tabler.cell(1, 9).value) zbs = float(tabler.cell(2, 9).value) zfxs = float(tabler.cell(3, 9).value) pjbcs = zbs / points pjfxs = zfxs / points sf = float(tabler.cell(4, 9).value) # 再拆细点 wy1 = pjbc / 1000 wy2 = wy1 + 1 wypd1 = wy2 * wy2 / pjbcs # 好像206.265是常数系数 wy3 = pjbc * fxzwc / 206.265 wypd2 = wy3 * wy3 / pjfxs wypd3 = math.sqrt(wypd1 + wypd2) wypd = round((wypd3 * 2 * math.sqrt(2)), 9) wypd0 = wypd lastname = tabler.cell(0, 3).value finalname = tabler.cell(0, 1).value befname = tabler.cell(0, 6).value utf_ln = to_utf8(lastname) utf_fn = to_utf8(finalname) utf_bn = to_utf8(befname) while row < rows: pname = tabler.cell(row, 0).value pnewx = float(tabler.cell(row, 1).value) sumnewx = sumnewx + pnewx pnewy = float(tabler.cell(row, 2).value) sumnewy = sumnewy + pnewy ppastx = float(tabler.cell(row, 3).value) sumpastx = sumpastx + ppastx ppasty = float(tabler.cell(row, 4).value) sumpasty = sumpasty + ppasty pcgcs = float(tabler.cell(row, 5).value) pbex = float(tabler.cell(row, 6).value) pbey = float(tabler.cell(row, 7).value) listname.append(pname) listnewx.append(pnewx) listnewy.append(pnewy) listpastx.append(ppastx) listpasty.append(ppasty) listcgcs.append(pcgcs) listbex.append(pbex) listbey.append(pbey) row = row + 1 # 最后返回这里输出 rr = 0 while rr < len(listname): rr1 = listname[rr] rr2 = listpastx[rr] rr3 = listpasty[rr] rr4 = listcgcs[rr] listname1.append(rr1) listpastx1.append(rr2) listpasty1.append(rr3) listcgcs1.append(rr4) rr = rr + 1 #存入数据库 insert_into_database(dbpath,lastname,finalname,befname,excelname,listname1,listpastx1,listpasty1,listcgcs1,pjbc,fxzwc,points,zbs,zfxs,sf,pjbcs,pjfxs,listbex,listbey,listnewx,listnewy) #读取xlsx def openpyxl_read(excelpath,dbpath): global gszbx global gszby listname1 = [] 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 = [] listbex=[] listbey=[] sumnewx = 0 sumnewy = 0 sumpastx = 0 sumpasty = 0 # 记录下最早的那几个开头 # excelpath是输入表格,outpath是输出路径,pjbc是平均边长 # fxzwc是方向中误差,zrbzwc是最弱边边长相对中误差,pjbcs是平均边数,pjfxs是平均方向数,sf是缩放值 # 新增总边数zbs,总方向数zfxs # 先计算位移判断值 pjbc = float(tabler['J1'].value) fxzwc = float(tabler['J2'].value) try: zbs = float(tabler['J3'].value) except: zbs = load_and_calculate_excel(excelpath,'J3') try: zfxs = float(tabler['J4'].value) except: zfxs = load_and_calculate_excel(excelpath,'J4') pjbcs = zbs / points pjfxs = zfxs / points sf = float(tabler['J5'].value) # 再拆细点 wy1 = pjbc / 1000 wy2 = wy1 + 1 wypd1 = wy2 * wy2 / pjbcs # 好像206.265是常数系数 wy3 = pjbc * fxzwc / 206.265 wypd2 = wy3 * wy3 / pjfxs wypd3 = math.sqrt(wypd1 + wypd2) wypd = round((wypd3 * 2 * math.sqrt(2)), 9) wypd0 = wypd befname=tabler['G1'].value lastname = tabler['D1'].value finalname = tabler['B1'].value while row <= rows: try: pname = tabler.cell(row, 1).value pnewx = float(tabler.cell(row, 2).value) sumnewx = sumnewx + pnewx pnewy = float(tabler.cell(row, 3).value) sumnewy = sumnewy + pnewy ppastx = float(tabler.cell(row, 4).value) sumpastx = sumpastx + ppastx ppasty = float(tabler.cell(row, 5).value) sumpasty = sumpasty + ppasty pcgcs = float(tabler.cell(row, 6).value) pbex = float(tabler.cell(row, 7).value) pbey = float(tabler.cell(row, 8).value) listname.append(pname) listnewx.append(pnewx) listnewy.append(pnewy) listpastx.append(ppastx) listpasty.append(ppasty) listcgcs.append(pcgcs) listbex.append(pbex) listbey.append(pbey) row = row + 1 except: break # 最后返回这里输出 rr = 0 while rr < len(listname): rr1 = listname[rr] rr2 = listpastx[rr] rr3 = listpasty[rr] rr4 = listcgcs[rr] listname1.append(rr1) listpastx1.append(rr2) listpasty1.append(rr3) listcgcs1.append(rr4) rr = rr + 1 #存入数据库 insert_into_database(dbpath,lastname,finalname,befname,excelname,listname1,listpastx1,listpasty1,listcgcs1,pjbc,fxzwc,points,zbs,zfxs,sf,pjbcs,pjfxs,listbex,listbey,listnewx,listnewy) # 主函数 读取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)