data_verification.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. '''
  2. coding:utf-8
  3. @Software:PyCharm
  4. @Time:2023/2/20 13:17
  5. @Author:zhangnf
  6. @Desc:
  7. '''
  8. from fastapi import FastAPI, UploadFile ,File
  9. from openpyxl import load_workbook
  10. import warnings
  11. from openpyxl.utils.cell import coordinate_from_string
  12. from openpyxl.comments import Comment
  13. from openpyxl.styles import PatternFill
  14. warnings.filterwarnings('ignore')
  15. import uvicorn
  16. app = FastAPI()
  17. from fastapi.middleware.cors import CORSMiddleware
  18. app.add_middleware(
  19. CORSMiddleware,
  20. allow_origins=["*"],
  21. allow_credentials=True,
  22. allow_methods=["*"],
  23. allow_headers=["*"],
  24. )
  25. @app.post("/uploadfile")
  26. async def create_upload_file(file: UploadFile = File(...)):
  27. # print(file.filename)
  28. contents = await file.read()
  29. savename = "/data/download/" + file.filename
  30. #savename = "uploadfile/" + file.filename
  31. with open(savename,"wb" ) as f:
  32. f.write(contents)
  33. # 读取excel表
  34. workbook = load_workbook(savename)
  35. # 获取指定的sheet
  36. sheet_names = workbook.sheetnames
  37. if "脱贫户信息查询" in sheet_names:
  38. sheet = workbook["脱贫户信息查询"]
  39. title_row_num = 0
  40. # 读取前5行,正常应该有字段名了
  41. row_range = sheet[1:5]
  42. for r in row_range:
  43. for c in r:
  44. if ("户主编号" == c.value):
  45. title_row_num = c.row
  46. # 获取字段名对应的列
  47. title_dict = {}
  48. title_rows = sheet[title_row_num]
  49. # 遍历字段名所在行的所有单元格
  50. for title_cell in title_rows:
  51. x, y = coordinate_from_string(title_cell.coordinate)
  52. title_dict[title_cell.value] = x
  53. # print(title_dict)
  54. # 开始读取表格内容
  55. # print(sheet.max_row)
  56. read_data(sheet,title_row_num+1,sheet.max_row,title_dict)
  57. # 保存文档
  58. workbook.save(savename)
  59. return {"code" :200 ,"msg": "分析完成,请点击下载查看分析结果","fileName":file.filename}
  60. else:
  61. print("读取不到指定的sheet页")
  62. return {"code" :500,"msg": "读取不到指定的sheet页--脱贫户信息查询"}
  63. def read_data(ws,start_row,end_row,title_dict):
  64. #监测对象致(返)贫风险非最新设计的风险类型
  65. for i in range(start_row, end_row):
  66. check_poverty_causes(ws,i,title_dict)
  67. check_identitycard_length(ws, i, title_dict)
  68. def check_poverty_causes(ws,row_num,title_dict):
  69. poverty_causes = ws[f"{title_dict['主要致贫原因']}{row_num}"].value
  70. # 致贫原因列表
  71. imageTypeList = ['因病', '因学', '因残', '因自然灾害','因意外事故','因产业项目失败','因务工就业不稳','缺劳动力','因房','因水','其他(填写备注)']
  72. if poverty_causes not in imageTypeList:
  73. ws[f"{title_dict['主要致贫原因']}{row_num}"].comment = Comment(text="21.监测对象致(返)贫风险非最新设计的风险类型", author="system")
  74. yellow_fill = PatternFill(patternType='solid', fgColor='FFFF00')
  75. ws[f"{title_dict['主要致贫原因']}{row_num}"].fill = yellow_fill
  76. def check_identitycard_length(ws,row_num,title_dict):
  77. identitycard = ws[f"{title_dict['户主证件号码']}{row_num}"].value
  78. if len(identitycard) not in [15,18,20,22]:
  79. ws[f"{title_dict['户主证件号码']}{row_num}"].comment = Comment(text="31.监测对象家庭成员证件号码位数异常(证件号码非15、18、20、22位)", author="system")
  80. yellow_fill = PatternFill(patternType='solid', fgColor='FFFF00')
  81. ws[f"{title_dict['户主证件号码']}{row_num}"].fill = yellow_fill
  82. if __name__ == '__main__':
  83. uvicorn.run('data_verification:app',host='localhost',port=8000,reload=True)