利用Python實(shí)現(xiàn)讀取Word表格計(jì)算匯總并寫(xiě)入Excel
前言
快過(guò)年了,又到了公司年底評(píng)級(jí)的時(shí)候了。今年的評(píng)級(jí)和往常一下,每個(gè)人都要填寫(xiě)公司的民主評(píng)議表,給各個(gè)同事進(jìn)行評(píng)價(jià)打分,然后部門(mén)收集起來(lái)根據(jù)收集上來(lái)的評(píng)價(jià)表進(jìn)行匯總統(tǒng)計(jì)。想想要收集幾十號(hào)人的評(píng)價(jià)表,并根據(jù)每個(gè)人的評(píng)價(jià)表又要填到Excel中進(jìn)行匯總計(jì)算統(tǒng)計(jì)給出每個(gè)人的評(píng)價(jià),就頭大。雖然不是個(gè)什么難事,但是是個(gè)無(wú)腦的細(xì)致活。幾十個(gè)人的評(píng)價(jià)也得要花大半天的時(shí)間來(lái)弄,而且搞多了還容易搞錯(cuò)。如是就想起干脆用Python寫(xiě)個(gè)小程序自動(dòng)來(lái)處理這些臟活累活,評(píng)級(jí)年年都要評(píng),每年都可以用。
要做的事情就是讀放到某個(gè)文件夾中的word文檔中的評(píng)價(jià)表格,根據(jù)表格內(nèi)容進(jìn)行處理,然后匯總所有的表格數(shù)據(jù),根據(jù)計(jì)算規(guī)則,算出每個(gè)人的評(píng)分,在根據(jù)評(píng)分計(jì)算每個(gè)人的評(píng)價(jià)。匯總后寫(xiě)入Excel中。
不可否認(rèn)用Python來(lái)實(shí)現(xiàn)這樣的事情真的是太方便了,人生苦短我用Python。
我是用的python的docx包來(lái)處理word,用pandas來(lái)處理數(shù)據(jù)并寫(xiě)入excel
一、首先導(dǎo)入包
pip install docx pip install pandas
pandas寫(xiě)excel依賴(lài)openpyxl包所以也到導(dǎo)入
pip install openpyxl
二、讀評(píng)價(jià)表所在的目錄文件
通過(guò)python的os包,列出文件夾里面的文件,識(shí)別出.docx的文件
files=os.listdir(filepah) for file in files: if file.find('.docx')>0: docfilepah=filepah+file
三、讀word文件,處理word中的表格數(shù)據(jù)
data=[] #讀word的docx評(píng)議表文件,并讀取word中的表格數(shù)據(jù) def procdoc(docfilepath): document=Document(docfilepath) tables=document.tables table=tables[0] for i in range(1,len(table.rows)): id=int(table.cell(i,0).text) name=table.cell(i,1).text excellent=0 if table.cell(i,2).text!='' and table.cell(i,2).text is not None: excellent=1 competent = 0 if table.cell(i, 3).text!='' and table.cell(i, 3).text is not None: competent=1 basicacompetent=0 if table.cell(i, 4).text!='' and table.cell(i, 4).text is not None: basicacompetent=1 notcompetent = 0 if table.cell(i, 5).text!='' and table.cell(i, 5).text is not None: notcompetent=1 dontunderstand =0 if table.cell(i, 6).text!='' and table.cell(i, 6).text is not None: dontunderstand=1 appraisedata=[id,name,excellent,competent,basicacompetent,notcompetent,dontunderstand] data.append(appraisedata)
四、統(tǒng)計(jì)計(jì)算
通過(guò)pandas直接對(duì)數(shù)據(jù)進(jìn)行統(tǒng)計(jì)計(jì)算,避免了傳統(tǒng)的循環(huán)計(jì)算。
df = pd.DataFrame(data,columns=['序號(hào)','姓名','優(yōu)秀','稱(chēng)職','基本稱(chēng)職','不稱(chēng)職','不了解']) df=df.groupby(['序號(hào)','姓名']).sum() #匯總每個(gè)人每一項(xiàng)的評(píng)分 df['票數(shù)'] = df.apply(lambda x: x.sum(), axis=1) #統(tǒng)計(jì)票數(shù) df['計(jì)分'] = (df['優(yōu)秀']*95+df['稱(chēng)職']*85+df['基本稱(chēng)職']*75+df['不稱(chēng)職']*65+df['不了解']*0)/len(df)#根據(jù)規(guī)則計(jì)分 df['評(píng)價(jià)']=df['計(jì)分'].map(getscore) #根據(jù)規(guī)則評(píng)價(jià)評(píng)級(jí)
計(jì)分方法:民主評(píng)議得分=Σ各等級(jí)票數(shù)*等級(jí)計(jì)分分?jǐn)?shù)/總票數(shù),其中“優(yōu)秀”計(jì)95分,“稱(chēng)職”計(jì)85分,“基本稱(chēng)職”計(jì)75分,“不稱(chēng)職”計(jì)65分,“不了解”不計(jì)分。
#根據(jù)評(píng)分規(guī)則計(jì)算評(píng)級(jí) def getscore(x): if x>=95: score='優(yōu)秀' elif x>=80 and x<95: score='稱(chēng)職' elif x>=75 and x<80: score='基本稱(chēng)職' elif x<75: score='不稱(chēng)職' return score
五、將統(tǒng)計(jì)計(jì)算結(jié)果寫(xiě)入?yún)R總Excel
通過(guò)pandas直接可以將dataframe寫(xiě)入到Excel文件
#將匯總計(jì)算好的數(shù)據(jù)寫(xiě)入Excel def write2excle(exclefile,dataframe): writer = pd.ExcelWriter(exclefile) dataframe.to_excel(writer) writer.save() print('輸出成功')
完整代碼
Python不到八十行代碼,實(shí)現(xiàn)讀Word->處理表格數(shù)據(jù)->匯總計(jì)算數(shù)據(jù)->寫(xiě)Excel。
完整的代碼如下:
import os import pandas as pd from docx import Document data=[] #讀word的docx評(píng)議表文件,并讀取word中的表格數(shù)據(jù) def procdoc(docfilepath): document=Document(docfilepath) tables=document.tables table=tables[0] for i in range(1,len(table.rows)): id=int(table.cell(i,0).text) name=table.cell(i,1).text excellent=0 if table.cell(i,2).text!='' and table.cell(i,2).text is not None: excellent=1 competent = 0 if table.cell(i, 3).text!='' and table.cell(i, 3).text is not None: competent=1 basicacompetent=0 if table.cell(i, 4).text!='' and table.cell(i, 4).text is not None: basicacompetent=1 notcompetent = 0 if table.cell(i, 5).text!='' and table.cell(i, 5).text is not None: notcompetent=1 dontunderstand =0 if table.cell(i, 6).text!='' and table.cell(i, 6).text is not None: dontunderstand=1 appraisedata=[id,name,excellent,competent,basicacompetent,notcompetent,dontunderstand] data.append(appraisedata) #讀取評(píng)議表的目錄,并處理目錄中的docx文件,根據(jù)評(píng)議表計(jì)算評(píng)分,寫(xiě)入?yún)R總表。 def readfile(filepah): files=os.listdir(filepah) for file in files: if file.find('.docx')>0: docfilepah=filepah+file procdoc(docfilepah) df = pd.DataFrame(data,columns=['序號(hào)','姓名','優(yōu)秀','稱(chēng)職','基本稱(chēng)職','不稱(chēng)職','不了解']) print(df) df=df.groupby(['序號(hào)','姓名']).sum() df['票數(shù)'] = df.apply(lambda x: x.sum(), axis=1) df['計(jì)分'] = (df['優(yōu)秀']*95+df['稱(chēng)職']*85+df['基本稱(chēng)職']*75+df['不稱(chēng)職']*65+df['不了解']*0)/len(df) df['評(píng)價(jià)']=df['計(jì)分'].map(getscore) print(df) write2excle('民主評(píng)議\\民主評(píng)議表匯總.xlsx',df) #根據(jù)評(píng)分規(guī)則計(jì)算評(píng)級(jí) def getscore(x): if x>=95: score='優(yōu)秀' elif x>=80 and x<95: score='稱(chēng)職' elif x>=75 and x<80: score='基本稱(chēng)職' elif x<75: score='不稱(chēng)職' return score #將匯總計(jì)算好的數(shù)據(jù)寫(xiě)入Excel def write2excle(exclefile,dataframe): writer = pd.ExcelWriter(exclefile) dataframe.to_excel(writer) writer.save() print('輸出成功') if __name__ == '__main__': readfile('民主評(píng)議\\')
全部源代碼:https://github.com/xiejava1018/pythonprocword
總結(jié)
到此這篇關(guān)于利用Python實(shí)現(xiàn)讀取Word表格計(jì)算匯總并寫(xiě)入Excel的文章就介紹到這了,更多相關(guān)Python讀取Word計(jì)算匯總寫(xiě)入Excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python使用pywinauto驅(qū)動(dòng)微信客戶(hù)端實(shí)現(xiàn)公眾號(hào)爬蟲(chóng)
- python+pywinauto+lackey實(shí)現(xiàn)PC端exe自動(dòng)化的示例代碼
- python實(shí)現(xiàn)按鍵精靈找色點(diǎn)擊功能教程,使用pywin32和Pillow庫(kù)
- PythonPC客戶(hù)端自動(dòng)化實(shí)現(xiàn)原理(pywinauto)
- Python辦公自動(dòng)化從Excel中計(jì)算整理數(shù)據(jù)并寫(xiě)入Word
- Python辦公自動(dòng)化Word轉(zhuǎn)Excel文件批量處理
- 一文教你如何用Python輕輕松松操作Excel,Word,CSV
- 使用Python自動(dòng)化Microsoft Excel和Word的操作方法
- Python實(shí)現(xiàn)Word表格轉(zhuǎn)成Excel表格的示例代碼
- Python?pywin32實(shí)現(xiàn)word與Excel的處理
相關(guān)文章
python中queue.Queue之task_done的用法
這篇文章主要介紹了python中queue.Queue之task_done的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02python使用tkinter打造三維繪圖系統(tǒng)的示例代碼
Python?的?tkinter?模塊是一個(gè)常用的?GUI(圖形用戶(hù)界面)工具包,它能夠讓你創(chuàng)建窗口應(yīng)用程序,你可以使用它來(lái)構(gòu)建用戶(hù)友好的界面,包括按鈕、標(biāo)簽、文本框、列表框等各種控件,本文講給大家介紹如何使用tkinter打造三維繪圖系統(tǒng),需要的朋友可以參考下2023-08-08Python實(shí)現(xiàn)自定義包的實(shí)例詳解
這篇文章主要介紹了實(shí)現(xiàn)自定義包的方法,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12使用Python的Flask框架表單插件Flask-WTF實(shí)現(xiàn)Web登錄驗(yàn)證
Flask處理表單除了本身的WTForms包,使用Flask-WTF擴(kuò)展來(lái)增強(qiáng)表單功能也是很多開(kāi)發(fā)者的選擇,這里我們就來(lái)講解如何使用Python的Flask框架表單插件Flask-WTF實(shí)現(xiàn)Web登錄驗(yàn)證2016-07-07Django與圖表的數(shù)據(jù)交互的實(shí)現(xiàn)
本文主要介紹了Django與圖表的數(shù)據(jù)交互的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08移除Selenium中window.navigator.webdriver值
這篇文章主要為大家介紹了如何正確的移除Selenium中window.navigator.webdriver的值方法步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06