工具箱相关
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import os
  2. from posixpath import dirname
  3. import xlrd
  4. import math
  5. import tkinter as tk
  6. import numpy as np
  7. from tkinter import filedialog as tkFileDialog
  8. from tkinter import messagebox
  9. from xlrd.book import Name
  10. from openpyxl import Workbook
  11. from openpyxl.styles import Alignment
  12. from openpyxl.styles import Font
  13. from openpyxl.styles import Border, Side
  14. from datetime import datetime
  15. def start():
  16. window = tk.Tk() # 创建窗口对象的背景色
  17. window.title('断面填表程序') # 设置窗口的标题
  18. window.geometry('400x250') # 设置窗口的大小
  19. title = tk.Label(window, text='断面填表程序',
  20. font=('微软雅黑', 12), width=40, height=3)
  21. title.place(x=0, y=0, anchor='nw')
  22. choosepath = tk.Label(window, text='选择断面表格文件夹:',
  23. font=('微软雅黑', 12), width=20, height=2)
  24. choosepath.place(x=0, y=50, anchor='nw')
  25. path_var1 = tk.StringVar() # 输入路径
  26. entry1 = tk.Entry(window, textvariable=path_var1)
  27. entry1.place(x=170, y=65, anchor='nw')
  28. resultpath = tk.Label(window, text='选择输出文件夹:',
  29. font=('微软雅黑', 12), width=20, height=2)
  30. resultpath.place(x=8, y=100, anchor='nw')
  31. path_var2 = tk.StringVar() # 输出路径
  32. entry2 = tk.Entry(window, textvariable=path_var2)
  33. entry2.place(x=170, y=115, anchor='nw')
  34. def choose1():
  35. file_dir = tkFileDialog.askdirectory()
  36. path_var1.set(file_dir)
  37. def choose2():
  38. file_dir2 = tkFileDialog.askdirectory()
  39. path_var2.set(file_dir2)
  40. def tuichu():
  41. window.destroy()
  42. def main():
  43. inpath = entry1.get()
  44. outpath = entry2.get()
  45. #遍历文件夹
  46. for parent,dirname,filenames in os.walk(inpath):
  47. for filename in filenames:
  48. #排除其他文件的干扰
  49. if '.xls' in filename:
  50. #完整路径
  51. filepath = parent + '/' +filename
  52. #记下断面名
  53. fname = str(filename)
  54. dmm = fname.replace('.xls','')
  55. #确定输出路径
  56. excelpath = outpath + '/' + dmm + '.xlsx'
  57. #判断下如果存在,则删除
  58. if os.path.exists(excelpath):
  59. os.remove(excelpath)
  60. #写一个方法来倒写(原本的表格,需写入的表格,断面名)
  61. excelwrite(filepath,excelpath,dmm)
  62. print(dmm + ' FINISH' )
  63. messagebox.showinfo("消息","运行成功")
  64. window.destroy()
  65. def excelwrite(filepath,excelpath,dmm):
  66. #先把抬头写好
  67. wb = Workbook() # 新建工作簿
  68. ws2 = wb['Sheet']
  69. wb.remove(ws2)
  70. sheet = wb.create_sheet("成果表") # 获取工作表
  71. #设置列宽(算了下大概有0.84的倍率换算)
  72. sheet.column_dimensions['A'].width = 5.87
  73. sheet.column_dimensions['B'].width = 11.82
  74. sheet.column_dimensions['C'].width = 11.82
  75. sheet.column_dimensions['D'].width = 14.93
  76. sheet.column_dimensions['E'].width = 5.87
  77. sheet.column_dimensions['F'].width = 11.82
  78. sheet.column_dimensions['G'].width = 11.82
  79. sheet.column_dimensions['H'].width = 18.8
  80. #设置行高
  81. sheet.row_dimensions[2].height = 20.1
  82. sheet.row_dimensions[3].height = 20.1
  83. sheet.row_dimensions[4].height = 20.1
  84. sheet.row_dimensions[5].height = 20.1
  85. #指定单元格居中显示
  86. alignment_center = Alignment(horizontal='center', vertical='center')# 指定区域单元格居中
  87. #右对齐
  88. alignment_right = Alignment(horizontal='right', vertical='center')
  89. #左对齐
  90. alignment_left = Alignment(horizontal='left', vertical='center')
  91. #自动换行
  92. # ws1.cell(2, 13).alignment = Alignment(wrap_text=True)
  93. #修改字体宋体,18,加粗
  94. sheet['A1'].font = Font(u'宋体',size = 18,bold=True)
  95. #修改字体,宋体,12
  96. font1 = Font(u'宋体',size = 12)
  97. #修改字体,宋体,12,下划线
  98. sheet['G5'].font = Font(u'宋体',size = 12,underline='single')
  99. #设置边框
  100. # 设置该单元格边框和颜色
  101. border1 = Border(
  102. left=Side(style='thin', color='00000000'),
  103. bottom=Side(style='thin', color='00000000'),
  104. right=Side(style='thin', color='00000000'),
  105. top=Side(style='thin', color='00000000'))
  106. #标题
  107. sheet.merge_cells(start_row=1, start_column=1, end_row=1, end_column=8)
  108. sheet.cell(row=1, column=1, value='黄河四川段综合治理可行性研究项目测绘横断面(CGCS2000)')
  109. sheet['A1'].alignment = alignment_center
  110. #第2行
  111. sheet.merge_cells(start_row=2, start_column=1, end_row=2, end_column=6)
  112. str1 = '断面名称:' + dmm + '号断面'
  113. sheet.cell(row=2, column=1, value=str1)
  114. sheet['A2'].font = font1
  115. sheet['A2'].alignment = alignment_left
  116. sheet.cell(row=2, column=7, value='日期:')
  117. sheet['G2'].font = font1
  118. sheet['G2'].alignment = alignment_right
  119. # 获取当前日期和时间
  120. now = datetime.now()
  121. yy = now.year
  122. mm = now.month
  123. dd = now.day
  124. str2 = str(yy) + '年' + str(mm) + '月' + str(dd) + '日'
  125. sheet['H2'] = str2
  126. sheet['H2'].alignment = alignment_center
  127. sheet['H2'].font = font1
  128. #第3行
  129. sheet.merge_cells(start_row=3, start_column=1, end_row=3, end_column=2)
  130. sheet.merge_cells(start_row=3, start_column=4, end_row=3, end_column=8)
  131. sheet.cell(row=3, column=1, value='断面端点编号:')
  132. sheet['A3'].alignment = alignment_left
  133. sheet['A3'].font = font1
  134. sheet['C3'].font = font1
  135. sheet['D3'].font = font1
  136. sheet.cell(row=3, column=4, value='断面基点坐标: X= Y= H=')
  137. sheet['D3'].alignment = alignment_center
  138. #第4行
  139. sheet.merge_cells(start_row=4, start_column=4, end_row=4, end_column=8)
  140. #第5行
  141. sheet.merge_cells(start_row=5, start_column=1, end_row=5, end_column=2)
  142. sheet.merge_cells(start_row=5, start_column=3, end_row=5, end_column=4)
  143. sheet.merge_cells(start_row=5, start_column=5, end_row=5, end_column=6)
  144. sheet.cell(row=5, column=1, value='断面方位角:')
  145. sheet['A5'].font = font1
  146. sheet['A5'].alignment = alignment_left
  147. sheet['C5'].font = Font(u'宋体',size = 12,underline='single')
  148. sheet.cell(row=5, column=5, value='测量比例尺:')
  149. sheet['E5'].alignment = alignment_right
  150. sheet['E5'].font = font1
  151. sheet.cell(row=5, column=7, value='1:100')
  152. sheet['G5'].alignment = alignment_center
  153. #然后边读边写
  154. #打开表格,从第二行开始读
  155. xlr = xlrd.open_workbook(filepath)
  156. table = xlr.sheets()[0]
  157. rows = table.nrows
  158. #为了排除最后的空行,先存list,再计算点数
  159. list1 = []
  160. ii = 1
  161. while ii < rows:
  162. if table.cell(ii, 4).value != '' and table.cell(ii, 0).value != '':
  163. list2 = []
  164. dh = table.cell(ii, 0).value
  165. lj = table.cell(ii, 5).value
  166. zz = table.cell(ii, 3).value
  167. bz = table.cell(ii, 4).value
  168. list2.append(dh)
  169. list2.append(lj)
  170. list2.append(zz)
  171. list2.append(bz)
  172. list1.append(list2)
  173. ii = ii + 1
  174. else:
  175. ii = ii + 1
  176. counts = len(list1)
  177. #再看有多少组
  178. num2 = counts // 27
  179. num1 = counts % 27
  180. if num1 != 0:
  181. num2 = num2 + 1
  182. #按页数展完
  183. num3 = counts // 54
  184. num4 = counts % 54
  185. if num4 != 0:
  186. num3 = num3 + 1
  187. #按页数把格式全附上
  188. mm1 = 6
  189. while mm1 < num3 * 28 + 6:
  190. #设置行高
  191. sheet.row_dimensions[mm1].height = 20.1
  192. sheet.cell(row=mm1, column=1).alignment = alignment_center
  193. sheet.cell(row=mm1, column=2).alignment = alignment_center
  194. sheet.cell(row=mm1, column=3).alignment = alignment_center
  195. sheet.cell(row=mm1, column=4).alignment = alignment_center
  196. sheet.cell(row=mm1, column=5).alignment = alignment_center
  197. sheet.cell(row=mm1, column=6).alignment = alignment_center
  198. sheet.cell(row=mm1, column=7).alignment = alignment_center
  199. sheet.cell(row=mm1, column=8).alignment = alignment_center
  200. sheet.cell(row=mm1, column=1).font = font1
  201. sheet.cell(row=mm1, column=2).font = font1
  202. sheet.cell(row=mm1, column=3).font = font1
  203. sheet.cell(row=mm1, column=4).font = font1
  204. sheet.cell(row=mm1, column=5).font = font1
  205. sheet.cell(row=mm1, column=6).font = font1
  206. sheet.cell(row=mm1, column=7).font = font1
  207. sheet.cell(row=mm1, column=8).font = font1
  208. #加边线框
  209. sheet.cell(row=mm1, column=1).border = border1
  210. sheet.cell(row=mm1, column=2).border = border1
  211. sheet.cell(row=mm1, column=3).border = border1
  212. sheet.cell(row=mm1, column=4).border = border1
  213. sheet.cell(row=mm1, column=5).border = border1
  214. sheet.cell(row=mm1, column=6).border = border1
  215. sheet.cell(row=mm1, column=7).border = border1
  216. sheet.cell(row=mm1, column=8).border = border1
  217. mm1 = mm1 + 1
  218. mm = 0
  219. #每28行一增
  220. row1 = 0
  221. while mm < num2:
  222. #分单双数
  223. if mm == 0 or mm % 2 == 0:
  224. #双数都在左边
  225. #先写抬头
  226. sheet.cell(row=(row1 * 28) + 6, column=1, value='点号')
  227. sheet.cell(row=(row1 * 28) + 6, column=2, value='累距(m)')
  228. sheet.cell(row=(row1 * 28) + 6, column=3, value='高程(m)')
  229. sheet.cell(row=(row1 * 28) + 6, column=4, value='备注')
  230. #能写入点的范围
  231. start1 = mm * 27
  232. end1 = start1 + 27
  233. nn1 = start1
  234. oo1 = 7
  235. while nn1 < end1:
  236. #只写有的
  237. if nn1 < len(list1):
  238. #写入
  239. sheet.cell(row=(row1 * 28) + oo1, column=1, value=int(list1[nn1][0]))
  240. sheet.cell(row=(row1 * 28) + oo1, column=2, value=format(float(list1[nn1][1]), '.3f'))
  241. sheet.cell(row=(row1 * 28) + oo1, column=3, value=format(float(list1[nn1][2]), '.3f'))
  242. sheet.cell(row=(row1 * 28) + oo1, column=4, value=list1[nn1][3])
  243. nn1 = nn1 + 1
  244. oo1 = oo1 + 1
  245. else:
  246. break
  247. mm = mm + 1
  248. else:
  249. #单数都在右边
  250. #先写抬头
  251. sheet.cell(row=(row1 * 28) + 6, column=5, value='点号')
  252. sheet.cell(row=(row1 * 28) + 6, column=6, value='累距(m)')
  253. sheet.cell(row=(row1 * 28) + 6, column=7, value='高程(m)')
  254. sheet.cell(row=(row1 * 28) + 6, column=8, value='备注')
  255. #能写入点的范围
  256. start1 = mm * 27
  257. end1 = start1 + 27
  258. nn1 = start1
  259. oo1 = 7
  260. while nn1 < end1:
  261. #只写有的
  262. if nn1 < len(list1):
  263. #写入
  264. sheet.cell(row=(row1 * 28) + oo1, column=5, value=int(list1[nn1][0]))
  265. sheet.cell(row=(row1 * 28) + oo1, column=6, value=format(float(list1[nn1][1]), '.3f'))
  266. sheet.cell(row=(row1 * 28) + oo1, column=7, value=format(float(list1[nn1][2]), '.3f'))
  267. sheet.cell(row=(row1 * 28) + oo1, column=8, value=list1[nn1][3])
  268. nn1 = nn1 + 1
  269. oo1 = oo1 + 1
  270. else:
  271. break
  272. mm = mm + 1
  273. row1 = row1 + 1
  274. wb.save(excelpath)
  275. tk.Button(window, text='选择', command=choose1).place(x=320, y=60, anchor='nw')
  276. tk.Button(window, text='选择', command=choose2).place(x=320, y=110, anchor='nw')
  277. tk.Button(window,text='确认',command=main).place(x=120,y=210,anchor='nw')
  278. tk.Button(window,text='取消',command=tuichu).place(x=220,y=210,anchor='nw')
  279. window.mainloop()
  280. if __name__ == '__main__':
  281. start()