博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python处理打卡数据的excel表格
阅读量:4097 次
发布时间:2019-05-25

本文共 6115 字,大约阅读时间需要 20 分钟。

from tkinter import *from tkinter import filedialogfrom openpyxl import Workbook  from openpyxl import load_workbookfrom openpyxl.styles import  PatternFillstartWork = '9:00'endWork = '18:00'ColorRed = PatternFill("solid", fgColor="FF4500")ColorGreen = PatternFill("solid", fgColor="ADFF2F")def read1():    filename= filedialog.askopenfilename()    LineTxt.delete(0, END)    LineTxt.insert(0,filename)def CompareStartTime(times,peopleName):    if times == None:        return False    else:        timeList = times.split(':')        if not timeList[0].isnumeric():            AreaTxt.insert(INSERT,"时间格式不对 !\n")             return False        if int(timeList[0]) >= 9:            AreaTxt.insert(INSERT,peopleName+':StartTime > 9:00,  '+times+'\n')            return False        else :            return Truedef CompareEndTime(times,peopleName):    if times == None:        return False    else:        timeList = times.split(':')        if not timeList[0].isnumeric():            AreaTxt.insert(INSERT,"时间格式不对 !\n")             return False        if int(timeList[0]) < 18:            AreaTxt.insert(INSERT,peopleName+':EndTime < 18:00,  '+times+'\n')            return False        else :            return Truedef praseCell(context,peopleName):    if context == None:        return False    else:        timeList = context.split()        start = CompareStartTime(timeList[0],peopleName)        end = CompareEndTime(timeList[-1],peopleName)        if start and end:            return True        else:            return Falsedef fun():    AreaTxt.delete(1.0,END)  # clear text    AreaTxt.insert(INSERT,"已选文件:"+LineTxt.get()+"\n",'g')       outWb = Workbook()   #输出wb      oWbsheet = outWb.active   #获取活动表      oWbsheet.title = "sheet1"    #命名表        try:        inWb = load_workbook(LineTxt.get())             wbSheet0 = inWb['刷卡记录']        AreaTxt.insert(INSERT,"已经打开文件,当前 sheet: "+inWb.sheetnames[0]+'\n','g')     except :        AreaTxt.insert(INSERT,"Error: 请选择格式为 .xlsx 的文件\n",'red')          return    colNum = 0    try:        #copy line 4        dayLine = 4        col = 1        oWbsheet.cell(1,1).value = '工号'        oWbsheet.cell(1,2).value = '人名\\日期'        while wbSheet0.cell(dayLine, col).value != None:             oWbsheet.cell(1,col+2).value = wbSheet0.cell(dayLine,col).value            col = col + 1        colNum = col - 1        print(colNum)    except :        AreaTxt.insert(INSERT,"Error: #copy line 4\n",'red')        return    try:        outLine = 2        lineNum = 5        while wbSheet0.cell(lineNum, 1).value != None:            oWbsheet.cell(outLine,1).value = wbSheet0.cell(lineNum,3).value            oWbsheet.cell(outLine,2).value = wbSheet0.cell(lineNum,11).value            lineNum = lineNum+1                 for j in range(1,colNum+1):   #第二列到 31 的时间 做处理                try:                    res = praseCell(wbSheet0.cell(lineNum, j).value,wbSheet0.cell(lineNum-1,11).value)                except :                    AreaTxt.insert(INSERT,"Error: 比较函数出错\n",'red')                    return                if res == True:                    oWbsheet.cell(outLine,2+j).value = "正常"                    oWbsheet.cell(outLine,2+j).fill = ColorGreen                else:                    oWbsheet.cell(outLine,2+j).value = "异常"                    oWbsheet.cell(outLine,2+j).fill = ColorRed            lineNum = lineNum+1            outLine = outLine+1        AreaTxt.insert(INSERT,"开始保存···\n")         outWb.save('out.xlsx')  #保存输出          AreaTxt.insert(INSERT,"Process finish !\n")     except :        AreaTxt.insert(INSERT,"Error: 处理过程遇到错误\n",'red')        returnroot= Tk() root.title("处理excel") frame= Frame(root)  frame.pack(padx=25,pady=25) #set areaAreaTxt = Text(frame,width=45,height=16,font =("微软雅黑",12))AreaTxt.tag_config('red', foreground='red')AreaTxt.tag_config('g', foreground='green')  AreaTxt.grid(row=3,column=0,padx=30,pady=5,columnspan=2) LineTxt = Entry(frame,foreground = 'blue',font = ('Helvetica', '12'))SelectFileBut = Button(frame,text="选择表格",font=("宋体",15),command=read1)ProcBut = Button(frame ,text="处理表格",font=("宋体",15),width=10,command= fun )LineTxt.grid(row=0,ipadx=5,pady=5,columnspan=2)SelectFileBut.grid(row=1,column=0,padx=5)  ProcBut.grid(row=1,column=1,padx=5,pady=5)   root.mainloop()

2

from xlrd import open_workbookimport xlwtstartWork = '9:00'endWork = '18:00'red = xlwt.easyxf('font: name Times New Roman, color-index red, bold on')gre = xlwt.easyxf('font: name Times New Roman, color-index green, bold on')def read1():    filename= filedialog.askopenfilename()def CompareStartTime(times,peopleName):    if times == None:        return False    else:        timeList = times.split(':')        if not timeList[0].isnumeric():            return False        if int(timeList[0]) >= 9:            return False        else :            return Truedef CompareEndTime(times,peopleName):    if times == None:        return False    else:        timeList = times.split(':')        if not timeList[0].isnumeric():            return False        if int(timeList[0]) < 18:            return False        else :            return Truedef praseCell(context,peopleName):    if context.isspace():        return False    else:        print('cxt',context)        timeList = context.split()        print('tlist',timeList)        if len(timeList)==0:            return False        start = CompareStartTime(timeList[0],peopleName)        end = CompareEndTime(timeList[-1],peopleName)        if start and end:            return True        else:            return Falsedef fun():    print("wt")    wt_book = xlwt.Workbook()    wt_sheet = wt_book.add_sheet('A Test Sheet')    in_book = open_workbook('20190201.xls')         rd_sheet = in_book.sheet_by_name("刷卡记录")    print (rd_sheet.nrows)    print (rd_sheet.ncols)    print (in_book.sheet_names())    colNum = 0    #copy line 4    dayLine = 3    col = 0    wt_sheet.write(0,0,'工号')    wt_sheet.write(0,1,'人名日期')    while col<31:         wt_sheet.write(0,col+2,rd_sheet.cell(dayLine,col).value)        col = col + 1    colNum = col - 1    print(colNum)    outLine = 1    lineNum = 4    while lineNum 

转载地址:http://crqii.baihongyu.com/

你可能感兴趣的文章
Linux系统编程——线程池
查看>>
yfan.qiu linux硬链接与软链接
查看>>
Linux C++线程池实例
查看>>
shared_ptr简介以及常见问题
查看>>
c++11 你需要知道这些就够了
查看>>
c++11 你需要知道这些就够了
查看>>
shared_ptr的一些尴尬
查看>>
C++总结8——shared_ptr和weak_ptr智能指针
查看>>
c++写时拷贝1
查看>>
C++ 写时拷贝 2
查看>>
Linux网络编程---I/O复用模型之poll
查看>>
Java NIO详解
查看>>
单列模式-编写类ConfigManager读取属性文件
查看>>
java中float和double的区别
查看>>
Statement与PreparedStatement区别
查看>>
Tomcat配置数据源步骤以及使用JNDI
查看>>
before start of result set 是什么错误
查看>>
(正则表达式)表单验证
查看>>
在JS中 onclick="save();return false;"return false是
查看>>
JSTL 常用标签总结
查看>>