栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python学习笔记之docx文档读取

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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     
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1026808.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号