Description: As the company oa temporary shortage, personnel attendance sister was doing when required by several excel spreadsheet to cross over whether employees have absenteeism or tardiness, heavy workload and error-prone.
This time it's coming opportunity Cock wire, I spent a day to sister line and an automated script.
1. Download the relevant package python
python operation excel spreadsheet can use the following three packages
xlrd - read excel file
xlwt - write excel file, this can not modify an existing excel file, only write the new file
xlutils - modify excel file is actually a copy of the recording by xlrd , and then modify it. Save for the old name, it replaces the original file, save it to create a new file with a new name
Note:
A date and time when reading python excel in.
Table of contents is 2019/5/13, python 43606.0 read value, the number of days from the date of the value obtained Save 1899/12/30
table of contents is 9: 00: 00, python read value is 0.375, which is the proportion of time over the day, that is 9/24
the table of contents is 2019/5/13 9: 00: 00, python read value is 43598.375
date and time It can be directly added as the value after the python are read into a digital
When b. Python read the digital excel, such as employee number is 181 129, the final result is 181,129.0, non-integer
c. When you call the save function to save the new excel file extension must be .xls
2. The python into .bat file format
You can not ask my sister to use cmd, and then use python xx.py to run python files, you must find a way to mess fool. We can achieve by .bat file format
new text file, rename it to "A Attendance gadgets .bat", enter the code below, @ py.exe indicates that the following parameters are python executable files
@ py.exe Akqfx.py
3. Include relevant code and excel format text
Raw data .xls
Go .xls
Leave .xls
Statutory holidays .xls
Akqfx.py
# The script correcting attendance record # author: yangbao Import OS from datetime Import datetime Import to xlrd from xlutils.copy Import Copy # define whether the file exists DEF get_list_file (): current_list = the os.listdir () must_list = [ ' raw data .xls ' , ' out * .xls ' , ' holidays * .xls ' , ' leave * .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): ifint (book_sheet.cell_value (I,. 1)) == int (the person_id): # different files, the processing time of different IF file_name == ' leave * .xls ' : cell_begin = book_sheet.cell_value (I,. 4 ) cell_end = book_sheet.cell_value (I,. 5 ) the 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 ) # Analyzing raw data absenteeism and whether the leave or go out late in the process # to 5min additional grace period if cell_begin-5/1440 <= input_time <= cell_end+5/1440: flag = 1 break 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 flag def 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' ) + ' Corrected * .xls ' new_ys_book.save (new_excel_name) IF the __name__ == ' __main__ ' : IF get_list_file (): Print ( ' start time and attendance analysis ... ' ) main () Print ( ' attendance analysis end .. . ' ) the INPUT ( ' press any key to end ' ) the else : the INPUT ( ' because the associated excel file is missing, attendance analysis fails, quit the program, press any key to end ' )
The records document only for personal use