工具箱相关
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DMljjs.py 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import os
  2. from posixpath import dirname
  3. import xlrd
  4. import xlwt
  5. import math
  6. import tkinter as tk
  7. import numpy as np
  8. from tkinter import filedialog as tkFileDialog
  9. from tkinter import messagebox
  10. def start():
  11. window = tk.Tk() # 创建窗口对象的背景色
  12. window.title('断面累距计算程序') # 设置窗口的标题
  13. window.geometry('400x200') # 设置窗口的大小
  14. title = tk.Label(window, text='断面累距计算程序',
  15. font=('微软雅黑', 12), width=40, height=3)
  16. title.place(x=0, y=0, anchor='nw')
  17. choosepath = tk.Label(window, text='断面表格文件夹:',
  18. font=('微软雅黑', 12), width=20, height=2)
  19. choosepath.place(x=0, y=50, anchor='nw')
  20. path_var1 = tk.StringVar() # 输入路径
  21. entry1 = tk.Entry(window, textvariable=path_var1)
  22. entry1.place(x=170, y=65, anchor='nw')
  23. def choose1():
  24. file_dir = tkFileDialog.askdirectory()
  25. path_var1.set(file_dir)
  26. def tuichu():
  27. window.destroy()
  28. def main():
  29. inpath = entry1.get()
  30. #遍历文件夹
  31. for parent,dirname,filenames in os.walk(inpath):
  32. for filename in filenames:
  33. #排除其他文件的干扰
  34. if '.xls' in filename:
  35. if '_100' in filename:
  36. pass
  37. else:
  38. #完整路径
  39. filepath = parent + '/' +filename
  40. #拆分出断面名字dmm
  41. dmm = filename.split('.',-1)[0]
  42. xls = xlrd.open_workbook(filepath)
  43. sheet1 = xls.sheets()[0]
  44. #存个list存放距离
  45. dislist = []
  46. rows = sheet1.nrows
  47. ii = 1
  48. fpx = 0
  49. fpy = 0
  50. while ii < rows:
  51. if sheet1.cell_value(ii,1) != '':
  52. if ii == 1:
  53. #起点
  54. fpx = sheet1.cell_value(ii,1)
  55. fpy = sheet1.cell_value(ii,2)
  56. list1 = []
  57. list1.append(fpx)
  58. list1.append(fpy)
  59. list1.append(sheet1.cell_value(ii,3))
  60. list1.append(sheet1.cell_value(ii,4))
  61. list1.append(0)
  62. dislist.append(list1)
  63. ii = ii + 1
  64. else:
  65. xx = sheet1.cell_value(ii,1)
  66. yy = sheet1.cell_value(ii,2)
  67. #距离公式
  68. num1 = xx - fpx
  69. num2 = yy - fpy
  70. num3 = num1 * num1
  71. num4 = num2 * num2
  72. num5 = num3 + num4
  73. dis1 = math.sqrt(num5)
  74. list1 = []
  75. list1.append(xx)
  76. list1.append(yy)
  77. list1.append(sheet1.cell_value(ii,3))
  78. list1.append(sheet1.cell_value(ii,4))
  79. list1.append(dis1)
  80. dislist.append(list1)
  81. ii = ii + 1
  82. else:
  83. break
  84. #写到xls上
  85. #重创一个地址,然后全部重命名
  86. filepath1 = parent + '/' + dmm + '_100.xls'
  87. workbook = xlwt.Workbook(filepath1)
  88. sheet = workbook.add_sheet("sheet1")
  89. #抬头
  90. sheet.write(0,0,'点号')
  91. sheet.write(0,1,'X')
  92. sheet.write(0,2,'Y')
  93. sheet.write(0,3,'Z')
  94. sheet.write(0,4,'备注')
  95. sheet.write(0,5,'累距')
  96. mm = 0
  97. while mm < len(dislist):
  98. sheet.write((mm + 1), 0, (mm + 1))
  99. sheet.write((mm + 1), 1, dislist[mm][0])
  100. sheet.write((mm + 1), 2, dislist[mm][1])
  101. sheet.write((mm + 1), 3, round(dislist[mm][2],3))
  102. sheet.write((mm + 1), 4, dislist[mm][3])
  103. sheet.write((mm + 1), 5, round(dislist[mm][4],3))
  104. mm = mm + 1
  105. #删除以前的
  106. os.remove(filepath)
  107. workbook.save(filepath1)
  108. print(filename + ' FINISH')
  109. #全部重命名
  110. for parent,dirname,filenames in os.walk(inpath):
  111. for filename in filenames:
  112. #排除其他文件的干扰
  113. if '.xls' in filename:
  114. ori = parent + '/' +filename
  115. newfile = filename.replace('_100','')
  116. new = parent + '/' + newfile
  117. os.rename(ori,new)
  118. print(filename + ' RENAME')
  119. messagebox.showinfo("消息","运行成功")
  120. window.destroy()
  121. tk.Button(window, text='选择', command=choose1).place(x=320, y=60, anchor='nw')
  122. tk.Button(window,text='确认',command=main).place(x=120,y=110,anchor='nw')
  123. tk.Button(window,text='取消',command=tuichu).place(x=220,y=110,anchor='nw')
  124. window.mainloop()
  125. if __name__ == '__main__':
  126. start()