利用Python實現(xiàn)讀取Word表格計算匯總并寫入Excel
前言
快過年了,又到了公司年底評級的時候了。今年的評級和往常一下,每個人都要填寫公司的民主評議表,給各個同事進行評價打分,然后部門收集起來根據(jù)收集上來的評價表進行匯總統(tǒng)計。想想要收集幾十號人的評價表,并根據(jù)每個人的評價表又要填到Excel中進行匯總計算統(tǒng)計給出每個人的評價,就頭大。雖然不是個什么難事,但是是個無腦的細致活。幾十個人的評價也得要花大半天的時間來弄,而且搞多了還容易搞錯。如是就想起干脆用Python寫個小程序自動來處理這些臟活累活,評級年年都要評,每年都可以用。
要做的事情就是讀放到某個文件夾中的word文檔中的評價表格,根據(jù)表格內(nèi)容進行處理,然后匯總所有的表格數(shù)據(jù),根據(jù)計算規(guī)則,算出每個人的評分,在根據(jù)評分計算每個人的評價。匯總后寫入Excel中。

不可否認用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ù)*等級計分分數(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)文章
windows10 pycharm下安裝pyltp庫和加載模型實現(xiàn)語義角色標(biāo)注的示例代碼
這篇文章主要介紹了windows10 pycharm下安裝pyltp庫和加載模型實現(xiàn)語義角色標(biāo)注,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
Python數(shù)據(jù)結(jié)構(gòu)與算法之圖的基本實現(xiàn)及迭代器實例詳解
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之圖的基本實現(xiàn)及迭代器,結(jié)合實例形式詳細分析了數(shù)據(jù)結(jié)構(gòu)與算法中圖的實現(xiàn)及迭代器相關(guān)算法原理與操作技巧,需要的朋友可以參考下2017-12-12

