Python還能這么玩之只用30行代碼從excel提取個人值班表
1.Excel 模塊 xlrd,xlwt,xlutils 分別負責 Excel 文件的讀、寫、讀寫轉(zhuǎn)換工作!
2.openpyxl 直接可以對 Excel 文件讀寫!
3.pandas 直接可以對 Excel 文件讀寫!
二、安裝 openpyxl 模塊pip install openpyxl三、讀取并篩選值班表中自己的信息
1.讀取所有的值班信息;
2.由于一般情況 excel 都會有部分表格為空,保存全部 None 的 excel 行字符串數(shù)據(jù);
3.循環(huán)全部的值班數(shù)據(jù),將當前行數(shù)據(jù)形成一個數(shù)據(jù)字符串;
4.判斷當前值班信息字符串是否含有自己的姓名;
5.對含有自己信息的數(shù)據(jù)中關(guān)鍵信息(值班時間,姓名)進行存儲;
6.然后判斷當前字符串是否含有全部 None 的數(shù)據(jù);
7.由于值班表沒有空出的行,所以查到 None,直接跳出循環(huán)。
dutys = [] book = openpyxl.load_workbook(’duty.xlsx’,data_only=True) sheet = book.active all_data = book.get_sheet_by_name('日常加班') none_str = ’’.join([str(None).ljust(20) for c in range(1,all_data.max_column+1)]) for r in range(1,all_data.max_row + 1): cur_str = ’’.join([str(all_data.cell(row=r,column=c).value).ljust(20) for c in range(1,all_data.max_column+1)]) if cur_str.find('***') >= 0: dutys.append({'date': all_data.cell(row=r,column=2).value,'name': all_data.cell(row=r,column=3).value }) elif cur_str.find(none_str) >= 0: break return dutys四、創(chuàng)建自己的值班信息表
1.創(chuàng)建一個值班信息表的 excel;
2.將自己的值班信息循環(huán);
3.將信息填入創(chuàng)建的表格。
book = openpyxl.Workbook() sheet = book.active for i in range(len(dutys)): sheet.cell(row=1 + i, column=1).value = dutys[i].get('name') sheet.cell(row=1 + i, column=2).value = f’{dutys[i].get('date')}’ book.save(’my_duty.xlsx’)五、全部代碼
#!/usr/bin/env python'''@Author :Rattenking@Date :2021/06/02 10:19@CSDN :https://blog.csdn.net/m0_38082783'''import openpyxlimport timedef get_my_duty_date(): dutys = [] book = openpyxl.load_workbook(’duty.xlsx’,data_only=True) sheet = book.active all_data = book.get_sheet_by_name('日常加班') none_str = ’’.join([str(None).ljust(20) for c in range(1,all_data.max_column+1)]) for r in range(1,all_data.max_row + 1): cur_str = ’’.join([str(all_data.cell(row=r,column=c).value).ljust(20) for c in range(1,all_data.max_column+1)]) if cur_str.find('***') >= 0: dutys.append({'date': all_data.cell(row=r,column=2).value,'name': all_data.cell(row=r,column=3).value }) elif cur_str.find(none_str) >= 0: break return dutysdef create_my_duty_list(dutys): book = openpyxl.Workbook() sheet = book.active for i in range(len(dutys)): sheet.cell(row=1 + i, column=1).value = dutys[i].get('name') sheet.cell(row=1 + i, column=2).value = f’{dutys[i].get('date')}’ book.save(’my_duty.xlsx’)if __name__ == '__main__': start_time = int(round(time.time() * 1000)) dutys = get_my_duty_date() create_my_duty_list(dutys) end_time = int(round(time.time() * 1000)) print(f’本次提取值班表時間:{end_time - start_time}ms’)六、執(zhí)行結(jié)果
熟悉 openpyxl 模塊的各個功能,方便對 excel 的操作;篩選提取自己關(guān)注的關(guān)鍵信息,重新建表;下一篇根據(jù)值班時間,用 python 自動給自己的微信發(fā)送信息,進行提示!
到此這篇關(guān)于Python還能這么玩之只用30行代碼從excel提取個人值班表的文章就介紹到這了,更多相關(guān)Python從excel提取個人值班表內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
