本来想用python做一个考勤统计的软件,但是由于种种原因,暂停了这个想法,这是个软件的雏形,仅供大家参考
这个小玩意涉及了文件的读取,时间的比较,异常的处理,编码处理,可以用于python的初级练手,接下来,我就把代码贴出来供大家参考:
#coding:utf8
'''
Created on 2016年6月27
@author: hehe
'''
import xlrd
import time
import datetime
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)
# use for to read all tables
def readtable():
data = xlrd.open_workbook(r'C:\Users\8888\Desktop\haha.xlsx')
table = data.sheet_by_index(0)
return table
def get_num_nrow(table):
return table.nrows
def compute_time(time_begin , time_end):
hour , minu = (int)(str(compare_time(time_begin, time_end))[0]),(int)(str(compare_time(time_begin, time_end))[2])*10+(int)(str(compare_time(time_begin, time_end))[3])
return hour*60 + minu
def compare_time(time_begin, time_end):
date_1 = time.strptime(time_begin,"%H:%M")
date_2 = time.strptime(time_end,"%H:%M")
date1=datetime.datetime(date_1[0],date_1[1],date_1[2],date_1[3],date_1[4],date_1[5])
date2=datetime.datetime(date_2[0],date_2[1],date_2[2],date_2[3],date_2[4],date_2[5])
#print date2-date1
return date2-date1
def com_chidao(time_begin,time_s_beg):
a=0
b=0
try:
a=compute_time(time_begin,time_s_beg)
except Exception:
b=compute_time(time_s_beg,time_begin)
return a,b
def com_zaotui(time_begin,time_s_beg):
c=0
d=0
try:
c=compute_time(time_begin,time_s_beg)
except Exception:
d=compute_time(time_s_beg,time_begin)
return c,d
def judge_morning(time_list):
time_begin = time_list[0]
time_s_beg = u'8:30'
time_end = time_list[1]
time_s_end = u'12:00'
if time_begin=='':
print '早上没来'
elif time_begin!='' and time_end=='':
print '未签退'
elif time_begin!='' and time_end!='':
a,b=com_chidao(time_begin,time_s_beg)
#c,d = com_zaotui(time_s_end,time_end)
if a==0 and b!=0:
print '迟到%d分'%b
#if c==0 and d!=0:
# print '早退%d分'%d
else :
print compare_time(time_begin,time_end)
def judge_afternoon(time_list):
time_begin = time_list[2]
time_s_beg = u'14:30'
time_end = time_list[3]
time_s_end = u'18:00'
if time_begin=='':
print '下午没来'
elif time_begin!='' and time_end=='':
print '未签退'
elif time_begin!='' and time_end!='':
a,b=com_chidao(time_begin,time_s_beg)
#c,d = com_zaotui(time_s_end,time_end)
if a==0 and b!=0:
print '迟到%d分'%b
#if c==0 and d!=0:
# print '早退%d分'%d
else :
print compare_time(time_begin,time_end)
def judge_neight(time_list):
time_begin = time_list[4]
time_s_beg = u'19:30'
time_end = time_list[5]
time_s_end = u'22:00'
if time_begin=='':
print '晚上没来'
elif time_begin!='' and time_end=='':
print '未签退'
elif time_begin!='' and time_end!='':
a,b=com_chidao(time_begin,time_s_beg)
#c,d = com_zaotui(time_s_end,time_end)
if a==0 and b!=0:
print '迟到%d分'%b
#if c==0 and d!=0:
# print '早退%d分'%d
else :
print compare_time(time_begin,time_end)
if __name__ == '__main__':
chidao = 0
kuang = 0
kuang_day = 0
val_time = 0
all_time = 0
data = []
table = readtable()
print 'done'
for i in range(get_num_nrow(table) ):
value = table.row_values(i)
if value[0][3]=='日':
continue
time_1_be , time_1_end , time_2_be ,time_2_end , time_3_be , time_3_end =value[1] ,value[3] ,value[6] ,value[8], value[10] ,value[12]
#print time_1_be , time_1_end , time_2_be ,time_2_end , time_3_be , time_3_end
time_list = [time_1_be , time_1_end , time_2_be ,time_2_end , time_3_be , time_3_end]
if time_list[0]=='旷工':
print '旷工'
kuang_day+=1
kuang+=3
continue
#print time_list[0]
#judje mornaing
judge_morning(time_list)
#judje afternoon
judge_afternoon(time_list)
#judge neight
judge_neight(time_list)