您的位置:首页技术文章
文章详情页

python 操作excel表格的方法

【字号: 日期:2022-07-03 09:58:10浏览:25作者:猪猪

说明:由于公司oa暂缺,人事妹子在做考勤的时候,需要通过几个excel表格去交叉比对员工是否有旷工或迟到,工作量大而且容易出错。这时候it?潘康幕?崂蠢玻??艘惶焓奔涓?米舆A艘桓鲎远??疟尽?/p>

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格式文本

python 操作excel表格的方法

python 操作excel表格的方法

Akqfx.py

# 该脚本为修正考勤记录# author: yangbaoimport osfrom datetime import datetimeimport xlrdfrom xlutils.copy import copy# 定义文件是否存在def get_list_file(): current_list = os.listdir() must_list = [’原始数据.xls’, ’外出.xls’, ’法定假日.xls’, ’请假.xls’] cj_set = set(must_list) - set(current_list) if cj_set: for i in cj_set: print(’{} 不存在,请检查!’.format(i)) return 0 else: return 1# 定义是否存在流程def get_qjorwc(file_name, person_id, input_time): book = xlrd.open_workbook(file_name) book_sheet = book.sheet_by_index(0) flag = 0 for 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 = 1break return flag# 定义是否是法定假日def get_fdjr(input_time): book = xlrd.open_workbook(’法定假日.xls’) book_sheet = book.sheet_by_index(0) flag = 0 for 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 return flagdef main(): 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 in range(ys_book_sheet.nrows): # 查上班时间 if ys_book_sheet.cell_value(i, 5) in unnormal_list: # 查是否是法定假日 dt = ys_book_sheet.cell_value(i, 3)[:10] if get_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/24 qj_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) in unnormal_list: # 查是否是法定假日 dt = ys_book_sheet.cell_value(i, 3)[:10] if get_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/24 qj_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__’: if get_list_file(): print(’开始考勤分析...’) main() print(’考勤分析结束...’) input(’按任意键结束’) else: input(’因为缺少相关excel文件,考勤分析失败,退出程序,按任意键结束’)

该文档仅作个人记录用

以上就是python 操作excel表格的方法的详细内容,更多关于python 操作excel表格的资料请关注好吧啦网其它相关文章!

标签: python
相关文章: