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