import docx
def getfields(p1):
'''
直接读取docx,获取所需要的字段
姓名 性别 年龄 胶囊编号 检查号 检查医生 送检医生 检查日期 主诉 镜检所见 检查结果
:return:
'''
姓名 = ""
性别 = ""
年龄 = ""
胶囊编号 = ""
检查号 = ""
检查医生 = ""
送检医生 = ""
检查日期 = ""
主诉 = ""
镜检所见 = ""
检查结果 = ""
doc = docx.Document(p1)
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()
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