python学习笔记之docx文档读取

import docx
def getfields(p1):
    '''
    直接读取docx,获取所需要的字段
    姓名	性别	年龄	胶囊编号	检查号	检查医生	送检医生	检查日期	主诉	镜检所见	检查结果
    :return:
    '''
    姓名 = ""
    性别 = ""
    年龄 = ""
    胶囊编号 = ""
    检查号 = ""
    检查医生 = ""
    送检医生 = ""
    检查日期 = ""
    主诉 = ""
    镜检所见 = ""
    检查结果 = ""
    doc = docx.Document(p1)
    # for paragraph in doc.paragraphs:
    #     print(paragraph.text)
    nr = ""
    for table in doc.tables:
        for row in table.rows:
            for cell in row.cells:
                if cell.text.strip() != "":
                    nr = nr + "\n" + cell.text.strip()
    nr = nr.strip()
    # print(nr)
    res = re.search(r"姓名:(.*)", nr, 0)
    if res: 姓名 = res.group(1)
    res = re.search(r"性别:(.*)", nr, 0)
    if res: 性别 = res.group(1)
    res = re.search(r"年龄:(.*)", nr, 0)
    if res: 年龄 = res.group(1)
    res = re.search(r"胶囊编号:(.*)", nr, 0)
    if res: 胶囊编号 = res.group(1)
    res = re.search(r"检查号:(.*)", nr, 0)
    if res: 检查号 = res.group(1)
    res = re.search(r"检查医生:(.*)", nr, 0)
    if res: 检查医生 = res.group(1)
    res = re.search(r"送检医生:(.*)", nr, 0)
    if res: 送检医生 = res.group(1)
    res = re.search(r"检查日期:(.*)", nr, 0)
    if res: 检查日期 = res.group(1)
    res = re.search(r"主诉:(.*)", nr, 0)
    if res: 主诉 = res.group(1)
    res = re.search(r"镜检所见:(.*)检查结果", nr, re.DOTALL)
    if res:
        镜检所见 = res.group(1).strip()
        镜检所见 = 镜检所见.replace(" ", "")
        ls = 镜检所见.split("\n")
        镜检所见 = ";".join(ls)
    res = re.search(r"检查结果:(.*)建    议", nr, re.DOTALL)
    if res:
        检查结果 = res.group(1).strip()
        检查结果 = 检查结果.replace(" ", "")
        ls = 检查结果.split("\n")
        检查结果 = ";".join(ls)
    print("*" * 88)
    ls = [姓名, 性别, 年龄, 胶囊编号, 检查号, 检查医生, 送检医生, 检查日期, 主诉, 镜检所见, 检查结果]
    print(ls)
    if (all(ls)): return ls     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光明有我16620122910

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值