python自动化办公知识点整理汇总_python自动化办公小结
在日常辦公中,經(jīng)常免不了和Excel打交道,每次手工處理數(shù)據(jù),稍微不細(xì)心點(diǎn)。數(shù)據(jù)可能就出錯(cuò)了。而且重復(fù)的任務(wù)又會(huì)占據(jù)大量的工作時(shí)間。那有沒有辦法可以解決這些問題呢?
今天介紹一種方法,可以解決日常工作的重復(fù)工作,節(jié)省時(shí)間,從而優(yōu)化自己工作。今天說(shuō)的就是通過(guò)Python語(yǔ)言的selenium+numpy+js+pandas庫(kù)。完成自動(dòng)化辦公。
首先,需要通過(guò)Python+Selenium+Js 封裝一個(gè)函數(shù)get,用于在公司管理平臺(tái)下載各類所需的數(shù)據(jù)表(如果數(shù)據(jù)是直接以excel提供,則忽略此步驟)。自動(dòng)化辦公目錄圖片如下:
123456.png
封裝的數(shù)據(jù)獲取的get函數(shù):
from selenium import webdriver
import time,random
import json,os
from selenium.webdriver.support.select import Select
import cv2
'''封裝函數(shù),自動(dòng)登錄 //*[@id="img"]驗(yàn)證碼xpath'''
class cook:
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(120)
try:
self.cook_login()
time.sleep(3)
if self.driver.title != '需求管理平臺(tái)':
print('cookie login fail')
'''彈出框處理#send_keys(Keys.ENTER)'''
self.driver.switch_to.alert.accept()
except:
time.sleep(2)
self.driver.delete_all_cookies()
self.driver.get('url')
self.login()
self.cookies_write()
#cookies寫入,從而每次調(diào)用可以跳過(guò)驗(yàn)證碼
def cookies_write(self):
dictCookies = self.driver.get_cookies()
jsonCookies = json.dumps(dictCookies)
os.remove('C:/Users/lenovo/Downloads/cookies4.json')
time.sleep(2)
with open('C:/Users/lenovo/Downloads/cookies4.json', 'w') as f:
f.write(jsonCookies)
print(type(jsonCookies),jsonCookies)
print('收集完成cookie')
'''
:param 賬號(hào)密碼
'''
def login(self,user='賬號(hào)',pwd='密碼'):
time.sleep(1.5)
self.driver.find_element_by_id('j_username').send_keys(user)
time.sleep(1)
self.driver.find_element_by_id('j_password').send_keys(pwd)
# img_yzm = self.driver.find_element_by_xpath('//*[@id="img"]')
# img_yzm.screenshot(r'D:\360MoveData\Users\lenovo\Music\Desktop\yzm.jpg')
# time.sleep(1)
# image1 = cv2.imread(r'D:\360MoveData\Users\lenovo\Music\Desktop\yzm.jpg')
# cv2.imshow('image', image1)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
code = input('\n請(qǐng)輸入驗(yàn)證碼:')
self.driver.find_element_by_id('code').send_keys(code)
log_in = self.driver.find_element_by_xpath("//input[@value='登錄']")
log_in.click()
while self.driver.title == '首頁(yè)登錄':
self.login()
def cook_login(self):
with open('C:/Users/lenovo/Downloads/cookies4.json', 'r') as f:
#, encoding='utf-8')
listCookies = json.loads(f.read())
self.driver.get('http://iimp.sh.cmcc/rip/login.do')
self.driver.delete_all_cookies()
for i in listCookies:
self.driver.add_cookie(i)
self.driver.get('http://iimp.sh.cmcc/rip/chaxun.do')
self.cookies_write()
''':return 網(wǎng)址dict'''
#返回各類數(shù)據(jù)的一個(gè)字典,從而可以通過(guò)鏈接進(jìn)行下載
def back(self):
report_list = []
for link in self.driver.find_elements_by_xpath("//*[@id='sf-menu2']/li/a"):
herf = link.get_attribute('href')
text = link.get_attribute('text')
lis1 = [text, herf]
report_list.append(lis1)
report_list = dict(report_list)
return report_list
'''更改時(shí)間
:param form H5標(biāo)簽的id
:param t 更改的時(shí)間'''
def alter_time(self,form, t):
js = "document.getElementById('%s').removeAttribute('readonly');" % form
self.driver.execute_script(js)
js_value = "document.getElementById('%s').value='%s'" % (form, t)
self.driver.execute_script(js_value)
'''導(dǎo)出報(bào)表函數(shù)'''
def report(self,name,start,end):
report_list = self.back()
if name == '政企業(yè)務(wù)需求報(bào)表':
self.driver.get(report_list['政企業(yè)務(wù)需求報(bào)表'])
self.driver.find_element_by_xpath(
'//*[@id="bd"]/div[1]/div[2]/table/tbody/tr/td/table/tbody/tr/td/input').click()
elif name == '政企KPI跟蹤報(bào)表':
self.driver.get(report_list['政企KPI跟蹤報(bào)表'])
self.driver.find_element_by_xpath(
'//*[@id="bd"]/div[1]/div[2]/table/tbody/tr/td/table/tbody/tr/td/input').click()
time.sleep(3)
elif name == '需求確認(rèn)時(shí)間查詢報(bào)表':
self.driver.get(report_list['需求確認(rèn)時(shí)間查詢報(bào)表'])
self.alter_time('confirmTimeStart', start)
self.alter_time('confirmTimeEnd', end)
self.driver.find_element_by_xpath('// *[ @ id = "export"]').click()
elif name == 'PATCH計(jì)劃':
self.driver.get(report_list['PATCH計(jì)劃'])
self.alter_time('planTimeStart', start)
self.alter_time('planTimeEnd', end)
self.driver.find_element_by_xpath(
'//*[@id="bd"]/div/div[2]/table/tbody/tr/td/table/tbody/tr[10]/td/input[2]').click()
elif name == '業(yè)支廠商工時(shí)報(bào)表':
self.driver.get(report_list['業(yè)支廠商工時(shí)報(bào)表'])
self.alter_time('planTimeStart', start)
self.alter_time('planTimeEnd', end)
self.driver.find_element_by_xpath(
'//*[@id="bd"]/div/div[2]/table/tbody/tr/td/table/tbody/tr[2]/td/input[2]').click()
elif name == '測(cè)試進(jìn)度報(bào)表':
self.driver.get(report_list['測(cè)試進(jìn)度'])
self.alter_time('planTimeStart', start)
self.alter_time('planTimeEnd', end)
time.sleep(1)
self.driver.find_element_by_xpath('//*[@id="testPerson"]').clear()
self.driver.find_element_by_xpath(
'//*[@id="bd"]/div/form/div[1]/table/tbody/tr/td/table/tbody/tr[3]/td/input[2]').click()
elif name == '缺陷工單修復(fù)時(shí)長(zhǎng)報(bào)表':
self.driver.get(report_list['缺陷工單修復(fù)時(shí)長(zhǎng)報(bào)表'])
self.alter_time('planOnlineTimeStart', start)
self.alter_time('planOnlineTimeEnd', end)
self.driver.find_element_by_xpath(
'//*[@id="bd"]/div/div[2]/table/tbody/tr/td/table/tbody/tr[5]/td/input[2]').click()
elif name == '需求查詢':
self.driver.get(report_list['需求查詢'])
self.alter_time('time', start)
self.alter_time('time1', end)
'''下拉選框選擇'''
Select(self.driver.find_element_by_xpath("//select[@id='remandType']")).select_by_visible_text('業(yè)務(wù)支撐')
time.sleep(2)
Select(self.driver.find_element_by_xpath("//select[@id='secondSelect']")).select_by_visible_text('集團(tuán)下發(fā)')
self.driver.find_element_by_xpath("//*[@name='importBtn']").click()
elif name == '敏捷工時(shí)統(tǒng)計(jì)報(bào)表':
self.driver.get(report_list['敏捷工時(shí)統(tǒng)計(jì)報(bào)表'])
self.alter_time('planTimeStart', start)
self.alter_time('planTimeEnd', end)
self.driver.find_element_by_xpath('//*[@id="bd"]/div[1]/div[2]/table/tbody/tr/td/table/tbody/tr[4]/td/input[2]').click()
else:
print('發(fā)生錯(cuò)誤,請(qǐng)檢查判斷條件')
def quit(self):
self.driver.quit()
準(zhǔn)備工作完成后,然后利用Pandas對(duì)表格進(jìn)行處理,處理數(shù)據(jù)的步驟:讀取、排序、篩選、去重、計(jì)算時(shí)間差、透視表、交叉表,刪除數(shù)據(jù)、時(shí)間修改以及excel的函數(shù):計(jì)算首次上線、計(jì)算上線次數(shù)、計(jì)算最后一次上線等等。
以一個(gè)每日任務(wù)代碼為例,代碼如下:
'''政企KPI任務(wù)報(bào)表'''
from control.common.get import cook
from control.common import demand
import time, os, datetime
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
# 每次記得更新時(shí)間
start = '2020-01-01'
end = '2021-01-31'
'''政企任務(wù)的報(bào)表下載'''
report1 = cook()
report1.report('PATCH計(jì)劃', start, end)
report1.report('政企業(yè)務(wù)需求報(bào)表', start, end)
report1.report('政企KPI跟蹤報(bào)表', start, end)
time.sleep(3)
#定義需要剔除考核的工單列表
zq_data = [政企工單列表]
sj_data = [數(shù)據(jù)工單列表]
#創(chuàng)建,處理后數(shù)據(jù)的導(dǎo)出路徑
t1 = demand.ltime(0)
t1path = r'D:\數(shù)據(jù)統(tǒng)計(jì)\12.政企KPI日?qǐng)?bào)/' + t1
if not os.path.exists(t1path):
os.makedirs(t1path)
os.chdir(t1path)
def clean():
time.sleep(2)
#數(shù)據(jù)讀取
data1 = pd.read_excel(r'C:\Users\lenovo\Downloads\政企業(yè)務(wù)需求報(bào)表.xls')
data2 = pd.read_excel(r'C:\Users\lenovo\Downloads\PATCH計(jì)劃2.xls')
df1 = data1.copy()
df1.index = df1['工單編號(hào)']
#數(shù)據(jù)排序和計(jì)算
df1.sort_values(by=['工單編號(hào)', '上線時(shí)間'], inplace=True, ascending=True)
df1['外部工時(shí)'][df1['是否重復(fù)'] == '是'] = df1[df1['是否重復(fù)'] == '是']['外部工時(shí)'] / 2
'''刪除指定的工單'''
for i in zq_data:
demand.drop_data(df1, i)
'''修改時(shí)間'''
df1.loc[工單編號(hào)1, '上線時(shí)間'] = '2018-04-24'
df1.loc[工單編號(hào)2, '上線時(shí)間'] = '2018-03-30'
df1.loc[工單編號(hào)3, '創(chuàng)建時(shí)間'] = '2018-06-12'
df1.loc[工單編號(hào)4, '創(chuàng)建時(shí)間'] = '2018-06-12'
df1.loc[工單編號(hào)5, '創(chuàng)建時(shí)間'] = '2018-06-28'
#更改值為時(shí)間格式
df1['需求確認(rèn)'] = pd.to_datetime(df1['需求確認(rèn)']) # ,format='%Y-%m-%d')
df1['到達(dá)時(shí)間'] = pd.to_datetime(df1['到達(dá)時(shí)間'])
df1['上線時(shí)間'] = pd.to_datetime(df1['上線時(shí)間'])
lst = df1[df1['到達(dá)時(shí)間'].isnull()].工單編號(hào).unique().tolist()
print(lst)
#替換指定工單時(shí)間
for i in lst:
df1.loc[i]['到達(dá)時(shí)間'] = df1.loc[i]['需求確認(rèn)'] - datetime.timedelta(2)
df1.loc['政企-2018-17184', '到達(dá)時(shí)間'] = pd.to_datetime('2018-12-08')
# 修改時(shí)間為年-月-日 去除時(shí)分秒
df1['需求確認(rèn)'] = df1['需求確認(rèn)'].apply(lambda x: x.strftime('%Y-%m-%d'))
df1['到達(dá)時(shí)間'] = df1['到達(dá)時(shí)間'].apply(lambda x: x.strftime('%Y-%m-%d'))
df1['上線時(shí)間'] = df1['上線時(shí)間'].apply(lambda x: x.strftime('%Y-%m-%d'))
df2 = data2.copy()
df2.index = df2['工單編號(hào)']
df2['計(jì)劃上線時(shí)間'] = pd.to_datetime(df2['計(jì)劃上線時(shí)間'])
df2.sort_values(by='計(jì)劃上線時(shí)間', inplace=True, ascending=True)
#計(jì)算最后一次上線工單
df2 = df2.groupby('工單編號(hào)').last()
df2['計(jì)劃上線時(shí)間'] = df2['計(jì)劃上線時(shí)間'].apply(lambda x: x.strftime('%Y-%m-%d'))
# 篩選出指定部門的工單
df2 = df2[df2['提出部門'].str.contains('數(shù)據(jù)業(yè)務(wù)中心') | df2['提出部門'].str.contains('產(chǎn)品運(yùn)營(yíng)支撐中心') | df2['提出部門'].str.contains('客戶響應(yīng)與產(chǎn)品運(yùn)營(yíng)中心')]
# 篩選出指定需求負(fù)責(zé)人的工單,后期用字典實(shí)現(xiàn)篩選
df2 = df2[df2['需求負(fù)責(zé)人'].str.contains('name1') | df2['需求負(fù)責(zé)人'].str.contains('name2') |
df2['需求負(fù)責(zé)人'].str.contains('name3') | df2['需求負(fù)責(zé)人'].str.contains('name4') |
df2['需求負(fù)責(zé)人'].str.contains('name5') | df2['需求負(fù)責(zé)人'].str.contains('name6') |
df2['需求負(fù)責(zé)人'].str.contains('name7') | df2['需求負(fù)責(zé)人'].str.contains('name8') |
df2['需求負(fù)責(zé)人'].str.contains('name9') | df2['需求負(fù)責(zé)人'].str.contains('name10') |
df2['需求負(fù)責(zé)人'].str.contains('name11') | df2['需求負(fù)責(zé)人'].str.contains('name12')]
# 刪除指定的行
for i in sj_data:
try:
demand.drop_data(df2, i)
except:
print('無(wú)法刪除的行:%s' % i)
df2['工單編號(hào)'] = df2.index
data3 = pd.read_excel(r'D:\360MoveData\Users\lenovo\Music\Desktop\svn\政企KPI日?qǐng)?bào).xlsx')
data3.index = data3['工單編號(hào)']
a = data3[data3.index.str.contains('數(shù)據(jù)')][['需求確認(rèn)', '到達(dá)時(shí)間']]
a['需求確認(rèn)'] = pd.to_datetime(a['需求確認(rèn)'])
a['到達(dá)時(shí)間'] = pd.to_datetime(a['到達(dá)時(shí)間'])
a['需求確認(rèn)'] = a['需求確認(rèn)'].apply(lambda x: x.strftime('%Y-%m-%d'))
a['到達(dá)時(shí)間'] = a['到達(dá)時(shí)間'].apply(lambda x: x.strftime('%Y-%m-%d'))
#兩張數(shù)據(jù)表合并
df2 = pd.merge(df2, a, left_index=True, right_index=True, how='left')
concat2 = df2[['工單編號(hào)', '工單名稱', '工單簡(jiǎn)介', '創(chuàng)建人', '需求負(fù)責(zé)人', '創(chuàng)建時(shí)間', '需求確認(rèn)', '計(jì)劃上線時(shí)間', '到達(dá)時(shí)間']]
#修改列名
concat2.rename(columns={'計(jì)劃上線時(shí)間': '上線時(shí)間', '工單簡(jiǎn)介': '簡(jiǎn)介'}, inplace=True)
df = pd.concat([df1, concat2])
#數(shù)據(jù)排序,后期用insert插入方式實(shí)現(xiàn)
df = df[['工單編號(hào)', '工單名稱', '簡(jiǎn)介', '創(chuàng)建人', '需求負(fù)責(zé)人', '創(chuàng)建時(shí)間', '需求確認(rèn)', '上線時(shí)間', '到達(dá)時(shí)間',
'期望上線時(shí)間', '需求期望上線時(shí)間', '到達(dá)總控時(shí)間', '一級(jí)總控拆分', '來(lái)源部門', '工單大類', '工單類型',
'一級(jí)業(yè)務(wù)標(biāo)簽', '二級(jí)業(yè)務(wù)標(biāo)簽', '一級(jí)指標(biāo)庫(kù)標(biāo)簽', '二級(jí)指標(biāo)庫(kù)標(biāo)簽', '三級(jí)指標(biāo)庫(kù)標(biāo)簽', '上線次數(shù)', '外部工時(shí)',
'是否重復(fù)']]
print('數(shù)據(jù)已經(jīng)處理完成,正在導(dǎo)出中……')
os.chdir(r'D:\360MoveData\Users\lenovo\Music\Desktop')
# 寫入Excel,并導(dǎo)出
shet = pd.ExcelWriter('政企KPI日?qǐng)?bào).xlsx')
df.to_excel(shet, sheet_name='政企KPI報(bào)表', index=False)
df1.to_excel(shet, sheet_name='政企需求表')
df2.to_excel(shet, sheet_name='patch計(jì)劃表')
shet.close()
print('本次政企據(jù)共%i條' % len(df1))
print('本次數(shù)據(jù)共%i條' % len(df2))
print('本次政企日?qǐng)?bào)數(shù)據(jù)共%i條' % len(df))
print('\n在' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '分,導(dǎo)出處理后數(shù)據(jù)')
通過(guò)自動(dòng)化,以前每次一小時(shí)左右完成的數(shù)據(jù)處理任務(wù),現(xiàn)在只需要幾分鐘就可以完成。而且自動(dòng)化處理的數(shù)據(jù)準(zhǔn)確性高,防止手工操作可能由于一些原因?qū)е聰?shù)據(jù)的不準(zhǔn)確性。自動(dòng)化省出來(lái)工作時(shí)間,從而可以用于繼續(xù)封裝其他任務(wù),提高整體效率。從而進(jìn)入一個(gè)良性循環(huán),低成本,高回報(bào)。
總結(jié)
以上是生活随笔為你收集整理的python自动化办公知识点整理汇总_python自动化办公小结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022国内汽车召回盘点 特斯拉成“召回
- 下一篇: 局部页面切换url为什么不变_pytho