利用Python實(shí)現(xiàn)讀取Word表格計(jì)算匯總并寫入Excel
前言
快過年了,又到了公司年底評(píng)級(jí)的時(shí)候了。今年的評(píng)級(jí)和往常一下,每個(gè)人都要填寫公司的民主評(píng)議表,給各個(gè)同事進(jìn)行評(píng)價(jià)打分,然后部門收集起來根據(jù)收集上來的評(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è)無腦的細(xì)致活。幾十個(gè)人的評(píng)價(jià)也得要花大半天的時(shí)間來弄,而且搞多了還容易搞錯(cuò)。如是就想起干脆用Python寫個(gè)小程序自動(dòng)來處理這些臟活累活,評(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à)。匯總后寫入Excel中。

不可否認(rèn)用Python來實(shí)現(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
二、讀評(píng)價(jià)表所在的目錄文件
通過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ì)算
通過pandas直接對(duì)數(shù)據(jù)進(jìn)行統(tǒng)計(jì)計(jì)算,避免了傳統(tǒng)的循環(huán)計(jì)算。
df = pd.DataFrame(data,columns=['序號(hào)','姓名','優(yōu)秀','稱職','基本稱職','不稱職','不了解']) 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['稱職']*85+df['基本稱職']*75+df['不稱職']*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分,“稱職”計(jì)85分,“基本稱職”計(jì)75分,“不稱職”計(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='稱職'
elif x>=75 and x<80:
score='基本稱職'
elif x<75:
score='不稱職'
return score
五、將統(tǒng)計(jì)計(jì)算結(jié)果寫入?yún)R總Excel
通過pandas直接可以將dataframe寫入到Excel文件
#將匯總計(jì)算好的數(shù)據(jù)寫入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ù)->寫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)分,寫入?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)秀','稱職','基本稱職','不稱職','不了解'])
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['稱職']*85+df['基本稱職']*75+df['不稱職']*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='稱職'
elif x>=75 and x<80:
score='基本稱職'
elif x<75:
score='不稱職'
return score
#將匯總計(jì)算好的數(shù)據(jù)寫入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ì)算匯總并寫入Excel的文章就介紹到這了,更多相關(guān)Python讀取Word計(jì)算匯總寫入Excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python使用pywinauto驅(qū)動(dòng)微信客戶端實(shí)現(xiàn)公眾號(hào)爬蟲
- python+pywinauto+lackey實(shí)現(xiàn)PC端exe自動(dòng)化的示例代碼
- python實(shí)現(xiàn)按鍵精靈找色點(diǎn)擊功能教程,使用pywin32和Pillow庫
- PythonPC客戶端自動(dòng)化實(shí)現(xiàn)原理(pywinauto)
- Python辦公自動(dòng)化從Excel中計(jì)算整理數(shù)據(jù)并寫入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微信公眾號(hào)開發(fā)簡(jiǎn)單流程實(shí)現(xiàn)
這篇文章主要介紹了python微信公眾號(hào)開發(fā)簡(jiǎn)單流程實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
windows10 pycharm下安裝pyltp庫和加載模型實(shí)現(xiàn)語義角色標(biāo)注的示例代碼
這篇文章主要介紹了windows10 pycharm下安裝pyltp庫和加載模型實(shí)現(xiàn)語義角色標(biāo)注,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Python數(shù)據(jù)結(jié)構(gòu)與算法之圖的基本實(shí)現(xiàn)及迭代器實(shí)例詳解
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之圖的基本實(shí)現(xiàn)及迭代器,結(jié)合實(shí)例形式詳細(xì)分析了數(shù)據(jù)結(jié)構(gòu)與算法中圖的實(shí)現(xiàn)及迭代器相關(guān)算法原理與操作技巧,需要的朋友可以參考下2017-12-12

