目錄
- 一、查找操作
- 二、安裝 openpyxl 模塊
- 三、讀取并篩選值班表中自己的信息
- 四、創(chuàng)建自己的值班信息表
- 五、全部代碼
- 六、執(zhí)行結(jié)果
- 七、總結(jié)
一、查找操作
1.Excel 模塊 xlrd,xlwt,xlutils 分別負(fù)責(zé) Excel 文件的讀、寫、讀寫轉(zhuǎn)換工作!
2.openpyxl 直接可以對(duì) Excel 文件讀寫!
3.pandas 直接可以對(duì) Excel 文件讀寫!
二、安裝 openpyxl 模塊
三、讀取并篩選值班表中自己的信息
1.讀取所有的值班信息;
2.由于一般情況 excel 都會(huì)有部分表格為空,保存全部 None 的 excel 行字符串?dāng)?shù)據(jù);
3.循環(huán)全部的值班數(shù)據(jù),將當(dāng)前行數(shù)據(jù)形成一個(gè)數(shù)據(jù)字符串;
4.判斷當(dāng)前值班信息字符串是否含有自己的姓名;
5.對(duì)含有自己信息的數(shù)據(jù)中關(guān)鍵信息(值班時(shí)間,姓名)進(jìn)行存儲(chǔ);
6.然后判斷當(dāng)前字符串是否含有全部 None 的數(shù)據(jù);
7.由于值班表沒(méi)有空出的行,所以查到 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)建一個(gè)值班信息表的 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'本次提取值班表時(shí)間:{end_time - start_time}ms')
六、執(zhí)行結(jié)果
![](/d/20211017/5bb2ca2694475544a5030e8f35970131.gif)
七、總結(jié)
熟悉 openpyxl 模塊的各個(gè)功能,方便對(duì) excel 的操作;篩選提取自己關(guān)注的關(guān)鍵信息,重新建表;下一篇根據(jù)值班時(shí)間,用 python 自動(dòng)給自己的微信發(fā)送信息,進(jìn)行提示!
到此這篇關(guān)于Python還能這么玩之只用30行代碼從excel提取個(gè)人值班表的文章就介紹到這了,更多相關(guān)Python從excel提取個(gè)人值班表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 利用python對(duì)Excel中的特定數(shù)據(jù)提取并寫入新表的方法
- python自動(dòng)打開(kāi)瀏覽器下載zip并提取內(nèi)容寫入excel
- Python實(shí)現(xiàn)提取XML內(nèi)容并保存到Excel中的方法
- python3讀取excel文件只提取某些行某些列的值方法
- python辦公自動(dòng)化之excel的操作
- 教你用Python實(shí)現(xiàn)Excel表格處理
- 教你怎么用Python處理excel實(shí)現(xiàn)自動(dòng)化辦公