python 批量填寫word表格

今天由於老爸下鄉扶貧工做,要填不少word表格,一張表一張表填寫顯然太慢了,就簡單寫了一個python代碼去處理。大致上就是使用docx庫來批量填寫word表格,至於word表格的數據來源是使用xlrd庫從excel表格中讀取出來的。
要填的word表格就是下面這張表,須要填寫的爲高亮部分。
word表格人員信息來自excle表格,以下圖所示:
excel表格每一個人填寫一張word表格,word表格多是因爲單元格合併的緣由,許多cell並非看上去的位置,如要填寫身份證號碼的單元格看起來像是第二行第四列的單元格(cell(1,3)),實際上是cell(1,6)。總之這個word表格的格式很是的亂七八糟,python操做起來很是困難,試了很久才找到每個須要填寫的元素的位置,還有單元格的paragraphs和runs的數量、內容!!!python

import docx
from docx import Document
import xlrd

#document對象、姓名、身份證號、電話、戶籍所在地(村)、文化程度、正在填寫的表格序號
def Wirte2Docx(doc, name, id, phoneNumber, resAddr, cultureLevel, helthLevel, tableNum):
    
    #獲取word文檔中的表格list
    tables = doc.tables
    #肯定要填寫第幾張表 modify
    table = tables[tableNum]

    #獲取要填寫或修改的cell(表格的單元格)
    nameCellRun = table.cell(1,1).paragraphs[0].runs[0]   #run對象能夠看作文本和格式的結合體,修改run.text即僅修改文本,保留原有的格式。
    idCellRun = table.cell(1,6) #沒有run對象 亂七八糟的表格格式!!!!煩死
    phoneCellRun = table.cell(1,20)
    
    #經測試發現這個cell有兩個run:第一個爲省 市 縣(市、區) 鄉鎮(街道) ,第二個爲 村(社區)這部分
    resAddrCellRuns = table.cell(3,1).paragraphs[0].runs
    provenancesRun = resAddrCellRuns[0]
    countryRun = resAddrCellRuns[1]

    #□、博士研究生、□、碩士研究生、□、大學本科生、□、大專生、□、中專中技、 □、高中、□、初中、 □、小學 、□、其餘 每一個元素一個run
    cultureLevelCellRuns = table.cell(5,1).paragraphs[0].runs

    #□健康或良好 □通常或較弱 □有慢性病 □殘疾 每一個元素一個run 共八個run
    healthLevelRuns = table.cell(7,1).paragraphs[0].runs




    #使用run在本來的表格樣式基礎上僅對錶格內容進行修改
    nameCellRun.text = name
    idCellRun.text = id
    phoneCellRun.text = phoneNumber

    provenances = "山西省 忻州市(市、區) 原平市(縣、區) 段家堡村鄉鎮(街道) "
    country = resAddr
    provenancesRun.text = provenances
    countryRun.text = country

    #填寫文化水平
    choseFlag="☑"
    if "博士" in cultureLevel:
        cultureLevelCellRuns[0].text = choseFlag + "博士研究生"
    elif "碩士" in cultureLevel:
        cultureLevelCellRuns[2].text = choseFlag + "碩士研究生"
    elif "本科" in cultureLevel:
        cultureLevelCellRuns[4].text = choseFlag + "大學本科生"
    elif "大專" in cultureLevel:
        cultureLevelCellRuns[6].text = choseFlag + "大專生"
    elif "中專" in cultureLevel or "中職" in cultureLevel:
        cultureLevelCellRuns[8].text = choseFlag
    elif "高中" in cultureLevel:
        cultureLevelCellRuns[11].text = choseFlag
    elif "初中" in cultureLevel:
        cultureLevelCellRuns[14].text = choseFlag
    elif "小學" in cultureLevel:
        cultureLevelCellRuns[17].text = choseFlag
    else:
        cultureLevelCellRuns[20].text = choseFlag
        print("第%d個表格的文化水平格式不規範,須要人工填寫,%s的文化水平爲%s" % (tableNum+1, name, cultureLevel))
    
    #填寫健康情況
    choseFlag="☑"
    if helthLevel == "健康" or helthLevel == "良好":
        healthLevelRuns[0].text = choseFlag + "健康或良好 □通常或較弱 □有慢性病 □殘疾"
    elif "慢性病" in helthLevel:
        healthLevelRuns[0].text = "□健康或良好 □通常或較弱 " + choseFlag + "有慢性病 □殘疾"
    elif "殘疾" in helthLevel:
        healthLevelRuns[0].text = "□健康或良好 □通常或較弱 □有慢性病 " + choseFlag + "殘疾"
    else:
        healthLevelRuns[0].text = "□健康或良好 " + choseFlag + "通常或較弱 □有慢性病 □殘疾"
        
        print("第%d個表格健康水平須要人工填寫,%s的健康水平爲%s" % (tableNum+1, name, helthLevel))


    return


if __name__ == "__main__":

    #讀取xls文件中的數據
    workShop = xlrd.open_workbook("test1.xls")
    sheet = workShop.sheet_by_index(0)

    #打開word文檔(表格未填寫)
    doc = Document("test3.docx")

    for i in range(1077,1097): #青疙瘩村的人員信息位於excel表格的1077-1096行
        row = sheet.row_values(i) #獲取該行的人員信息 放在一個list裏
        Wirte2Docx(doc, row[1], row[2], row[16], "青疙瘩村", row[5], row[7], i-1077)

    #保存填寫後的word文檔
    doc.save("青疙瘩資料.docx")

很是簡單而不優美,可是懶得改了能用就行!web