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