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
python学习笔记之docx文档读取
import docxdef getfields(p1):'''直接读取docx,获取所需要的字段姓名 性别 年龄 胶囊编号 检查号 检查医生 送检医生 检查
版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!