import os from posixpath import dirname import xlrd import xlwt import math import tkinter as tk import numpy as np from tkinter import filedialog as tkFileDialog from tkinter import messagebox def start(): window = tk.Tk() # 创建窗口对象的背景色 window.title('断面累距计算程序') # 设置窗口的标题 window.geometry('400x200') # 设置窗口的大小 title = tk.Label(window, text='断面累距计算程序', font=('微软雅黑', 12), width=40, height=3) title.place(x=0, y=0, anchor='nw') choosepath = tk.Label(window, text='断面表格文件夹:', font=('微软雅黑', 12), width=20, height=2) choosepath.place(x=0, y=50, anchor='nw') path_var1 = tk.StringVar() # 输入路径 entry1 = tk.Entry(window, textvariable=path_var1) entry1.place(x=170, y=65, anchor='nw') def choose1(): file_dir = tkFileDialog.askdirectory() path_var1.set(file_dir) def tuichu(): window.destroy() def main(): inpath = entry1.get() #遍历文件夹 for parent,dirname,filenames in os.walk(inpath): for filename in filenames: #排除其他文件的干扰 if '.xls' in filename: if '_100' in filename: pass else: #完整路径 filepath = parent + '/' +filename #拆分出断面名字dmm dmm = filename.split('.',-1)[0] xls = xlrd.open_workbook(filepath) sheet1 = xls.sheets()[0] #存个list存放距离 dislist = [] rows = sheet1.nrows ii = 1 fpx = 0 fpy = 0 while ii < rows: if sheet1.cell_value(ii,1) != '': if ii == 1: #起点 fpx = sheet1.cell_value(ii,1) fpy = sheet1.cell_value(ii,2) list1 = [] list1.append(fpx) list1.append(fpy) list1.append(sheet1.cell_value(ii,3)) list1.append(sheet1.cell_value(ii,4)) list1.append(0) dislist.append(list1) ii = ii + 1 else: xx = sheet1.cell_value(ii,1) yy = sheet1.cell_value(ii,2) #距离公式 num1 = xx - fpx num2 = yy - fpy num3 = num1 * num1 num4 = num2 * num2 num5 = num3 + num4 dis1 = math.sqrt(num5) list1 = [] list1.append(xx) list1.append(yy) list1.append(sheet1.cell_value(ii,3)) list1.append(sheet1.cell_value(ii,4)) list1.append(dis1) dislist.append(list1) ii = ii + 1 else: break #写到xls上 #重创一个地址,然后全部重命名 filepath1 = parent + '/' + dmm + '_100.xls' workbook = xlwt.Workbook(filepath1) sheet = workbook.add_sheet("sheet1") #抬头 sheet.write(0,0,'点号') sheet.write(0,1,'X') sheet.write(0,2,'Y') sheet.write(0,3,'Z') sheet.write(0,4,'备注') sheet.write(0,5,'累距') mm = 0 while mm < len(dislist): sheet.write((mm + 1), 0, (mm + 1)) sheet.write((mm + 1), 1, dislist[mm][0]) sheet.write((mm + 1), 2, dislist[mm][1]) sheet.write((mm + 1), 3, round(dislist[mm][2],3)) sheet.write((mm + 1), 4, dislist[mm][3]) sheet.write((mm + 1), 5, round(dislist[mm][4],3)) mm = mm + 1 #删除以前的 os.remove(filepath) workbook.save(filepath1) print(filename + ' FINISH') #全部重命名 for parent,dirname,filenames in os.walk(inpath): for filename in filenames: #排除其他文件的干扰 if '.xls' in filename: ori = parent + '/' +filename newfile = filename.replace('_100','') new = parent + '/' + newfile os.rename(ori,new) print(filename + ' RENAME') messagebox.showinfo("消息","运行成功") window.destroy() tk.Button(window, text='选择', command=choose1).place(x=320, y=60, anchor='nw') tk.Button(window,text='确认',command=main).place(x=120,y=110,anchor='nw') tk.Button(window,text='取消',command=tuichu).place(x=220,y=110,anchor='nw') window.mainloop() if __name__ == '__main__': start()