Python還能這么玩之只用30行代碼從excel提取個人值班表
一、查找操作
1.Excel 模塊 xlrd,xlwt,xlutils 分別負責(zé) Excel 文件的讀、寫、讀寫轉(zhuǎn)換工作!
2.openpyxl 直接可以對 Excel 文件讀寫!
3.pandas 直接可以對 Excel 文件讀寫!
二、安裝 openpyxl 模塊
pip install openpyxl
三、讀取并篩選值班表中自己的信息
1.讀取所有的值班信息;
2.由于一般情況 excel 都會有部分表格為空,保存全部 None 的 excel 行字符串?dāng)?shù)據(jù);
3.循環(huán)全部的值班數(shù)據(jù),將當(dāng)前行數(shù)據(jù)形成一個數(shù)據(jù)字符串;
4.判斷當(dāng)前值班信息字符串是否含有自己的姓名;
5.對含有自己信息的數(shù)據(jù)中關(guān)鍵信息(值班時間,姓名)進行存儲;
6.然后判斷當(dāng)前字符串是否含有全部 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 openpyxl import time def 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 dutys def 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é)果
七、總結(jié)
熟悉 openpyxl 模塊的各個功能,方便對 excel 的操作;篩選提取自己關(guān)注的關(guān)鍵信息,重新建表;下一篇根據(jù)值班時間,用 python 自動給自己的微信發(fā)送信息,進行提示!
到此這篇關(guān)于Python還能這么玩之只用30行代碼從excel提取個人值班表的文章就介紹到這了,更多相關(guān)Python從excel提取個人值班表內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。