python 操作excel表格的方法
說明:由于公司oa暫缺,人事妹子在做考勤的時候,需要通過幾個excel表格去交叉比對員工是否有曠工或遲到,工作量大而且容易出錯。這時候it?潘康幕?嶗蠢玻??艘惶焓奔涓?米輿A艘桓鱟遠??瘧盡?/p>
1. 下載相關(guān)python包python操作excel表格可以使用以下三個包xlrd - 讀excel文件xlwt - 寫excel文件,這個不能修改已有的excel文件,只能寫新的文件xlutils - 修改excel文件,其實就是通過xlrd拷貝一份記錄,再進行修改。保存為老的名字就替換了原文件,保存為新的名字就創(chuàng)建一個新文件
注意事項:a. python讀取excel的日期和時間時表格內(nèi)容是2019/5/13,python讀到的值是43606.0,該值為從日期減1899/12/30得到的天數(shù)表格內(nèi)容是9:00:00,python讀到的值是0.375,該值為時間過了一天的比例,即9/24表格內(nèi)容是2019/5/13 9:00:00,python讀到的值是43598.375日期和時間可以直接相加,因為python讀到的都是轉(zhuǎn)化為數(shù)字之后的值
b. python讀取excel的數(shù)字時,如員工編號為181129,最后結(jié)果是181129.0,非整數(shù)
c. 調(diào)用save函數(shù)保存新的excel文件時,后綴名必須是.xls
2. 將python文件轉(zhuǎn)為.bat格式你不可能要求妹子去使用cmd,然后使用python xx.py去執(zhí)行python文件,必須想個辦法搞成傻瓜式的。我們可以通過.bat格式文件實現(xiàn)新建文本文件,重命名為“A考勤小工具.bat”,輸入下面代碼,@py.exe表示后面的參數(shù)是python可執(zhí)行文件@py.exe Akqfx.py
3. 附上相關(guān)代碼和excel格式文本
Akqfx.py
# 該腳本為修正考勤記錄# author: yangbaoimport osfrom datetime import datetimeimport xlrdfrom xlutils.copy import copy# 定義文件是否存在def get_list_file(): current_list = os.listdir() must_list = [’原始數(shù)據(jù).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) # 判斷原始數(shù)據(jù)曠工和遲到是否在請假或外出流程里 # 給額外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(’原始數(shù)據(jù).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(’考勤分析結(jié)束...’) input(’按任意鍵結(jié)束’) else: input(’因為缺少相關(guān)excel文件,考勤分析失敗,退出程序,按任意鍵結(jié)束’)
該文檔僅作個人記錄用
以上就是python 操作excel表格的方法的詳細內(nèi)容,更多關(guān)于python 操作excel表格的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Java基于redis和mysql實現(xiàn)簡單的秒殺(附demo)2. php讀取xml中某個元素的內(nèi)容(PHP5以上才支持)3. IntelliJ IDEA設(shè)置編碼格式的方法4. 如何用python識別滑塊驗證碼中的缺口5. SpringBoot+SpringCache實現(xiàn)兩級緩存(Redis+Caffeine)6. IDEA SpringBoot 項目配置Swagger2的詳細教程7. Kotlin + Flow 實現(xiàn)Android 應(yīng)用初始化任務(wù)啟動庫8. Docker究竟是什么 為什么這么流行 它的優(yōu)點和缺陷有哪些?9. ASP.NET MVC視圖頁使用jQuery傳遞異步數(shù)據(jù)的幾種方式詳解10. AJAX實現(xiàn)省市縣三級聯(lián)動效果
