data_verification.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 = "/data/download/" + 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 r in row_range:
  38. for c in r:
  39. if "户主编号" == c.value:
  40. title_row_num = c.row
  41. # 获取字段名对应的列
  42. title_dict = {}
  43. title_rows = sheet[title_row_num]
  44. # 遍历字段名所在行的所有单元格
  45. for title_cell in title_rows:
  46. x, y = coordinate_from_string(title_cell.coordinate)
  47. title_dict[title_cell.value] = x
  48. # print(title_dict)
  49. # 开始读取表格内容
  50. # print(sheet.max_row)
  51. read_data(sheet, title_row_num + 1, sheet.max_row, title_dict)
  52. # 保存文档
  53. workbook.save(savename)
  54. return {"code": 200, "msg": "分析完成,请点击下载查看分析结果", "fileName": file.filename}
  55. def read_data(ws, start_row, end_row, title_dict):
  56. # 监测对象致(返)贫风险非最新设计的风险类型
  57. for i in range(start_row, end_row):
  58. check_poverty_causes(ws, i, title_dict)
  59. check_identitycard_length(ws, i, title_dict)
  60. def check_poverty_causes(ws, row_num, title_dict):
  61. poverty_causes = ws[f"{title_dict['主要致贫原因']}{row_num}"].value
  62. # 致贫原因列表
  63. imageTypeList = ["因病", "因学", "因残", "因自然灾害", "因意外事故", "因产业项目失败", "因务工就业不稳", "缺劳动力", "因房", "因水", "其他(填写备注)"]
  64. if poverty_causes not in imageTypeList:
  65. ws[f"{title_dict['主要致贫原因']}{row_num}"].comment = Comment(
  66. text="21.监测对象致(返)贫风险非最新设计的风险类型", author="system"
  67. )
  68. yellow_fill = PatternFill(patternType="solid", fgColor="FFFF00")
  69. ws[f"{title_dict['主要致贫原因']}{row_num}"].fill = yellow_fill
  70. def check_identitycard_length(ws, row_num, title_dict):
  71. identitycard = ws[f"{title_dict['户主证件号码']}{row_num}"].value
  72. if len(identitycard) not in [15, 18, 20, 22]:
  73. ws[f"{title_dict['户主证件号码']}{row_num}"].comment = Comment(
  74. text="31.监测对象家庭成员证件号码位数异常(证件号码非15、18、20、22位)", author="system"
  75. )
  76. yellow_fill = PatternFill(patternType="solid", fgColor="FFFF00")
  77. ws[f"{title_dict['户主证件号码']}{row_num}"].fill = yellow_fill
  78. if __name__ == "__main__":
  79. uvicorn.run("data_verification:app", host="localhost", port=8000, reload=True)