Python操作xlwings的實(shí)例詳解
阿里云產(chǎn)品費(fèi)用巡檢,一般流程是登錄賬號(hào),再逐項(xiàng)核對填寫。雖然簡單,但如果帳號(hào)多表格多,帳號(hào)間的數(shù)據(jù)有關(guān)聯(lián),填寫起來就比較費(fèi)力氣。幾張表格,可能從下載數(shù)據(jù)到核寫完畢,輾轉(zhuǎn)半個(gè)小時(shí)。
因此在保留excel原文件格式不變的基礎(chǔ)上,自動(dòng)填寫相關(guān)數(shù)值變得重要。
python操作excel的模塊多,xlrd,pandas,xlwings,openpyxl。經(jīng)常搞不清這么多功能類似的模塊有什么區(qū)別,這里發(fā)現(xiàn)xlwings可以派上用場,因?yàn)槲矣袀€(gè)保留excel格式的需求,文件格式:
表1-1

注意:主要修改第10、11行,其它不變。
數(shù)據(jù)來源
通過爬蟲登錄阿里云,下載數(shù)據(jù)寫入csv。帶上日期,如data_07-25.csv
表1-2

爬蟲腳本
#?-*-?coding:?utf-8?-*-
from?selenium?import?webdriver
from?selenium.webdriver.chrome.options?import?Options
import?time,os,glob,csv
from?datetime?import?datetime
options?=?Options()
options.add_argument('--disable-infobars')
options.add_argument('--incognito')
#?options.add_argument('--headless')
bro?=?webdriver.Chrome(executable_path='C:\drf2\drf2\chromedriver.exe',?chrome_options=options)
bro.maximize_window()
bro.get('https://www.aliyun.com/')
bro.implicitly_wait(10)
#點(diǎn)擊首頁的登錄按鈕
bro.find_element_by_xpath('//*[@id="J_3207526240"]/div/div/div[1]/div[2]/div[2]/a[4]').click()
time.sleep(1)
#點(diǎn)擊RAM用戶
bro.find_element_by_xpath('//*[@id="root"]/div/div[2]/div/div[2]/div[1]/div[2]/div[2]/div/div[2]/div[2]/span/div').click()
u?=?bro.find_element_by_xpath('//*[@id="--aliyun-xconsole-app"]/div[1]/div[2]/div/div/div[1]/div[2]/div/div/div[2]/div/div/div/form/div[1]/div[2]/div[1]/span/input')
#用戶名
u.send_keys('')
time.sleep(5)
#點(diǎn)擊下一步
bro.find_element_by_xpath('//*[@id="--aliyun-xconsole-app"]/div[1]/div[2]/div/div/div[1]/div[2]/div/div/div[2]/div/div/div/form/div[5]/button/span').click()
p?=?bro.find_element_by_xpath('//*[@id="--aliyun-xconsole-app"]/div[1]/div[2]/div/div/div[1]/div[2]/div/div/div[2]/div/div/div/form/div[2]/div[2]/span/input')
#密碼
p.send_keys('')
time.sleep(5)
#?點(diǎn)擊登錄按鈕
bro.find_element_by_xpath('//*[@id="--aliyun-xconsole-app"]/div[1]/div[2]/div/div/div[1]/div[2]/div/div/div[2]/div/div/div/form/div[5]/button/span').click()
time.sleep(3)
#?點(diǎn)擊控制臺(tái)
bro.find_element_by_xpath(
????'//*[@id="J_3207526240"]/div/div/div[1]/div[2]/div[2]/a[3]').click()
time.sleep(6)
#切換窗口
bro.switch_to.window(bro.window_handles[-1])
#?點(diǎn)擊費(fèi)用
bro.find_element_by_xpath(
????'/html/body/div[1]/div/div/nav/div[1]/a').click()
time.sleep(3)
bro.switch_to.window(bro.window_handles[-1])
available_credit?=?bro.find_element_by_xpath('//*[@id="app__home"]/div/div/div/div[2]/div[1]/div[1]/div[2]/div/div[1]/span[1]/span').text
time.sleep(3)
#點(diǎn)擊帳單詳情
bro.find_element_by_xpath(
????'//*[@id="root-app"]/div[1]/div/div[6]/div[3]/a').click()
time.sleep(1.5)
#點(diǎn)擊產(chǎn)品量價(jià)匯總
bro.find_element_by_xpath(
????'//*[@id="app__ent-expense"]/div/div/div[1]/div[1]/div/div/div/ul/li[4]/div/span').click()
time.sleep(1.5)
trs?=?bro.find_elements_by_xpath('//tbody/tr[position()>?1]')
for?f?in?os.listdir('C:/Users/Administrator/Desktop/費(fèi)用巡檢/'):
????if?f.startswith('fee'):
????????os.remove('C:/Users/Administrator/Desktop/費(fèi)用巡檢/%s'?%?f)
with?open('C:/Users/Administrator/Desktop/費(fèi)用巡檢/fee_%s.csv'?%?datetime.now().__format__('%m-%d'),?'a+',?newline='',?encoding='gb18030')?as?f:
????f_csv?=?csv.writer(f)
????f_csv.writerow(['可用額度',available_credit.split('?')[1]])
????for?tr?in?trs:
????????tr?=?tr.text.split('\n')
????????f_csv.writerow([tr[0],tr[1].split('?')[1]])
bro.quit()
上手
pandas讀取表1-2的數(shù)據(jù)
為了方便識(shí)別,變量名直接用中文了
import?pandas?as?pd
df?=?pd.read_csv('data_%s.csv'?%?datetime.now().__format__('%m-%d'),?encoding='gbk',?names=['內(nèi)容',?'金額'])
內(nèi)容安全?=?eval(df.iloc[5,?1])
系統(tǒng)短信?=?0
云服務(wù)器ECS流量?=?eval(df.iloc[4,?1])
對象存儲(chǔ)?=?eval(df.iloc[8,?1])
文件存儲(chǔ)?=?eval(df.iloc[6,?1])
視頻點(diǎn)播?=?eval(df.iloc[11,?1])
大數(shù)據(jù)?=?eval(df.iloc[2,?1])?+?eval(df.iloc[7,?1])
CDN?=?eval(df.iloc[1,?1])
日志服務(wù)?=?eval(df.iloc[10,?1])
塊存儲(chǔ)?=?eval(df.iloc[3,?1])
合計(jì)?=?round(內(nèi)容安全?+?系統(tǒng)短信?+?云服務(wù)器ECS流量?+?對象存儲(chǔ)?+?文件存儲(chǔ)?+?視頻點(diǎn)播?+?大數(shù)據(jù)?+?CDN?+?日志服務(wù)?+?塊存儲(chǔ),?2)
余額?=?eval(df.iloc[0,?1].replace(',',?''))
xlwings獲取表1-1sheet
import?xlwings?as?xw from?datetime?import?datetime import?os app?=?xw.App(visible=False,add_book=False) app.display_alerts?=?False app.screen_updating?=?False wb?=?app.books.open(filename) ws?=?wb.sheets[0]
xlwings修改表1-1數(shù)據(jù)
#?修改第10行,expand參數(shù)可以方便的按順序一行寫完
ws.range('B10').options(expand='table').value?=?[內(nèi)容安全,?系統(tǒng)短信,?云服務(wù)器ECS流量,?對象存儲(chǔ),?文件存儲(chǔ),?視頻點(diǎn)播,?大數(shù)據(jù),?CDN,?日志服務(wù),?塊存儲(chǔ),?合計(jì),
????????????????????????????????????????????????????余額]
#?修改第11行
ws.range('e41').value?=?'本月(%s月)已使用%s元,實(shí)際賬戶余額為%s元。'?%?(datetime.now().month,?合計(jì),?余額)
path?=?'D:/桌面/巡檢/%s'?%?datetime.now().__format__('%m-%d')
if?not?os.path.exists(path):
????os.mkdir(path)
wb.save(os.path.join(path,'教育費(fèi)用_%s.xlsx'?%?datetime.now().__format__('%m-%d')))
wb.close()
app.quit()
總結(jié)
通過使用xlwings自動(dòng)修改表格,我的6張表格從原先的操作半小時(shí),到現(xiàn)在鼠標(biāo)duang~duang~duang~幾下即可做好。減少上百次的復(fù)制粘貼點(diǎn)擊后,工作更輕松了。
到此這篇關(guān)于Python操作xlwings的實(shí)例詳解的文章就介紹到這了,更多相關(guān)Python xlwings內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python接口自動(dòng)化系列之unittest結(jié)合ddt的使用教程詳解
這篇文章主要介紹了Python接口自動(dòng)化系列之unittest結(jié)合ddt的使用教程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
media配置及把用戶頭像從數(shù)據(jù)庫展示到前端的操作方法
media配置可以讓用戶上傳的所有文件都固定的存放在某一個(gè)指定的文件夾下,接下來通過本文給大家介紹下media配置及把用戶頭像從數(shù)據(jù)庫展示到前端的操作,需要的朋友可以參考下2022-09-09
基于Python實(shí)現(xiàn)圖片瀏覽器的應(yīng)用程序
圖像瀏覽器應(yīng)用程序是一種非常常見和實(shí)用的工具,這篇文章就來為大家介紹一下如何使用Python編程語言和wxPython庫創(chuàng)建一個(gè)簡單的圖像瀏覽器應(yīng)用程序,感興趣的可以了解下2023-10-10
Python基礎(chǔ)請求庫urllib模塊使用深入探究
在Python中,urllib庫是一個(gè)強(qiáng)大的模塊,用于處理URLs,它包含了多個(gè)子模塊,其中urllib.request是用于發(fā)出HTTP請求的核心組件,本文將深入探討urllib的基本使用、高級(jí)功能以及一些實(shí)際場景的示例,方便更全面地了解這個(gè)重要的網(wǎng)絡(luò)請求工具2024-01-01

