1.Excel 模塊 xlrd,xlwt,xlutils 分別負(fù)責(zé) Excel 文件的讀、寫、讀寫轉(zhuǎn)換工作!
2.openpyxl 直接可以對 Excel 文件讀寫!
3.pandas 直接可以對 Excel 文件讀寫!
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)鍵信息(值班時間,姓名)進(jì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
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')
熟悉 openpyxl 模塊的各個功能,方便對 excel 的操作;篩選提取自己關(guān)注的關(guān)鍵信息,重新建表;下一篇根據(jù)值班時間,用 python 自動給自己的微信發(fā)送信息,進(jìn)行提示!
到此這篇關(guān)于Python還能這么玩之只用30行代碼從excel提取個人值班表的文章就介紹到這了,更多相關(guān)Python從excel提取個人值班表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:金融催收 商丘 酒泉 江蘇 定西 龍巖 寧夏 云南
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python還能這么玩之只用30行代碼從excel提取個人值班表》,本文關(guān)鍵詞 Python,還能,這么,玩之,只用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。