濮阳杆衣贸易有限公司

主頁 > 知識庫 > Python還能這么玩之只用30行代碼從excel提取個人值班表

Python還能這么玩之只用30行代碼從excel提取個人值班表

熱門標(biāo)簽:地圖標(biāo)注面積 儋州電話機器人 所得系統(tǒng)電梯怎樣主板設(shè)置外呼 北京電銷外呼系統(tǒng)加盟 北瀚ai電銷機器人官網(wǎng)手機版 市場上的電銷機器人 佛山400電話辦理 朝陽手機外呼系統(tǒng) 小蘇云呼電話機器人

一、查找操作

1.Excel 模塊 xlrd,xlwt,xlutils 分別負(fù)責(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)鍵信息(值班時間,姓名)進(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

四、創(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ā)送信息,進(jìn)行提示!

到此這篇關(guān)于Python還能這么玩之只用30行代碼從excel提取個人值班表的文章就介紹到這了,更多相關(guān)Python從excel提取個人值班表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 利用python對Excel中的特定數(shù)據(jù)提取并寫入新表的方法
  • python自動打開瀏覽器下載zip并提取內(nèi)容寫入excel
  • Python實現(xiàn)提取XML內(nèi)容并保存到Excel中的方法
  • python3讀取excel文件只提取某些行某些列的值方法
  • python辦公自動化之excel的操作
  • 教你用Python實現(xiàn)Excel表格處理
  • 教你怎么用Python處理excel實現(xià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)。
  • 相關(guān)文章
  • 下面列出與本文章《Python還能這么玩之只用30行代碼從excel提取個人值班表》相關(guān)的同類信息!
  • 本頁收集關(guān)于Python還能這么玩之只用30行代碼從excel提取個人值班表的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    江达县| 临潭县| 攀枝花市| 大同市| 华池县| 云浮市| 海宁市| 周宁县| 措美县| 白城市| 青铜峡市| 西华县| 右玉县| 夏津县| 陇西县| 将乐县| 沾益县| 崇明县| 长宁县| 千阳县| 罗城| 丰原市| 青冈县| 遂平县| 宾阳县| 安远县| 松江区| 历史| 吉木萨尔县| 房产| 广汉市| 中方县| 长汀县| 本溪| 武宁县| 尚义县| 崇左市| 左贡县| 北京市| 屏东县| 耿马|