python做excel表格代码_python - 操作excel表格

说明:由于公司oa暂缺,人事妹子在做考勤的时候,需要通过几个excel表格去交叉比对员工是否有旷工或迟到,工作量大而且容易出错。

这时候it屌丝的机会来啦,花了一天时间给妹子撸了一个自动化脚本。

1. 下载相关python包

python操作excel表格可以使用以下三个包

xlrd - 读excel文件

xlwt - 写excel文件,这个不能修改已有的excel文件,只能写新的文件

xlutils - 修改excel文件,其实就是通过xlrd拷贝一份记录,再进行修改。保存为老的名字就替换了原文件,保存为新的名字就创建一个新文件

注意事项:

a. python读取excel的日期和时间时

表格内容是2019/5/13,python读到的值是43606.0,该值为从日期减1899/12/30得到的天数

表格内容是9:00:00,python读到的值是0.375,该值为时间过了一天的比例,即9/24

表格内容是2019/5/13 9:00:00,python读到的值是43598.375

日期和时间可以直接相加,因为python读到的都是转化为数字之后的值

b. python读取excel的数字时,如员工编号为181129,最后结果是181129.0,非整数

c. 调用save函数保存新的excel文件时,后缀名必须是.xls

2. 将python文件转为.bat格式

你不可能要求妹子去使用cmd,然后使用python xx.py去执行python文件,必须想个办法搞成傻瓜式的。我们可以通过.bat格式文件实现

新建文本文件,重命名为“A考勤小工具.bat”,输入下面代码,@py.exe表示后面的参数是python可执行文件

@py.exe Akqfx.py

3. 附上相关代码和excel格式文本

1399177-20190702163339709-2055902877.png

原始数据.xls

1399177-20190702163351751-1608191050.png

外出.xls

1399177-20190702163404477-933679883.png

请假.xls

1399177-20190702163421224-1684264074.png

法定假日.xls

1399177-20190702163437583-640343490.png

Akqfx.py

ContractedBlock.gif

ExpandedBlockStart.gif

#该脚本为修正考勤记录#author: yangbao

importosfrom datetime importdatetimeimportxlrdfrom xlutils.copy importcopy#定义文件是否存在

defget_list_file():

current_list=os.listdir()

must_list= ['原始数据.xls', '外出.xls', '法定假日.xls', '请假.xls']

cj_set= set(must_list) -set(current_list)ifcj_set:for i incj_set:print('{} 不存在,请检查!'.format(i))return0else:return 1

#定义是否存在流程

defget_qjorwc(file_name, person_id, input_time):

book=xlrd.open_workbook(file_name)

book_sheet=book.sheet_by_index(0)

flag=0for i in range(1, book_sheet.nrows):if int(book_sheet.cell_value(i, 1)) ==int(person_id):#文件不同,时间处理不同

if file_name == '请假.xls':

cell_begin= book_sheet.cell_value(i, 4)

cell_end= book_sheet.cell_value(i, 5)else:

cell_begin= book_sheet.cell_value(i, 3) + book_sheet.cell_value(i, 4)

cell_end= book_sheet.cell_value(i, 5) + book_sheet.cell_value(i, 6)#判断原始数据旷工和迟到是否在请假或外出流程里

#给额外5min的宽限时间

if cell_begin-5/1440 <= input_time <= cell_end+5/1440:

flag= 1

break

returnflag#定义是否是法定假日

defget_fdjr(input_time):

book= xlrd.open_workbook('法定假日.xls')

book_sheet=book.sheet_by_index(0)

flag=0for i in range(1, book_sheet.nrows):

dt= datetime(*xlrd.xldate_as_tuple(book_sheet.cell_value(i, 0), 0))if dt.strftime('%Y-%m-%d') ==input_time:

flag= 1

break

returnflagdefmain():

ys_book= xlrd.open_workbook('原始数据.xls')

ys_book_sheet=ys_book.sheet_by_index(0)

new_ys_book=copy(ys_book)

new_ys_book_sheet=new_ys_book.get_sheet(0)

unnormal_list= ['旷工', '迟到']for i inrange(ys_book_sheet.nrows):#查上班时间

if ys_book_sheet.cell_value(i, 5) inunnormal_list:#查是否是法定假日

dt = ys_book_sheet.cell_value(i, 3)[:10]ifget_fdjr(dt):

new_ys_book_sheet.write(i,5, '*')#查是否有流程

if ys_book_sheet.cell_value(i, 4) != '':

cell_on_time= ys_book_sheet.cell_value(i, 3)[:10] + ' ' + ys_book_sheet.cell_value(i, 4)

cell_on_time_format= datetime.strptime(cell_on_time, "%Y-%m-%d %H:%M:%S") \- datetime.strptime('1899-12-30', '%Y-%m-%d')

cell_on_time_number= cell_on_time_format.days + cell_on_time_format.seconds / (24 * 3600)if 12 < cell_on_time_format.seconds / 3600 < 13:

cell_on_time_number= cell_on_time_format.days + 11.5/24

else:

cell_on_time= ys_book_sheet.cell_value(i, 3)[:10]

cell_on_time_format= datetime.strptime(cell_on_time, "%Y-%m-%d") \- datetime.strptime('1899-12-30', '%Y-%m-%d')

cell_on_time_number= cell_on_time_format.days + cell_on_time_format.seconds / (24 * 3600) + 9/24qj_on_flag= get_qjorwc('请假.xls', ys_book_sheet.cell_value(i, 1), cell_on_time_number)

wc_on_flag= get_qjorwc('外出.xls', ys_book_sheet.cell_value(i, 1), cell_on_time_number)if qj_on_flag == 1 or wc_on_flag == 1:

new_ys_book_sheet.write(i,5, '已有流程')

new_ys_book_sheet.write(i,11, '')#查下班时间

if ys_book_sheet.cell_value(i, 7) inunnormal_list:#查是否是法定假日

dt = ys_book_sheet.cell_value(i, 3)[:10]ifget_fdjr(dt):

new_ys_book_sheet.write(i,7, '*')

new_ys_book_sheet.write(i,11, '')#查是否有流程

if ys_book_sheet.cell_value(i, 6) != '':

cell_out_time= ys_book_sheet.cell_value(i, 3)[:10] + ' ' + ys_book_sheet.cell_value(i, 6)

cell_out_time_format= datetime.strptime(cell_out_time, "%Y-%m-%d %H:%M:%S") \- datetime.strptime('1899-12-30', '%Y-%m-%d')

cell_out_time_number= cell_out_time_format.days + cell_out_time_format.seconds / (24 * 3600)if 12 < cell_out_time_format.seconds / 3600 < 13:

cell_out_time_number= cell_out_time_format.days + 13.5/24

else:

cell_out_time= ys_book_sheet.cell_value(i, 3)[:10]

cell_out_time_format= datetime.strptime(cell_out_time, "%Y-%m-%d") \- datetime.strptime('1899-12-30', '%Y-%m-%d')

cell_out_time_number= cell_out_time_format.days + cell_out_time_format.seconds / (24 * 3600) + 18/24qj_out_flag= get_qjorwc('请假.xls', ys_book_sheet.cell_value(i, 1), cell_out_time_number)

wc_out_flag= get_qjorwc('外出.xls', ys_book_sheet.cell_value(i, 1), cell_out_time_number)if qj_out_flag == 1 or wc_out_flag == 1:

new_ys_book_sheet.write(i,7, '已有流程')

new_ys_book_sheet.write(i,11, '')

new_excel_name= datetime.now().strftime('%Y%m%d_%H%M%S')+'校正后.xls'new_ys_book.save(new_excel_name)if __name__ == '__main__':ifget_list_file():print('开始考勤分析...')

main()print('考勤分析结束...')

input('按任意键结束')else:

input('因为缺少相关excel文件,考勤分析失败,退出程序,按任意键结束')

View Code

该文档仅作个人记录用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值