123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- import os
- from posixpath import dirname
- import xlrd
- import math
- import tkinter as tk
- import numpy as np
- from tkinter import filedialog as tkFileDialog
- from tkinter import messagebox
- from xlrd.book import Name
- from openpyxl import Workbook
- from openpyxl.styles import Alignment
- from openpyxl.styles import Font
- from openpyxl.styles import Border, Side
- from datetime import datetime
-
- def start():
- window = tk.Tk() # 创建窗口对象的背景色
- window.title('断面填表程序') # 设置窗口的标题
- window.geometry('400x250') # 设置窗口的大小
- 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')
- resultpath = tk.Label(window, text='选择输出文件夹:',
- font=('微软雅黑', 12), width=20, height=2)
- resultpath.place(x=8, y=100, anchor='nw')
- path_var2 = tk.StringVar() # 输出路径
- entry2 = tk.Entry(window, textvariable=path_var2)
- entry2.place(x=170, y=115, anchor='nw')
-
- def choose1():
- file_dir = tkFileDialog.askdirectory()
- path_var1.set(file_dir)
-
- def choose2():
- file_dir2 = tkFileDialog.askdirectory()
- path_var2.set(file_dir2)
-
- def tuichu():
- window.destroy()
-
- def main():
- inpath = entry1.get()
- outpath = entry2.get()
- #遍历文件夹
- for parent,dirname,filenames in os.walk(inpath):
- for filename in filenames:
- #排除其他文件的干扰
- if '.xls' in filename:
- #完整路径
- filepath = parent + '/' +filename
- #记下断面名
- fname = str(filename)
- dmm = fname.replace('.xls','')
- #确定输出路径
- excelpath = outpath + '/' + dmm + '.xlsx'
- #判断下如果存在,则删除
- if os.path.exists(excelpath):
- os.remove(excelpath)
- #写一个方法来倒写(原本的表格,需写入的表格,断面名)
- excelwrite(filepath,excelpath,dmm)
- print(dmm + ' FINISH' )
-
- messagebox.showinfo("消息","运行成功")
- window.destroy()
-
- def excelwrite(filepath,excelpath,dmm):
- #先把抬头写好
- wb = Workbook() # 新建工作簿
- ws2 = wb['Sheet']
- wb.remove(ws2)
- sheet = wb.create_sheet("成果表") # 获取工作表
- #设置列宽(算了下大概有0.84的倍率换算)
- sheet.column_dimensions['A'].width = 5.87
- sheet.column_dimensions['B'].width = 11.82
- sheet.column_dimensions['C'].width = 11.82
- sheet.column_dimensions['D'].width = 14.93
- sheet.column_dimensions['E'].width = 5.87
- sheet.column_dimensions['F'].width = 11.82
- sheet.column_dimensions['G'].width = 11.82
- sheet.column_dimensions['H'].width = 18.8
- #设置行高
- sheet.row_dimensions[2].height = 20.1
- sheet.row_dimensions[3].height = 20.1
- sheet.row_dimensions[4].height = 20.1
- sheet.row_dimensions[5].height = 20.1
- #指定单元格居中显示
- alignment_center = Alignment(horizontal='center', vertical='center')# 指定区域单元格居中
- #右对齐
- alignment_right = Alignment(horizontal='right', vertical='center')
- #左对齐
- alignment_left = Alignment(horizontal='left', vertical='center')
- #自动换行
- # ws1.cell(2, 13).alignment = Alignment(wrap_text=True)
- #修改字体宋体,18,加粗
- sheet['A1'].font = Font(u'宋体',size = 18,bold=True)
- #修改字体,宋体,12
- font1 = Font(u'宋体',size = 12)
- #修改字体,宋体,12,下划线
- sheet['G5'].font = Font(u'宋体',size = 12,underline='single')
- #设置边框
- # 设置该单元格边框和颜色
- border1 = Border(
- left=Side(style='thin', color='00000000'),
- bottom=Side(style='thin', color='00000000'),
- right=Side(style='thin', color='00000000'),
- top=Side(style='thin', color='00000000'))
- #标题
- sheet.merge_cells(start_row=1, start_column=1, end_row=1, end_column=8)
- sheet.cell(row=1, column=1, value='黄河四川段综合治理可行性研究项目测绘横断面(CGCS2000)')
- sheet['A1'].alignment = alignment_center
- #第2行
- sheet.merge_cells(start_row=2, start_column=1, end_row=2, end_column=6)
- str1 = '断面名称:' + dmm + '号断面'
- sheet.cell(row=2, column=1, value=str1)
- sheet['A2'].font = font1
- sheet['A2'].alignment = alignment_left
- sheet.cell(row=2, column=7, value='日期:')
- sheet['G2'].font = font1
- sheet['G2'].alignment = alignment_right
- # 获取当前日期和时间
- now = datetime.now()
- yy = now.year
- mm = now.month
- dd = now.day
- str2 = str(yy) + '年' + str(mm) + '月' + str(dd) + '日'
- sheet['H2'] = str2
- sheet['H2'].alignment = alignment_center
- sheet['H2'].font = font1
- #第3行
- sheet.merge_cells(start_row=3, start_column=1, end_row=3, end_column=2)
- sheet.merge_cells(start_row=3, start_column=4, end_row=3, end_column=8)
- sheet.cell(row=3, column=1, value='断面端点编号:')
- sheet['A3'].alignment = alignment_left
- sheet['A3'].font = font1
- sheet['C3'].font = font1
- sheet['D3'].font = font1
- sheet.cell(row=3, column=4, value='断面基点坐标: X= Y= H=')
- sheet['D3'].alignment = alignment_center
- #第4行
- sheet.merge_cells(start_row=4, start_column=4, end_row=4, end_column=8)
- #第5行
- sheet.merge_cells(start_row=5, start_column=1, end_row=5, end_column=2)
- sheet.merge_cells(start_row=5, start_column=3, end_row=5, end_column=4)
- sheet.merge_cells(start_row=5, start_column=5, end_row=5, end_column=6)
- sheet.cell(row=5, column=1, value='断面方位角:')
- sheet['A5'].font = font1
- sheet['A5'].alignment = alignment_left
- sheet['C5'].font = Font(u'宋体',size = 12,underline='single')
- sheet.cell(row=5, column=5, value='测量比例尺:')
- sheet['E5'].alignment = alignment_right
- sheet['E5'].font = font1
- sheet.cell(row=5, column=7, value='1:100')
- sheet['G5'].alignment = alignment_center
-
- #然后边读边写
- #打开表格,从第二行开始读
- xlr = xlrd.open_workbook(filepath)
- table = xlr.sheets()[0]
- rows = table.nrows
- #为了排除最后的空行,先存list,再计算点数
- list1 = []
- ii = 1
- while ii < rows:
- if table.cell(ii, 4).value != '' and table.cell(ii, 0).value != '':
- list2 = []
- dh = table.cell(ii, 0).value
- lj = table.cell(ii, 5).value
- zz = table.cell(ii, 3).value
- bz = table.cell(ii, 4).value
- list2.append(dh)
- list2.append(lj)
- list2.append(zz)
- list2.append(bz)
- list1.append(list2)
- ii = ii + 1
- else:
- ii = ii + 1
- counts = len(list1)
- #再看有多少组
- num2 = counts // 27
- num1 = counts % 27
- if num1 != 0:
- num2 = num2 + 1
- #按页数展完
- num3 = counts // 54
- num4 = counts % 54
- if num4 != 0:
- num3 = num3 + 1
- #按页数把格式全附上
- mm1 = 6
- while mm1 < num3 * 28 + 6:
- #设置行高
- sheet.row_dimensions[mm1].height = 20.1
- sheet.cell(row=mm1, column=1).alignment = alignment_center
- sheet.cell(row=mm1, column=2).alignment = alignment_center
- sheet.cell(row=mm1, column=3).alignment = alignment_center
- sheet.cell(row=mm1, column=4).alignment = alignment_center
- sheet.cell(row=mm1, column=5).alignment = alignment_center
- sheet.cell(row=mm1, column=6).alignment = alignment_center
- sheet.cell(row=mm1, column=7).alignment = alignment_center
- sheet.cell(row=mm1, column=8).alignment = alignment_center
- sheet.cell(row=mm1, column=1).font = font1
- sheet.cell(row=mm1, column=2).font = font1
- sheet.cell(row=mm1, column=3).font = font1
- sheet.cell(row=mm1, column=4).font = font1
- sheet.cell(row=mm1, column=5).font = font1
- sheet.cell(row=mm1, column=6).font = font1
- sheet.cell(row=mm1, column=7).font = font1
- sheet.cell(row=mm1, column=8).font = font1
- #加边线框
- sheet.cell(row=mm1, column=1).border = border1
- sheet.cell(row=mm1, column=2).border = border1
- sheet.cell(row=mm1, column=3).border = border1
- sheet.cell(row=mm1, column=4).border = border1
- sheet.cell(row=mm1, column=5).border = border1
- sheet.cell(row=mm1, column=6).border = border1
- sheet.cell(row=mm1, column=7).border = border1
- sheet.cell(row=mm1, column=8).border = border1
- mm1 = mm1 + 1
- mm = 0
- #每28行一增
- row1 = 0
- while mm < num2:
- #分单双数
- if mm == 0 or mm % 2 == 0:
- #双数都在左边
- #先写抬头
- sheet.cell(row=(row1 * 28) + 6, column=1, value='点号')
- sheet.cell(row=(row1 * 28) + 6, column=2, value='累距(m)')
- sheet.cell(row=(row1 * 28) + 6, column=3, value='高程(m)')
- sheet.cell(row=(row1 * 28) + 6, column=4, value='备注')
- #能写入点的范围
- start1 = mm * 27
- end1 = start1 + 27
- nn1 = start1
- oo1 = 7
- while nn1 < end1:
- #只写有的
- if nn1 < len(list1):
- #写入
- sheet.cell(row=(row1 * 28) + oo1, column=1, value=int(list1[nn1][0]))
- sheet.cell(row=(row1 * 28) + oo1, column=2, value=format(float(list1[nn1][1]), '.3f'))
- sheet.cell(row=(row1 * 28) + oo1, column=3, value=format(float(list1[nn1][2]), '.3f'))
- sheet.cell(row=(row1 * 28) + oo1, column=4, value=list1[nn1][3])
- nn1 = nn1 + 1
- oo1 = oo1 + 1
- else:
- break
- mm = mm + 1
- else:
- #单数都在右边
- #先写抬头
- sheet.cell(row=(row1 * 28) + 6, column=5, value='点号')
- sheet.cell(row=(row1 * 28) + 6, column=6, value='累距(m)')
- sheet.cell(row=(row1 * 28) + 6, column=7, value='高程(m)')
- sheet.cell(row=(row1 * 28) + 6, column=8, value='备注')
- #能写入点的范围
- start1 = mm * 27
- end1 = start1 + 27
- nn1 = start1
- oo1 = 7
- while nn1 < end1:
- #只写有的
- if nn1 < len(list1):
- #写入
- sheet.cell(row=(row1 * 28) + oo1, column=5, value=int(list1[nn1][0]))
- sheet.cell(row=(row1 * 28) + oo1, column=6, value=format(float(list1[nn1][1]), '.3f'))
- sheet.cell(row=(row1 * 28) + oo1, column=7, value=format(float(list1[nn1][2]), '.3f'))
- sheet.cell(row=(row1 * 28) + oo1, column=8, value=list1[nn1][3])
- nn1 = nn1 + 1
- oo1 = oo1 + 1
- else:
- break
- mm = mm + 1
- row1 = row1 + 1
-
- wb.save(excelpath)
-
- tk.Button(window, text='选择', command=choose1).place(x=320, y=60, anchor='nw')
- tk.Button(window, text='选择', command=choose2).place(x=320, y=110, anchor='nw')
- tk.Button(window,text='确认',command=main).place(x=120,y=210,anchor='nw')
- tk.Button(window,text='取消',command=tuichu).place(x=220,y=210,anchor='nw')
- window.mainloop()
-
- if __name__ == '__main__':
- start()
|