data_verification.py 3.8 KB

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