2206 lines
69 KiB
Plaintext
2206 lines
69 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "a3a2e21f-e62e-465b-818d-8acea289434f",
|
||
"metadata": {},
|
||
"source": [
|
||
"# 体质检测"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "04524c85-988e-4dbf-86eb-939a9db7aa28",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 体测人员导入"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "bbba6efc-73cd-4db6-bae7-014724fee731",
|
||
"metadata": {
|
||
"tags": []
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import openpyxl\n",
|
||
"import json\n",
|
||
"\n",
|
||
"\n",
|
||
"wb = openpyxl.load_workbook('data/宁夏能化人员.xlsx',data_only=True)\n",
|
||
"sheet = wb.active\n",
|
||
"# sheets = wb.sheetnames\n",
|
||
"person = {}\n",
|
||
"\n",
|
||
"for n in range(2, sheet.max_row+1):\n",
|
||
" code = int(sheet.cell(n, 2).value)\n",
|
||
" person.setdefault(code, {})\n",
|
||
" dict1 = {}\n",
|
||
" dict1['name'] = sheet.cell(n, 3).value\n",
|
||
" dict1['sex'] = sheet.cell(n, 4).value\n",
|
||
" unit = sheet.cell(n, 9).value\n",
|
||
" \n",
|
||
" dict1['sub_unit'] = sheet.cell(n, 6).value\n",
|
||
" if unit == '管理部门':\n",
|
||
" dict1['unit'] = sheet.cell(n, 6).value\n",
|
||
" else:\n",
|
||
" dict1['unit'] = sheet.cell(n, 9).value\n",
|
||
" \n",
|
||
" dict1['birth'] = str(sheet.cell(n, 5).value).replace('/','-').split(' ')[0] \n",
|
||
" person[code] = dict1\n",
|
||
"filename = 'data/宁夏能化人员-1.json'\n",
|
||
"with open(filename, 'w') as fl:\n",
|
||
" json.dump(person, fl, ensure_ascii=False)\n",
|
||
"print(len(person),'ok')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "ddddbac1-ab71-436a-ab64-2a15600f5b1b",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import openpyxl\n",
|
||
"import json\n",
|
||
"\n",
|
||
"\n",
|
||
"wb = openpyxl.load_workbook('data/宁夏能化-全部名单.xlsx',data_only=True)\n",
|
||
"sheet = wb.active\n",
|
||
"# sheets = wb.sheetnames\n",
|
||
"person = {}\n",
|
||
"\n",
|
||
"for n in range(2, sheet.max_row+1):\n",
|
||
" code = int(sheet.cell(n, 2).value)\n",
|
||
" person.setdefault(code, {})\n",
|
||
" dict1 = {}\n",
|
||
" dict1['name'] = sheet.cell(n, 3).value\n",
|
||
" dict1['sex'] = sheet.cell(n, 4).value\n",
|
||
" unit = sheet.cell(n, 8).value\n",
|
||
" \n",
|
||
" dict1['sub_unit'] = sheet.cell(n, 6).value\n",
|
||
" if unit == '管理部门':\n",
|
||
" dict1['unit'] = sheet.cell(n, 9).value\n",
|
||
" else:\n",
|
||
" dict1['unit'] = sheet.cell(n, 8).value\n",
|
||
" \n",
|
||
" dict1['birth'] = str(sheet.cell(n, 5).value).replace('/','-').split(' ')[0] \n",
|
||
" person[code] = dict1\n",
|
||
"filename = 'data/宁夏能化人员all.json'\n",
|
||
"with open(filename, 'w') as fl:\n",
|
||
" json.dump(person, fl, ensure_ascii=False)\n",
|
||
"print(len(person),'ok')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "048a95aa-1691-45f6-b933-6eaf95ae1d30",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 生成读卡系统文件"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "ce406710-6b7d-4c51-98ec-78883bd3ce5f",
|
||
"metadata": {
|
||
"tags": []
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"list1 = []\n",
|
||
"for k, v in dict1.items():\n",
|
||
" dict2 = {}\n",
|
||
" #if dict1['sex'] =='男':\n",
|
||
" # sex = 1\n",
|
||
" \n",
|
||
" dict2 = {'id':k,'name':v['name'],'gender':v['sex'],'birth':v['birth'],'unit':v['unit']}\n",
|
||
" list1.append(dict2)\n",
|
||
"json_data = json.dumps(list1,ensure_ascii=False, indent=4) \n",
|
||
"\n",
|
||
"# 将 json 数据写入文件\n",
|
||
"with open(\"data/data_宁夏能化人员.json\", \"w\",encoding = 'utf-8') as file:\n",
|
||
" file.write(json_data) \n",
|
||
"print('ok')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "6bcd45c2-10af-4d5f-9e0b-5cd1df4f7a7f",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 获取人员测试成绩"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "970e171e-1360-448f-a28c-520ccb8f314a",
|
||
"metadata": {
|
||
"tags": []
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"import datetime\n",
|
||
"import csv\n",
|
||
"from datetime import date\n",
|
||
"import my_module as My\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化人员all.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl) \n",
|
||
"filename = 'data/marks_20250619.csv'\n",
|
||
"re_ta = My.get_result(filename,dict1)\n",
|
||
"\n",
|
||
"\n",
|
||
"filename = 'data/result_宁夏能化人员all.json'\n",
|
||
"with open(filename,'w') as fl:\n",
|
||
" json.dump(re_ta, fl, ensure_ascii=False) \n",
|
||
"print(len(re_ta))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "24189bf6-d2b2-4855-a6b2-7a6e36cd79f3",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"import datetime\n",
|
||
"import csv\n",
|
||
"from datetime import date\n",
|
||
"import my_module as My\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl) \n",
|
||
"filename = 'data/marks_20250619.csv'\n",
|
||
"re_ta = My.get_result(filename,dict1)\n",
|
||
"\n",
|
||
"\n",
|
||
"filename = 'data/result_宁夏能化人员.json'\n",
|
||
"with open(filename,'w') as fl:\n",
|
||
" json.dump(re_ta, fl, ensure_ascii=False) \n",
|
||
"print(len(re_ta))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "2082c096-9f36-430c-9823-f09d8b6f9b42",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"filename = 'data/result_宁夏能化人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl) \n",
|
||
"\n",
|
||
"filename = 'data/result_宁夏能化人员all.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict2 = json.load(fl) \n",
|
||
"list1 = []\n",
|
||
"for k,v in dict2.items():\n",
|
||
" if k not in dict1.keys():\n",
|
||
" #print(k,v['name'],v['sex'],v['unit'])\n",
|
||
" #print(k,v)\n",
|
||
" list1.append(str(k))\n",
|
||
"print(list1)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "6d381859-6d21-45d2-8313-55ebbabf8bd4",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 生成测试得分"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "0905ad6a-f7e4-43ed-ae29-c4850deb946b",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"import time\n",
|
||
"import my_module as My\n",
|
||
"\n",
|
||
"#list_item = ['lung','grip','flexion','jump','pushup','balance','reaction','step','situp','height','weight']\n",
|
||
"list_item = ['lung','grip','flexion','jump','pushup','balance','reaction','step','situp']\n",
|
||
"filename = 'data/result_宁夏能化人员all.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict2 = json.load(fl) \n",
|
||
"for k, v in dict2.items():\n",
|
||
" #print(k)\n",
|
||
" if v['sex'] == '男':\n",
|
||
" sex = 'M'\n",
|
||
" else:\n",
|
||
" sex = 'F' \n",
|
||
" if 'bmi' in v.keys():\n",
|
||
" #bmi_data = v['height']['成绩'].split()[0]+','+ v['weight']['成绩'].split()[0]\n",
|
||
" bmi_data = v['bmi']['成绩']\n",
|
||
" data1 = {'code':k,'sex':sex,'age':v['age'],'item':'HeightWeight','result':bmi_data}\n",
|
||
" dict2[k]['bmi'] = {}\n",
|
||
" dict2[k]['bmi']['成绩'] = bmi_data\n",
|
||
" dict2[k]['bmi']['score'] = My.cal_bmi(data1)\n",
|
||
" for item_en in list_item:\n",
|
||
" if item_en in v.keys(): \n",
|
||
" data1 = {'code':k,'sex':sex,'age':v['age'],'item':item_en,'result':float(v[item_en]['成绩'].split()[0])}\n",
|
||
" #print(k,v['name'])\n",
|
||
" dict2[k][item_en]['score'] = My.cal_score(data1)\n",
|
||
" #print(k,v[item_en]['成绩'],cal_score(data1))\n",
|
||
"\n",
|
||
"filename = f'data/result_宁夏能化人员all.json'\n",
|
||
"with open(filename,'w') as fl:\n",
|
||
" json.dump(dict2,fl , ensure_ascii=False) \n",
|
||
"print('ok!') "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "d034a61d-1fbd-417b-99bd-277e43ebb678",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 导出测试人员信息"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "867ad6b0-9e9d-48bc-a10a-3b9cb6125890",
|
||
"metadata": {
|
||
"tags": []
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"import openpyxl\n",
|
||
"\n",
|
||
"items = ['lung','grip','flexion','jump','pushup','situp','balance','reaction','step']\n",
|
||
"title = ['编号','姓名','性别','单位','部门','身高','体重','肺活量','握力','坐位体前屈','纵跳','俯卧撑','一分钟仰卧起坐','单脚站立','选择反应时','台阶指数']\n",
|
||
"\n",
|
||
"filename = 'data/result_宁夏能化人员all.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化人员all.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict2 = json.load(fl)\n",
|
||
" \n",
|
||
"list1 = []\n",
|
||
"for k, v in dict1.items():\n",
|
||
" list2 = []\n",
|
||
" list2.append(str(k).rjust(5,'0'))\n",
|
||
" list2.append(v['name']) \n",
|
||
" list2.append(dict2[k]['sex'])\n",
|
||
" list2.append(dict2[k]['unit'])\n",
|
||
" list2.append(dict2[k]['sub_unit'])\n",
|
||
" if 'bmi' in v.keys():\n",
|
||
" height = v['bmi']['成绩'].split(',')[0]\n",
|
||
" weight = v['bmi']['成绩'].split(',')[1]\n",
|
||
" list2.append(height)\n",
|
||
" list2.append(weight)\n",
|
||
" else:\n",
|
||
" list2.append('')\n",
|
||
" list2.append('')\n",
|
||
" for item in items:\n",
|
||
" if item in v.keys():\n",
|
||
" list2.append(v[item]['成绩']) \n",
|
||
" elif item =='name':\n",
|
||
" list2.append(v[item])\n",
|
||
" else:\n",
|
||
" list2.append('')\n",
|
||
" \n",
|
||
" list1.append(list2)\n",
|
||
"filename = 'data/宁夏能化测试情况明细表.xlsx'\n",
|
||
"wb = openpyxl.Workbook()\n",
|
||
"sheet = wb.active\n",
|
||
"sheet.append(title)\n",
|
||
"for row in list1:\n",
|
||
" sheet.append(row)\n",
|
||
" \n",
|
||
"wb.save(filename)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "75b98b19-ab32-414a-b3f2-6efd58e1f9a9",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"import openpyxl\n",
|
||
"\n",
|
||
"items = ['lung','grip','flexion','jump','pushup','situp','balance','reaction','step']\n",
|
||
"title = ['编号','姓名','性别','单位','部门','身高','体重','肺活量','握力','坐位体前屈','纵跳','俯卧撑','一分钟仰卧起坐','单脚站立','选择反应时','台阶指数']\n",
|
||
"\n",
|
||
"filename = 'data/result_宁夏能化人员all.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化人员all.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict2 = json.load(fl)\n",
|
||
" \n",
|
||
"list1 = []\n",
|
||
"for k, v in dict1.items():\n",
|
||
" list2 = []\n",
|
||
" list2.append(str(k).rjust(5,'0'))\n",
|
||
" list2.append(v['name']) \n",
|
||
" list2.append(v['sex'])\n",
|
||
" list2.append(v['unit'])\n",
|
||
" list2.append(v['age'])\n",
|
||
" \n",
|
||
" list1.append(list2)\n",
|
||
"filename = 'data/宁夏能化测试人员年龄情况明细表.xlsx'\n",
|
||
"wb = openpyxl.Workbook()\n",
|
||
"sheet = wb.active\n",
|
||
"sheet.append(title)\n",
|
||
"for row in list1:\n",
|
||
" sheet.append(row)\n",
|
||
" \n",
|
||
"wb.save(filename)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "699c6a40-a6ae-4300-9646-708cb85aa5e8",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 统计各部门测试人员情况"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "c4b71b0f-0f0c-4962-a540-611cbab51bfa",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"\n",
|
||
"filename = 'data/result_宁夏能化人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict2 = json.load(fl)\n",
|
||
"\n",
|
||
"list1 = []\n",
|
||
"for k, v in dict2.items():\n",
|
||
" if k not in dict1.keys():\n",
|
||
" list2 = []\n",
|
||
" unit = v['unit']\n",
|
||
" if unit=='管理部门':\n",
|
||
" sub_unit = v['sub_unit']\n",
|
||
" else:\n",
|
||
" sub_unit = unit\n",
|
||
" list2 = [k,v['name'],v['sex'],sub_unit]\n",
|
||
" list1.append(list2)\n",
|
||
" \n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化未体测人员.xlsx'\n",
|
||
"wb = openpyxl.Workbook()\n",
|
||
"sheet = wb.active\n",
|
||
"sheet.append(title)\n",
|
||
"for row in list1:\n",
|
||
" sheet.append(row)\n",
|
||
" \n",
|
||
"wb.save(filename)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "3796748f-2079-46b6-a3a1-b22441880843",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 统计各部门已测试情况"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "def3f47a-24ef-4815-b63b-2a16b79b4c15",
|
||
"metadata": {
|
||
"tags": []
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"\n",
|
||
"filename = 'data/result_宁夏能化人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict2 = json.load(fl)\n",
|
||
"\n",
|
||
"dict3 = {}\n",
|
||
"\n",
|
||
"\n",
|
||
"title =['单位','体测人数']\n",
|
||
"list1 = []\n",
|
||
"for k,v in dict1.items():\n",
|
||
" unit = v['unit']\n",
|
||
" if unit=='管理部门':\n",
|
||
" sub_unit = dict2[k]['sub_unit']\n",
|
||
" else:\n",
|
||
" sub_unit = unit\n",
|
||
" dict3.setdefault(unit,{}) \n",
|
||
" dict3[unit].setdefault(sub_unit,{})\n",
|
||
" dict3[unit][sub_unit].setdefault('人数',0)\n",
|
||
" dict3[unit][sub_unit]['人数'] = dict3[unit][sub_unit]['人数'] + 1\n",
|
||
"\n",
|
||
"for k, v in dict3.items():\n",
|
||
" list2 = []\n",
|
||
" for k1,v1 in v.items():\n",
|
||
" list2 = [k,k1,v1['人数']]\n",
|
||
" list1.append(list2)\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化部门测试情况表.xlsx'\n",
|
||
"wb = openpyxl.Workbook()\n",
|
||
"sheet = wb.active\n",
|
||
"sheet.append(title)\n",
|
||
"for row in list1:\n",
|
||
" sheet.append(row)\n",
|
||
" \n",
|
||
"wb.save(filename)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "61dde8c3-844a-4016-8952-cb726ab5f61d",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"\n",
|
||
"filename = 'data/result_宁夏能化人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict2 = json.load(fl)\n",
|
||
"\n",
|
||
"dict3 = {}\n",
|
||
"\n",
|
||
"\n",
|
||
"title =['单位','体测人数']\n",
|
||
"list1 = []\n",
|
||
"for k,v in dict2.items():\n",
|
||
" unit = v['unit']\n",
|
||
" if unit=='管理部门':\n",
|
||
" sub_unit = dict2[k]['sub_unit']\n",
|
||
" else:\n",
|
||
" sub_unit = unit\n",
|
||
" dict3.setdefault(unit,{}) \n",
|
||
" dict3[unit].setdefault(sub_unit,{})\n",
|
||
" dict3[unit][sub_unit].setdefault('人数',0)\n",
|
||
" dict3[unit][sub_unit]['人数'] = dict3[unit][sub_unit]['人数'] + 1\n",
|
||
"\n",
|
||
"for k, v in dict3.items():\n",
|
||
" list2 = []\n",
|
||
" for k1,v1 in v.items():\n",
|
||
" list2 = [k,k1,v1['人数']]\n",
|
||
" list1.append(list2)\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化部门人员情况表.xlsx'\n",
|
||
"wb = openpyxl.Workbook()\n",
|
||
"sheet = wb.active\n",
|
||
"sheet.append(title)\n",
|
||
"for row in list1:\n",
|
||
" sheet.append(row)\n",
|
||
" \n",
|
||
"wb.save(filename)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "970a1cc8-4fdf-478a-9f5b-b82d7d4bf762",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 生成体测成绩明细表"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "d31020be-c9c0-404f-8e0b-f8f507385f48",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"import openpyxl\n",
|
||
"\n",
|
||
"items = ['lung','grip','flexion','jump','pushup','situp','balance','reaction','step']\n",
|
||
"bmi = ['height','weight']\n",
|
||
"title = ['编号','姓名','性别','单位/部门','身高','体重','bmi','肺活量','得分','握力','得分','坐位体前屈','得分','纵跳','得分','俯卧撑','得分','一分钟仰卧起坐','得分','单脚站立','得分','选择反应时','得分','台阶指数','得分']\n",
|
||
"\n",
|
||
"filename = 'data/result_宁夏能化人员all.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"\n",
|
||
"list1 = []\n",
|
||
"for k, v in dict1.items(): #print(k,dict2[str(k)]['name'])\n",
|
||
" list2 = []\n",
|
||
" list2.append(str(k).rjust(5,'0'))\n",
|
||
" list2.append(dict1[k]['name']) \n",
|
||
" list2.append(dict1[k]['sex'])\n",
|
||
" list2.append(dict1[k]['unit'])\n",
|
||
" i = 0\n",
|
||
" if 'bmi' in dict1[k].keys():\n",
|
||
" list2.append(dict1[k]['bmi']['成绩'].split(',')[0])\n",
|
||
" list2.append(dict1[k]['bmi']['成绩'].split(',')[1])\n",
|
||
" list2.append(dict1[k]['bmi']['score'])\n",
|
||
" i+=1\n",
|
||
" else:\n",
|
||
" list2.append('') \n",
|
||
" list2.append('') \n",
|
||
" list2.append('') \n",
|
||
" \n",
|
||
" \n",
|
||
" for item in items:\n",
|
||
" if item in dict1[k].keys():\n",
|
||
" list2.append(dict1[k][item]['成绩'])\n",
|
||
" list2.append(dict1[k][item]['score']) \n",
|
||
" i+=1\n",
|
||
" elif item =='name':\n",
|
||
" list2.append(dict1[k][item])\n",
|
||
" else:\n",
|
||
" list2.append('') \n",
|
||
" list2.append('') \n",
|
||
" if i>2:\n",
|
||
" list1.append(list2)\n",
|
||
"filename = 'data/宁夏能化体测情况明细表.xlsx'\n",
|
||
"wb = openpyxl.Workbook()\n",
|
||
"sheet = wb.active\n",
|
||
"sheet.append(title)\n",
|
||
"for row in list1:\n",
|
||
" sheet.append(row)\n",
|
||
" \n",
|
||
"wb.save(filename)\n",
|
||
"print('ok') "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "8d5f4103-0d1e-4711-b324-ece360f8dcd3",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 生成报告"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "23ffd115-72a3-4f90-9e64-ffa6420df8a4",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import requests\n",
|
||
"import json\n",
|
||
"import openpyxl\n",
|
||
"\n",
|
||
"\n",
|
||
"headers = {\n",
|
||
" \"Content-Type\": \"application/json; charset=UTF-8\"\n",
|
||
" }\n",
|
||
"filename = 'data/result_宁夏能化人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"list1 = []\n",
|
||
"file_path ='./宁夏能化人员/'\n",
|
||
"list_item = ['lung','grip','flexion','jump','pushup','balance','reaction','step','situp','bmi']\n",
|
||
"i=0\n",
|
||
"list2 = []\n",
|
||
"for k, v in dict1.items():\n",
|
||
" list1 = []\n",
|
||
" mydata = {}\n",
|
||
" \n",
|
||
" id = str(k).rjust(4,\"0\")\n",
|
||
" mydata['path'] = file_path+id+'-'+ v['name']+'.pdf'\n",
|
||
" mydata['title'] = '中石化宁夏能化公司'\n",
|
||
" mydata['subtitle'] = v['unit']\n",
|
||
" mydata['id'] = id\n",
|
||
" mydata['name'] = v['name']\n",
|
||
" if v['sex'] == '男':\n",
|
||
" mydata['gender'] = 'male'\n",
|
||
" else:\n",
|
||
" mydata['gender'] = 'female'\n",
|
||
" \n",
|
||
" mydata['month'] = v['month']\n",
|
||
" mydata['fits'] = {}\n",
|
||
" survey_list = ['tcm','psy57','spine']\n",
|
||
" for item in survey_list:\n",
|
||
" if item in v.keys():\n",
|
||
" mydata.setdefault('surveys',{})\n",
|
||
" mydata['surveys'][item] = v[item]\n",
|
||
" \n",
|
||
" \n",
|
||
" #mydata['fits'] = {}\n",
|
||
" for item in list_item:\n",
|
||
" if item in v.keys():\n",
|
||
" mydata.setdefault('fits',{})\n",
|
||
" if item in ['lung','pushup','step','situp']:\n",
|
||
" mark = v[item]['成绩'].split()[0].split('.')[0]\n",
|
||
" else:\n",
|
||
" mark = v[item]['成绩'].split()[0]\n",
|
||
" mydata['fits'][item] = {'mark':mark,'score':v[item]['score']}\n",
|
||
" #if len(mydata['fits']) >2 or len(mydata['surveys']) >0:\n",
|
||
" if len(mydata['fits']) >2 : \n",
|
||
" list1.append(mydata)\n",
|
||
" list2.append([k,v['name']])\n",
|
||
" i+=1\n",
|
||
" x = requests.post('http://localhost:3003', data = json.dumps(list1), headers=headers)\n",
|
||
" #print(id,v['name'],x.text)\n",
|
||
" #print(mydata)\n",
|
||
" #x.close()\n",
|
||
"print(i)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "9ffec868-3851-42f6-8431-c5c5d3d15424",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import requests\n",
|
||
"import json\n",
|
||
"import openpyxl\n",
|
||
"\n",
|
||
"\n",
|
||
"headers = {\n",
|
||
" \"Content-Type\": \"application/json; charset=UTF-8\"\n",
|
||
" }\n",
|
||
"filename = 'data/result_宁夏能化人员all.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"list1 = []\n",
|
||
"file_path ='./宁夏能化人员/'\n",
|
||
"list_item = ['lung','grip','flexion','jump','pushup','balance','reaction','step','situp','bmi']\n",
|
||
"i=0\n",
|
||
"list2 = []\n",
|
||
"for k, v in dict1.items():\n",
|
||
" list1 = []\n",
|
||
" mydata = {}\n",
|
||
" if str(k) in ['2018652', '2019665', '2019160', '2019512', '3427477', '2019423', '2019494', '2019901']:\n",
|
||
" \n",
|
||
" id = str(k).rjust(4,\"0\")\n",
|
||
" mydata['path'] = file_path+id+'-'+ v['name']+'.pdf'\n",
|
||
" mydata['title'] = '中石化宁夏能化公司'\n",
|
||
" mydata['subtitle'] = v['unit']\n",
|
||
" mydata['id'] = id\n",
|
||
" mydata['name'] = v['name']\n",
|
||
" if v['sex'] == '男':\n",
|
||
" mydata['gender'] = 'male'\n",
|
||
" else:\n",
|
||
" mydata['gender'] = 'female'\n",
|
||
" \n",
|
||
" mydata['month'] = v['month']\n",
|
||
" mydata['fits'] = {}\n",
|
||
" survey_list = ['tcm','psy57','spine']\n",
|
||
" for item in survey_list:\n",
|
||
" if item in v.keys():\n",
|
||
" mydata.setdefault('surveys',{})\n",
|
||
" mydata['surveys'][item] = v[item]\n",
|
||
" \n",
|
||
" \n",
|
||
" #mydata['fits'] = {}\n",
|
||
" for item in list_item:\n",
|
||
" if item in v.keys():\n",
|
||
" mydata.setdefault('fits',{})\n",
|
||
" if item in ['lung','pushup','step','situp']:\n",
|
||
" mark = v[item]['成绩'].split()[0].split('.')[0]\n",
|
||
" else:\n",
|
||
" mark = v[item]['成绩'].split()[0]\n",
|
||
" mydata['fits'][item] = {'mark':mark,'score':v[item]['score']}\n",
|
||
" #if len(mydata['fits']) >2 or len(mydata['surveys']) >0:\n",
|
||
" if len(mydata['fits']) >2 : \n",
|
||
" list1.append(mydata)\n",
|
||
" list2.append([k,v['name']])\n",
|
||
" i+=1\n",
|
||
" x = requests.post('http://localhost:3003', data = json.dumps(list1), headers=headers)\n",
|
||
" #print(id,v['name'],x.text)\n",
|
||
" #print(mydata)\n",
|
||
" #x.close()\n",
|
||
"print(i)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "a056e973-b5d2-4130-85bf-1b1f21f53e48",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 体测报告按部门分类"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "59f69be9-ab27-4308-8641-927dbcfda87e",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import os,sys,shutil\n",
|
||
"import json\n",
|
||
"import glob\n",
|
||
"from pathlib import Path\n",
|
||
"\n",
|
||
"fi_path = '/home/songyi/pdf-typescript-old2/宁夏能化人员'\n",
|
||
"new_path = 'file/宁夏能化'\n",
|
||
"old = []\n",
|
||
"dict2 = {}\n",
|
||
"\n",
|
||
"filename = 'data/result_宁夏能化人员all.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"fls = glob.glob(f'{fi_path}/*.pdf')\n",
|
||
"print(len(fls))\n",
|
||
"for fn in fls:\n",
|
||
" fi_name =Path(fn).stem.split('-')[0]\n",
|
||
" code = int(fi_name) \n",
|
||
" unit_path = Path(new_path,dict1[str(code)]['unit'])\n",
|
||
" unit_path.mkdir(parents = True, exist_ok = True)\n",
|
||
" n_name = Path(unit_path,Path(fn).stem+'.pdf')\n",
|
||
" #if not os.path.exists(n_name):\n",
|
||
" shutil.copyfile(fn,n_name)\n",
|
||
" #print(fn)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "e4d3abe9-707d-4edd-92df-95a5b0fea941",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 生成体测报告打印明细表"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "896955ac-351a-4914-94a6-56e6d1ae9e6e",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import os,sys,shutil\n",
|
||
"import json\n",
|
||
"import glob\n",
|
||
"from pathlib import Path\n",
|
||
"import openpyxl\n",
|
||
"\n",
|
||
"fi_path = '/home/songyi/pdf-typescript-old2/宁夏能化人员'\n",
|
||
"\n",
|
||
"filename = 'data/result_宁夏能化人员all.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"fls = glob.glob(f'{fi_path}/*.pdf')\n",
|
||
"list1 = []\n",
|
||
"title = ['测试编号','姓名','性别','部门']\n",
|
||
"for fn in fls:\n",
|
||
" list2 = []\n",
|
||
" fi_name =Path(fn).stem.split('-')[0] \n",
|
||
" code = int(fi_name)\n",
|
||
" sex = dict1[str(code)]['sex']\n",
|
||
" unit = dict1[str(code)]['unit']\n",
|
||
" list2 = [fi_name,Path(fn).stem.split('-')[1],sex,unit]\n",
|
||
" \n",
|
||
" list1.append(list2)\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化体测报告明细表.xlsx'\n",
|
||
"wb = openpyxl.Workbook()\n",
|
||
"sheet = wb.active\n",
|
||
"sheet.append(title)\n",
|
||
"for row in list1:\n",
|
||
" sheet.append(row)\n",
|
||
" \n",
|
||
"wb.save(filename) \n",
|
||
"print('ok')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "3e5363cb-233c-4050-9c3a-18207d7f597a",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 统计体质检测等级明细表"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "360831b7-e204-4801-9f72-879e338ed962",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"import openpyxl\n",
|
||
"\n",
|
||
"\n",
|
||
"filename = 'data/data_宁夏能化人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"list1 = []\n",
|
||
"for k, v in dict1.items():\n",
|
||
" list1.append([k,v['name'],v['unit'],v['sex'],v['age'],v['level'],v['score']])\n",
|
||
" \n",
|
||
"filename = 'data/宁夏能化等级情况表.xlsx'\n",
|
||
"wb = openpyxl.Workbook()\n",
|
||
"sheet = wb.active\n",
|
||
"#sheet.append(title)\n",
|
||
"for row in list1:\n",
|
||
" sheet.append(row)\n",
|
||
" \n",
|
||
"wb.save(filename) \n",
|
||
" "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "d9705453-5c8c-40d2-83c5-7c2ddfc5f094",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 导出体检报告阳性数据"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "e9f6d0ce-7be5-4bb9-8a64-9281a99eb897",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import openpyxl\n",
|
||
"import json\n",
|
||
"\n",
|
||
"\n",
|
||
"wb = openpyxl.load_workbook('data/2025年中国石化长城能源化工(宁夏)有限公司体检数据.xlsx',data_only=True)\n",
|
||
"sheet = wb.active\n",
|
||
"# sheets = wb.sheetnames\n",
|
||
"person = {}\n",
|
||
"\n",
|
||
"for n in range(2, sheet.max_row+1):\n",
|
||
" code = int(sheet.cell(n, 1).value)\n",
|
||
" person.setdefault(code, {})\n",
|
||
" dict1 = {}\n",
|
||
" dict1['name'] = sheet.cell(n, 3).value\n",
|
||
" dict1['sex'] = sheet.cell(n, 4).value\n",
|
||
" dict1['unit'] = sheet.cell(n, 2).value\n",
|
||
" dict1['age'] = sheet.cell(n, 5).value\n",
|
||
" content = sheet.cell(n, 6).value\n",
|
||
" dict1['signs'] = content.split(',')\n",
|
||
" person[code] = dict1\n",
|
||
"filename = 'data/宁夏能化体检数据2025.json'\n",
|
||
"with open(filename, 'w') as fl:\n",
|
||
" json.dump(person, fl, ensure_ascii=False)\n",
|
||
"print(len(person),'ok')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "f315bd79-122f-4dd6-9666-d4945fe216cb",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 筛选体重相关异常数据人员"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "e3117237-b921-41fe-9b65-4918c5b2a165",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"import openpyxl\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化体检数据2025.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"list1 = []\n",
|
||
"list2 = ['肥胖','超重','脂肪肝','血脂','高脂血症','动脉粥样硬化']\n",
|
||
"for k, v in dict1.items():\n",
|
||
" zz = set()\n",
|
||
" for item in v['signs']:\n",
|
||
" for xm in list2:\n",
|
||
" if xm in item:\n",
|
||
" zz.add(item)\n",
|
||
" if len(zz) > 0:\n",
|
||
" list1.append([k,v['name'],v['unit'],v['sex'],v['age'],','.join(zz)])\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化体检体重相关人员.xlsx'\n",
|
||
"wb = openpyxl.Workbook()\n",
|
||
"sheet = wb.active\n",
|
||
"#sheet.append(title)\n",
|
||
"for row in list1:\n",
|
||
" sheet.append(row)\n",
|
||
" \n",
|
||
"wb.save(filename) \n",
|
||
" "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "fabb8138-ad15-43a0-abf5-c5ff73318333",
|
||
"metadata": {},
|
||
"source": [
|
||
"# 高危风险干预"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "5b5fcb90-e60e-4edd-929a-702fe93bd0e7",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 导入干预人员名单"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "396e64b8-c306-4613-9848-f0f183052b78",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"import csv\n",
|
||
"import openpyxl\n",
|
||
"import time\n",
|
||
"from datetime import date\n",
|
||
"\n",
|
||
"wb = openpyxl.load_workbook('data/宁夏能化高危风险人群最终干预人员名单.xlsx',data_only=True)\n",
|
||
"sheet = wb.active\n",
|
||
"# sheets = wb.sheetnames\n",
|
||
"person = {}\n",
|
||
"for n in range(2, sheet.max_row+1):\n",
|
||
" code = str(int(sheet.cell(n, 2).value))\n",
|
||
" person.setdefault(code, {})\n",
|
||
" dict1 = {}\n",
|
||
" dict1['name'] = sheet.cell(n, 3).value\n",
|
||
" dict1['sex'] = sheet.cell(n, 4).value\n",
|
||
" dict1['unit'] = sheet.cell(n, 6).value \n",
|
||
" dict1['age'] = int(sheet.cell(n, 5).value)\n",
|
||
" dict1['symptoms'] = sheet.cell(n, 8).value\n",
|
||
" person[code] = dict1\n",
|
||
"filename = 'data/宁夏能化高危风险干预人员.json'\n",
|
||
"with open(filename, 'w') as fl:\n",
|
||
" json.dump(person, fl, ensure_ascii=False)\n",
|
||
"print(len(person),'ok')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "952544fd-0fc6-4365-a43b-a12cab537bb2",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 获取问卷人员信息"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 28,
|
||
"id": "7261e146-03f5-4943-8a3a-479d0e4a025d",
|
||
"metadata": {
|
||
"execution": {
|
||
"iopub.execute_input": "2025-12-09T09:50:57.787016Z",
|
||
"iopub.status.busy": "2025-12-09T09:50:57.786321Z",
|
||
"iopub.status.idle": "2025-12-09T09:50:57.827675Z",
|
||
"shell.execute_reply": "2025-12-09T09:50:57.827125Z",
|
||
"shell.execute_reply.started": "2025-12-09T09:50:57.786958Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"204 ok\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import json\n",
|
||
"import csv\n",
|
||
"import openpyxl\n",
|
||
"import time\n",
|
||
"from datetime import date\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化高危风险干预人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict3 = json.load(fl)\n",
|
||
" \n",
|
||
"dict1 = {}\n",
|
||
"list1 = []\n",
|
||
"filename = 'data/sql_20251209.csv'\n",
|
||
"with open(filename,'r',newline='') as csv_file:\n",
|
||
" fl = csv.reader(csv_file,delimiter=',')\n",
|
||
" header = next(fl) \n",
|
||
" for line in fl:\n",
|
||
" list1.append(line)\n",
|
||
"\n",
|
||
"nn = 0\n",
|
||
"for item in list1:\n",
|
||
" dict2 = {}\n",
|
||
" name = item[2]\n",
|
||
" phone = str(item[3])[2:]\n",
|
||
" birth = str(item[5])\n",
|
||
" content = json.loads(item[1])\n",
|
||
" tcm = []\n",
|
||
" for i in range(0,60):\n",
|
||
" tcm.append(0)\n",
|
||
" for k, v in content.items(): \n",
|
||
" if 'tcm' not in k:\n",
|
||
" dict2[k] = v\n",
|
||
" else:\n",
|
||
" i = int(k[3:])\n",
|
||
" tcm[i-1] = int(v)\n",
|
||
" \n",
|
||
" code = str(int(dict2['code']))\n",
|
||
" if code in dict3.keys():\n",
|
||
" dict1.setdefault(code,{})\n",
|
||
" dict1[code]['name'] = dict2['name']\n",
|
||
" if dict2['gender'] == 'm':\n",
|
||
" dict1[code]['sex'] = '男'\n",
|
||
" else:\n",
|
||
" dict1[code]['sex'] = '女'\n",
|
||
" dict1[code]['birth'] = str(item[4])\n",
|
||
" dict1[code]['unit'] = dict2['unit']\n",
|
||
" dict1[code]['age'] = dict3[code]['age']\n",
|
||
" dict1[code]['phone'] = phone\n",
|
||
" dict1[code]['waist'] = dict2['waist']\n",
|
||
" dict1[code]['hip'] = dict2['hip']\n",
|
||
" dict1[code]['tcm'] = tcm\n",
|
||
"filename = 'data/survey_宁夏能化高危风险干预人员.json'\n",
|
||
"\n",
|
||
"with open(filename,'w') as fl:\n",
|
||
" json.dump(dict1, fl, ensure_ascii=False)\n",
|
||
"print(len(dict1),'ok')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 25,
|
||
"id": "02d82c2a-2319-4c0b-be58-0a1b107fdc35",
|
||
"metadata": {
|
||
"execution": {
|
||
"iopub.execute_input": "2025-12-22T07:38:35.217789Z",
|
||
"iopub.status.busy": "2025-12-22T07:38:35.217210Z",
|
||
"iopub.status.idle": "2025-12-22T07:38:35.238667Z",
|
||
"shell.execute_reply": "2025-12-22T07:38:35.238165Z",
|
||
"shell.execute_reply.started": "2025-12-22T07:38:35.217760Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"111 ok\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import json\n",
|
||
"import csv\n",
|
||
"import openpyxl\n",
|
||
"import time\n",
|
||
"from datetime import date\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化高危风险干预人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict3 = json.load(fl)\n",
|
||
" \n",
|
||
"dict1 = {}\n",
|
||
"list1 = []\n",
|
||
"filename = 'data/sql_20251222.csv'\n",
|
||
"with open(filename,'r',newline='') as csv_file:\n",
|
||
" fl = csv.reader(csv_file,delimiter=',')\n",
|
||
" header = next(fl) \n",
|
||
" for line in fl:\n",
|
||
" list1.append(line)\n",
|
||
"\n",
|
||
"nn = 0\n",
|
||
"for item in list1:\n",
|
||
" dict2 = {}\n",
|
||
" \n",
|
||
" phone = str(item[0])[2:]\n",
|
||
" content = json.loads(json.loads(item[1]))\n",
|
||
" tcm = []\n",
|
||
" for i in range(0,60):\n",
|
||
" tcm.append(0)\n",
|
||
" for k, v in content.items(): \n",
|
||
" if 'tcm' not in k:\n",
|
||
" dict2[k] = v\n",
|
||
" else:\n",
|
||
" i = int(k[3:])\n",
|
||
" tcm[i-1] = int(v)\n",
|
||
" \n",
|
||
" code = str(int(dict2['code']))\n",
|
||
" if code in dict3.keys():\n",
|
||
" dict1[code] = dict3[code]\n",
|
||
" dict1[code]['waist'] = dict2['waist']\n",
|
||
" dict1[code]['hip'] = dict2['hip']\n",
|
||
" dict1[code]['tcm'] = tcm \n",
|
||
" \n",
|
||
"\n",
|
||
"filename = 'data/survey_宁夏能化高危风险干预人员(202512).json'\n",
|
||
"\n",
|
||
"with open(filename,'w') as fl:\n",
|
||
" json.dump(dict1, fl, ensure_ascii=False)\n",
|
||
"print(len(dict1),'ok')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "c4486c37-d1a4-460f-9997-6405c51b805c",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 常用参数及自定义函数"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 26,
|
||
"id": "34cb26d7-cd7b-4ad5-bb7b-71cfcce16c24",
|
||
"metadata": {
|
||
"execution": {
|
||
"iopub.execute_input": "2025-12-22T07:39:25.697010Z",
|
||
"iopub.status.busy": "2025-12-22T07:39:25.696512Z",
|
||
"iopub.status.idle": "2025-12-22T07:39:25.707108Z",
|
||
"shell.execute_reply": "2025-12-22T07:39:25.706358Z",
|
||
"shell.execute_reply.started": "2025-12-22T07:39:25.696961Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"\n",
|
||
"questions = [[1],[-1, 2],[-1, 2],[-1, 8],[-1, 3],[1],[-1],[-1, 7],[2],[2],[2],[2, 3],[2],[2],[3],[3],[3],[3],[3],[4],[4],[4],[4],[4],[4],[4],[4],[5], [5],[5],[5],[5],[5],[5],[5],[6],[6],[6],[6],[6],[6],[7],[7],[7],[7],[7],[7],[8],[8],[8],[8],[8],[8],[9],[9],[9],[9],[9],[9],[9]]\n",
|
||
"\n",
|
||
"kinds = ['平和','气虚','阳虚','阴虚','痰湿','湿热','血瘀','气郁','特禀']\n",
|
||
"\n",
|
||
"def tcm_calc(arr):\n",
|
||
" qa = [8, 8, 7, 8, 8, 6, 7, 7, 7]\n",
|
||
" s = [0] * 9\n",
|
||
" for i in range(len(questions)):\n",
|
||
" m = arr[i] - 1\n",
|
||
" for v in questions[i]:\n",
|
||
" if v < 0:\n",
|
||
" s[-v - 1] += 4 - m\n",
|
||
" else:\n",
|
||
" s[v - 1] += m\n",
|
||
" return [int((v / qa[i]) * 25) for i, v in enumerate(s)]\n",
|
||
"\n",
|
||
"def tcm_kind(score):\n",
|
||
" kind = 0\n",
|
||
" near = False\n",
|
||
" max_kind = 0\n",
|
||
" max_score = 0\n",
|
||
" for i in range(1, 9):\n",
|
||
" if score[i] > max_score:\n",
|
||
" max_kind = i\n",
|
||
" max_score = score[i]\n",
|
||
" if score[0] >= 60 and max_score < 40:\n",
|
||
" if max_score >= 30:\n",
|
||
" near = True\n",
|
||
" kind = max_kind\n",
|
||
" else:\n",
|
||
" kind = max_kind\n",
|
||
" return {\n",
|
||
" \"kind\": kind,\n",
|
||
" \"near\": near\n",
|
||
" }\n",
|
||
"list2 = ['成就感','愉快心境','放松程度','压力应对','体力充沛','情感充沛度']\n",
|
||
"list3 = [[5,7],[7,4],[7,4],[7,4],[8,1],[6,1]] \n",
|
||
"list4 = ['颈椎','胸椎','腰椎','骶尾椎']\n",
|
||
"list5 = [[0,10,10],[10,17,7],[17,24,6],[24,26,2]]\n",
|
||
"qb = [\n",
|
||
" 1, 1, 1, 1, 1,\n",
|
||
" 4, 3, 2, 3, 2, 4, 3,\n",
|
||
" 4, 3, 2, 4, 4, 2, 4,\n",
|
||
" 3, 2, 2, 4, 3, 3, 2,\n",
|
||
" 5, 5, 5, 5, 5, 5, 5, 5,\n",
|
||
" 6, 6, 6, 6, 6, 6\n",
|
||
" ]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "f0bae0b0-2edb-4d3e-a2ae-154d23fea48b",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 导出问卷监测情况表"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 27,
|
||
"id": "d78c0f3f-cd1a-4c6c-9f5b-b1b874a95fe3",
|
||
"metadata": {
|
||
"execution": {
|
||
"iopub.execute_input": "2025-12-22T07:40:30.226248Z",
|
||
"iopub.status.busy": "2025-12-22T07:40:30.225697Z",
|
||
"iopub.status.idle": "2025-12-22T07:40:30.260291Z",
|
||
"shell.execute_reply": "2025-12-22T07:40:30.259853Z",
|
||
"shell.execute_reply.started": "2025-12-22T07:40:30.226194Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import openpyxl\n",
|
||
"\n",
|
||
"\n",
|
||
"filename = 'data/survey_宁夏能化高危风险干预人员(202512).json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl) \n",
|
||
"data_list = []\n",
|
||
"for k, v in dict1.items():\n",
|
||
" list6 = []\n",
|
||
" list6.append(str(k).rjust(4,'0'))\n",
|
||
" list6.append(v['name'])\n",
|
||
" list6.append(v['sex'])\n",
|
||
" list6.append(v['unit'])\n",
|
||
" list6.append(v['age'])\n",
|
||
" list6.append(v['waist'])\n",
|
||
" list6.append(v['hip'])\n",
|
||
" \n",
|
||
" if 'tcm' in v.keys():\n",
|
||
" list1 = []\n",
|
||
" tcm =v['tcm']\n",
|
||
" for item in tcm:\n",
|
||
" list1.append(item)\n",
|
||
" score = tcm_calc(list1)\n",
|
||
"\n",
|
||
" result = tcm_kind(score)\n",
|
||
" kind = result['kind']\n",
|
||
" near = result['near']\n",
|
||
" #print(k,kinds[kind], near, score)\n",
|
||
" list6.append(kinds[kind])\n",
|
||
" if near:\n",
|
||
" list6.append('是')\n",
|
||
" else:\n",
|
||
" list6.append('')\n",
|
||
" for item in score:\n",
|
||
" list6.append(item)\n",
|
||
" else:\n",
|
||
" for i in range(0,11):\n",
|
||
" list6.append('')\n",
|
||
" i+=1\n",
|
||
" \n",
|
||
" data_list.append(list6)\n",
|
||
"filename = 'data/宁夏能化高危风险干预人员体质问卷明细表(20251222).xlsx'\n",
|
||
"wb = openpyxl.Workbook()\n",
|
||
"sheet = wb.active\n",
|
||
"title = ['编号', '姓名', '性别', '单位/部门', '年龄', '腰围', '臀围', '中医体质', '是否倾向', '平和', '气虚', '阳虚', '阴虚', '痰湿', '湿热', '血瘀', '气郁', '特禀']\n",
|
||
"sheet.append(title)\n",
|
||
"for row in data_list:\n",
|
||
" sheet.append(row)\n",
|
||
" \n",
|
||
"wb.save(filename) "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "93a9b31e-49ea-45cd-bb0b-5c3db19bbb54",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 导出问卷人员信息"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "f0853629-e971-492b-88c8-4e9521b4b017",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"import openpyxl\n",
|
||
"\n",
|
||
"\n",
|
||
"title = ['员工编号','姓名','性别','部门']\n",
|
||
"\n",
|
||
"filename = 'data/survey_宁夏能化干预人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"\n",
|
||
"filename = 'data/宁夏能化干预人员.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict2 = json.load(fl)\n",
|
||
" \n",
|
||
"list1 = []\n",
|
||
"for k, v in dict2.items():\n",
|
||
" \n",
|
||
" if str(k) not in dict1.keys():\n",
|
||
" list2 = []\n",
|
||
" list2 = [k,v['name'],v['sex'],v['unit']] \n",
|
||
" list1.append(list2)\n",
|
||
"filename = 'data/宁夏能化干预人员未参加问卷人员(20251010).xlsx'\n",
|
||
"wb = openpyxl.Workbook()\n",
|
||
"sheet = wb.active\n",
|
||
"sheet.append(title)\n",
|
||
"for row in list1:\n",
|
||
" sheet.append(row)\n",
|
||
" \n",
|
||
"wb.save(filename)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "4b0cd38c-d14c-48e7-83cb-8afc7665b060",
|
||
"metadata": {},
|
||
"source": [
|
||
"# 体重管理系统"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "98de56de-95fe-4f21-be25-896fb1bda175",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 问卷转换"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 15,
|
||
"id": "22be8f39-afbb-448a-8ae6-c825c7d38757",
|
||
"metadata": {
|
||
"execution": {
|
||
"iopub.execute_input": "2025-12-22T03:43:54.245786Z",
|
||
"iopub.status.busy": "2025-12-22T03:43:54.245059Z",
|
||
"iopub.status.idle": "2025-12-22T03:43:54.418251Z",
|
||
"shell.execute_reply": "2025-12-22T03:43:54.417770Z",
|
||
"shell.execute_reply.started": "2025-12-22T03:43:54.245721Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"import openpyxl\n",
|
||
"\n",
|
||
"\n",
|
||
"title = []\n",
|
||
"\n",
|
||
"filename = 'data/surveys_records_2025-12-22.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"list1 = []\n",
|
||
"title = ['id', 'date_created']\n",
|
||
"data_title = ['name', 'gender', 'birth', 'code', 'unit', 'height', 'weight', 'next_weight', 'waist', 'hip', 'level4', 'level3', 'level2', 'recipe', 'level1', 'last_level4', 'last_level3', 'last_level2', 'last_level1', 'last_recipe', 'last_lose', 'last_sport', 'sport_type', 'sport_duration', 'last_sport_time', 'last_lose-Comment', 'last_recipe-Comment', 'last_lose_weight', 'last_sport-Comment', 'sport_type-Comment']\n",
|
||
"for item in dict1:\n",
|
||
" list2 = []\n",
|
||
" for xm in title:\n",
|
||
" if xm in item.keys():\n",
|
||
" list2.append(item[xm])\n",
|
||
" else:\n",
|
||
" list2.append('')\n",
|
||
" for xm in data_title:\n",
|
||
" data = json.loads(item['data'])\n",
|
||
" if xm in data.keys():\n",
|
||
" if xm =='sport_type':\n",
|
||
" list2.append(','.join(data[xm]))\n",
|
||
" else:\n",
|
||
" list2.append(data[xm])\n",
|
||
" else:\n",
|
||
" list2.append('')\n",
|
||
" \n",
|
||
" list1.append(list2)\n",
|
||
"all_title = ['id', 'date_created','name', 'gender', 'birth', 'code', 'unit', 'height', 'weight', 'next_weight', 'waist', 'hip', 'level4', 'level3', 'level2', 'recipe', 'level1', 'last_level4', 'last_level3', 'last_level2', 'last_level1', 'last_recipe', 'last_lose', 'last_sport', 'sport_type', 'sport_duration', 'last_sport_time', 'last_lose-Comment', 'last_recipe-Comment', 'last_lose_weight', 'last_sport-Comment', 'sport_type-Comment']\n",
|
||
"filename = 'data/宁夏能化干预人员问卷(20251222).xlsx'\n",
|
||
"wb = openpyxl.Workbook()\n",
|
||
"sheet = wb.active\n",
|
||
"sheet.append(all_title)\n",
|
||
"for row in list1:\n",
|
||
" sheet.append(row)\n",
|
||
" \n",
|
||
"wb.save(filename) "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "ad2f24f2-f43c-4181-a5e4-16876b0d7443",
|
||
"metadata": {
|
||
"execution": {
|
||
"iopub.execute_input": "2025-11-14T09:41:32.283676Z",
|
||
"iopub.status.busy": "2025-11-14T09:41:32.283096Z",
|
||
"iopub.status.idle": "2025-11-14T09:41:32.303376Z",
|
||
"shell.execute_reply": "2025-11-14T09:41:32.302637Z",
|
||
"shell.execute_reply.started": "2025-11-14T09:41:32.283623Z"
|
||
}
|
||
},
|
||
"source": [
|
||
"## 问卷导入数据库"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "d266379b-4797-4f60-a83d-dc2349a48f0b",
|
||
"metadata": {},
|
||
"source": [
|
||
"### 核对新增人员"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 16,
|
||
"id": "91c53866-3725-4ceb-b0eb-23fb14e20561",
|
||
"metadata": {
|
||
"execution": {
|
||
"iopub.execute_input": "2025-12-22T03:44:05.908310Z",
|
||
"iopub.status.busy": "2025-12-22T03:44:05.907768Z",
|
||
"iopub.status.idle": "2025-12-22T03:44:06.062310Z",
|
||
"shell.execute_reply": "2025-12-22T03:44:06.061610Z",
|
||
"shell.execute_reply.started": "2025-12-22T03:44:05.908261Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"('汪晖', '男', '销售中心', '1971-02-01', '02019646', 1)\n",
|
||
"('马燕金', '男', '销售中心', '1983-04-01', '02018528', 1)\n",
|
||
"('孙皓', '男', '销售中心', '1990-10-01', '02019111', 1)\n",
|
||
"('李润良', '男', '环保建材运行部', '1992-06-01', '03427471', 1)\n",
|
||
"('王春光', '男', '环保建材运行部', '1969-12-01', '02020528', 1)\n",
|
||
"('户利平', '男', '环保建材运行部', '1969-08-01', '02020512', 1)\n",
|
||
"('马婉婷', '女', '公用工程运行部', '2001-11-01', '03561578', 1)\n",
|
||
"('储方伟', '男', '环保建材运行部', '1976-01-01', '02020483', 1)\n",
|
||
"('马斯勇', '男', '环保建材运行部', '1987-08-01', '02020525', 1)\n",
|
||
"('康磊', '男', '公用工程运行部', '1987-07-01', '02020289', 1)\n",
|
||
"('王涛', '男', '环保建材运行部', '1982-07-01', '00000521', 1)\n",
|
||
"('杨德忠', '男', '公用工程运行部', '1972-11-01', '02020306', 1)\n",
|
||
"('杨思国', '男', '电气仪表中心', '1981-04-01', '02018877', 1)\n",
|
||
"('党金广', '男', '电气仪表中心', '1987-08-01', '02018839', 1)\n",
|
||
"('张锋', '男', '电气仪表中心', '1971-05-01', '02020003', 1)\n",
|
||
"('张磊', '男', '电气仪表中心', '1989-12-01', '02018949', 1)\n",
|
||
"('秦国振', '男', '电气仪表中心', '1997-07-01', '03462294', 1)\n",
|
||
"('陈龙', '男', '电气仪表中心', '1981-05-01', '02018914', 1)\n",
|
||
"('朱岳峰', '男', '电气仪表中心', '1988-02-01', '02018865', 1)\n",
|
||
"19\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import json\n",
|
||
"import openpyxl\n",
|
||
"import psycopg2\n",
|
||
"\n",
|
||
"conn = psycopg2.connect(\n",
|
||
" host=\"localhost\",\n",
|
||
" database=\"tice\",\n",
|
||
" user=\"mydata\",\n",
|
||
" password=\"songyi\"\n",
|
||
")\n",
|
||
"cur = conn.cursor()\n",
|
||
"wb = openpyxl.load_workbook('data/宁夏能化干预人员问卷(20251222).xlsx',data_only=True)\n",
|
||
"sheet = wb.active\n",
|
||
"# sheets = wb.sheetnames\n",
|
||
"org_id = 1\n",
|
||
"event_id = 1\n",
|
||
"sql = \"SELECT code from fitness_person where org_id = %s\"\n",
|
||
"params = (org_id,)\n",
|
||
"cur.execute(sql, params)\n",
|
||
"rows = cur.fetchall()\n",
|
||
"codes = []\n",
|
||
"for row in rows:\n",
|
||
" codes.append(row[0])\n",
|
||
"\n",
|
||
"person = {}\n",
|
||
"for n in range(2, sheet.max_row+1):\n",
|
||
" code = str(sheet.cell(n, 6).value)\n",
|
||
" if code not in codes:\n",
|
||
" person.setdefault(code, {})\n",
|
||
" dict1 = {}\n",
|
||
" dict1['name'] = sheet.cell(n, 3).value\n",
|
||
" sex = sheet.cell(n, 4).value\n",
|
||
" if sex =='male':\n",
|
||
" dict1['sex'] = '男'\n",
|
||
" else:\n",
|
||
" dict1['sex'] = '女'\n",
|
||
" birth = str(sheet.cell(n, 5).value).split()[0]\n",
|
||
" \n",
|
||
" if len(birth)<10:\n",
|
||
" nian = birth.split('-')[0]\n",
|
||
" yue = birth.split('-')[1].rjust(2,'0')\n",
|
||
" if len(birth.split('-')[0]) ==3:\n",
|
||
" ri = birth.split('-')[2].rjust(2,'0')\n",
|
||
" else:\n",
|
||
" ri = '01' \n",
|
||
" dict1['birth'] = nian+'-'+ yue + '-'+ ri\n",
|
||
" else:\n",
|
||
" dict1['birth'] = birth\n",
|
||
" dict1['unit'] = sheet.cell(n, 7).value\n",
|
||
" person[code] = dict1\n",
|
||
"org_id = 1\n",
|
||
"data1 = []\n",
|
||
"for k,v in person.items():\n",
|
||
" tuple1 = (v['name'],v['sex'],v['unit'],v['birth'],k,org_id)\n",
|
||
" print(tuple1)\n",
|
||
" data1.append(tuple1)\n",
|
||
"print(len(data1))\n",
|
||
"sql = \"INSERT INTO fitness_person ( name, sex,unit,birth,code,org_id) VALUES (%s, %s, %s,%s, %s, %s)\" \n",
|
||
"#print(data1)\n",
|
||
"cur.executemany(sql, data1)\n",
|
||
"conn.commit()\n",
|
||
"\n",
|
||
"# 关闭连接\n",
|
||
"cur.close()\n",
|
||
"conn.close()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "5c2a37a0-18d3-4c89-8dd9-283a417b5c60",
|
||
"metadata": {},
|
||
"source": [
|
||
"### 导入问卷信息(已作废)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "0b386dac-a335-41ba-8838-59b2866bb9a3",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"import openpyxl\n",
|
||
"import psycopg2\n",
|
||
"\n",
|
||
"conn = psycopg2.connect(\n",
|
||
" host=\"localhost\",\n",
|
||
" database=\"tice\",\n",
|
||
" user=\"mydata\",\n",
|
||
" password=\"songyi\"\n",
|
||
")\n",
|
||
"cur = conn.cursor()\n",
|
||
"\n",
|
||
"org_id = 1\n",
|
||
"event_id = 1\n",
|
||
"filename = 'data/surveys_records_宁夏能化.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"list1 = []\n",
|
||
"title = ['date_created', 'height', 'weight', 'next_weight', 'waist', 'hip', 'level4', 'level3', 'level2', 'recipe', 'level1', 'last_level4', 'last_level3', 'last_level2', 'last_level1', 'last_recipe', 'last_lose', 'last_sport', 'sport_type', 'sport_duration', 'last_sport_time', 'last_lose_Comment', 'last_recipe_Comment', 'last_lose_weight', 'last_sport_Comment', 'sport_type_Comment']\n",
|
||
"data_title = ['height', 'weight', 'next_weight', 'waist', 'hip', 'level4', 'level3', 'level2', 'recipe', 'level1', 'last_level4', 'last_level3', 'last_level2', 'last_level1', 'last_recipe', 'last_lose', 'last_sport', 'sport_type', 'sport_duration', 'last_sport_time', 'last_lose-Comment', 'last_recipe-Comment', 'last_lose_weight', 'last_sport-Comment', 'sport_type-Comment']\n",
|
||
"for item in dict1:\n",
|
||
" \n",
|
||
" code = item['data']['code']\n",
|
||
" sql = \"SELECT id from fitness_person where code = %s and org_id = %s\"\n",
|
||
" params = (code,org_id)\n",
|
||
" cur.execute(sql, params)\n",
|
||
" rows = cur.fetchone()\n",
|
||
" if rows:\n",
|
||
" list2 = []\n",
|
||
" id = rows[0]\n",
|
||
" list2.append(event_id)\n",
|
||
" list2.append(id)\n",
|
||
" date_created = item['date_created']\n",
|
||
" list2.append(date_created)\n",
|
||
" for xm in data_title:\n",
|
||
" if xm in item['data'].keys():\n",
|
||
" if xm =='sport_type':\n",
|
||
" list2.append(','.join(item['data'][xm]))\n",
|
||
" else:\n",
|
||
" list2.append(item['data'][xm])\n",
|
||
" else:\n",
|
||
" list2.append('')\n",
|
||
" list1.append(tuple(list2))\n",
|
||
"sql = \"INSERT INTO fitness_survey_data (event_id,user_id,date_created, height, weight, next_weight, waist, hip, level4, level3, level2, recipe, level1, last_level4, last_level3, last_level2, last_level1, last_recipe, last_lose, last_sport, sport_type, sport_duration, last_sport_time, last_lose_Comment, last_recipe_Comment, last_lose_weight, last_sport_Comment, sport_type_Comment)\" \n",
|
||
"sql = sql + \" VALUES (%s,%s, %s, %s,%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s, %s, %s, %s)\"\n",
|
||
"cur.executemany(sql, list1)\n",
|
||
"conn.commit()\n",
|
||
"\n",
|
||
"# 关闭连接\n",
|
||
"cur.close()\n",
|
||
"conn.close()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "83a2518b-12d8-4ef8-8ee9-5b4289a94208",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 问卷导入JSON"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 20,
|
||
"id": "b7133988-213c-4b52-8d6b-17b56b8ea310",
|
||
"metadata": {
|
||
"execution": {
|
||
"iopub.execute_input": "2025-12-22T03:49:22.646173Z",
|
||
"iopub.status.busy": "2025-12-22T03:49:22.645514Z",
|
||
"iopub.status.idle": "2025-12-22T03:49:22.842656Z",
|
||
"shell.execute_reply": "2025-12-22T03:49:22.842110Z",
|
||
"shell.execute_reply.started": "2025-12-22T03:49:22.646116Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"02019646\n",
|
||
"2018434\n",
|
||
"02018586\n",
|
||
"02018444\n",
|
||
"02020443\n",
|
||
"02020415\n",
|
||
"02018446\n",
|
||
"2018447\n",
|
||
"03533284\n",
|
||
"02020243\n",
|
||
"02019374\n",
|
||
"02020238\n",
|
||
"02019952\n",
|
||
"03497967\n",
|
||
"02018709\n",
|
||
"02019342\n",
|
||
"02019620\n",
|
||
"02020203\n",
|
||
"02018528\n",
|
||
"02019111\n",
|
||
"02018482\n",
|
||
"02018481\n",
|
||
"02018628\n",
|
||
"02018498\n",
|
||
"02018499\n",
|
||
"964089\n",
|
||
"0214545\n",
|
||
"02020481\n",
|
||
"2018609\n",
|
||
"01348929\n",
|
||
"02020526\n",
|
||
"03427471\n",
|
||
"02020196\n",
|
||
"02020528\n",
|
||
"2019094\n",
|
||
"02020512\n",
|
||
"02020476\n",
|
||
"03561578\n",
|
||
"0003533364\n",
|
||
"02019294\n",
|
||
"02020483\n",
|
||
"02020527\n",
|
||
"02020467\n",
|
||
"02020296\n",
|
||
"02020525\n",
|
||
"02020289\n",
|
||
"03434760\n",
|
||
"02018435\n",
|
||
"2019094\n",
|
||
"02019688\n",
|
||
"02019688\n",
|
||
"03561564\n",
|
||
"02020346\n",
|
||
"02020458\n",
|
||
"00000521\n",
|
||
"02020306\n",
|
||
"02019655\n",
|
||
"02020189\n",
|
||
"02019787\n",
|
||
"02019787\n",
|
||
"02018611\n",
|
||
"02018566\n",
|
||
"02018645\n",
|
||
"02019485\n",
|
||
"02020127\n",
|
||
"02019054\n",
|
||
"2019006\n",
|
||
"028415\n",
|
||
"03462271\n",
|
||
"02028323\n",
|
||
"02020492\n",
|
||
"02018794\n",
|
||
"02018877\n",
|
||
"02018926\n",
|
||
"02018809\n",
|
||
"02018938\n",
|
||
"02020083\n",
|
||
"02018839\n",
|
||
"2018985\n",
|
||
"02019866\n",
|
||
"02018793\n",
|
||
"03428863\n",
|
||
"02019017\n",
|
||
"02018981\n",
|
||
"02020003\n",
|
||
"03462285\n",
|
||
"02018626\n",
|
||
"02018604\n",
|
||
"02018892\n",
|
||
"02018827\n",
|
||
"02018949\n",
|
||
"02019599\n",
|
||
"02019622\n",
|
||
"02018575\n",
|
||
"2018438\n",
|
||
"02019613\n",
|
||
"03363370\n",
|
||
"01702447\n",
|
||
"020186\n",
|
||
"02018709\n",
|
||
"03497967\n",
|
||
"0201968\n",
|
||
"02019641\n",
|
||
"02018709\n",
|
||
"02019697\n",
|
||
"02019021\n",
|
||
"0435\n",
|
||
"02018685\n",
|
||
"02018772\n",
|
||
"02019620\n",
|
||
"03497960\n",
|
||
"02019888\n",
|
||
"02019682\n",
|
||
"03391642\n",
|
||
"03462294\n",
|
||
"02018914\n",
|
||
"2019961\n",
|
||
"02018859\n",
|
||
"02019377\n",
|
||
"02017961\n",
|
||
"02018607\n",
|
||
"02018865\n",
|
||
"02020277\n",
|
||
"02020251\n",
|
||
"02020321\n",
|
||
"03391661\n",
|
||
"02020276\n",
|
||
"02019062\n",
|
||
"02019887\n",
|
||
"03148213\n",
|
||
"2019822\n",
|
||
"02019840\n",
|
||
"02019790\n",
|
||
"02019819\n",
|
||
"02019754\n",
|
||
"03437945\n",
|
||
"02019749\n",
|
||
"02019111\n",
|
||
"03418292\n",
|
||
"02019764\n",
|
||
"02019744\n",
|
||
"02019734\n",
|
||
"02019374\n",
|
||
"02019778\n",
|
||
"02019788\n",
|
||
"02019725\n",
|
||
"02017961\n",
|
||
"02019751\n",
|
||
"02020282\n",
|
||
"02019856\n",
|
||
"02019832\n",
|
||
"02019848\n",
|
||
"03437965\n",
|
||
"02019954\n",
|
||
"02020052\n",
|
||
"02019820\n",
|
||
"02019893\n",
|
||
"02019752\n",
|
||
"02019784\n",
|
||
"02020343\n",
|
||
"02019810\n",
|
||
"2019735\n",
|
||
"02019023\n",
|
||
"02020327\n",
|
||
"02019852\n",
|
||
"02018538\n",
|
||
"02018977\n",
|
||
"2018942\n",
|
||
"02019898\n",
|
||
"02019794\n",
|
||
"02019805\n",
|
||
"02019342\n",
|
||
"02019895\n",
|
||
"02019154\n",
|
||
"02019775\n",
|
||
"3391687\n",
|
||
"02019917\n",
|
||
"02018754\n",
|
||
"03418302\n",
|
||
"02019738\n",
|
||
"03462274\n",
|
||
"03561561\n",
|
||
"02019783\n",
|
||
"02018967\n",
|
||
"2019324\n",
|
||
"02019355\n",
|
||
"02019003\n",
|
||
"02019377\n",
|
||
"02019803\n",
|
||
"02019311\n",
|
||
"02019346\n",
|
||
"02019525\n",
|
||
"02020502\n",
|
||
"03497957\n",
|
||
"02020242\n",
|
||
"02019347\n",
|
||
"3418215\n",
|
||
"02019340\n",
|
||
"02019773\n",
|
||
"0219181\n",
|
||
"02019336\n",
|
||
"02019340\n",
|
||
"02018999\n",
|
||
"02019357\n",
|
||
"02019100\n",
|
||
"02019430\n",
|
||
"02019356\n",
|
||
"03418230\n",
|
||
"03437952\n",
|
||
"03437952\n",
|
||
"02019231\n",
|
||
"03497937\n",
|
||
"03497917\n",
|
||
"02019318\n",
|
||
"03462262\n",
|
||
"02019110\n",
|
||
"2019919\n",
|
||
"02019382\n",
|
||
"03497562\n",
|
||
"02019369\n",
|
||
"03391686\n",
|
||
"02019736\n",
|
||
"02019786\n",
|
||
"02019411\n",
|
||
"3391674\n",
|
||
"02018727\n",
|
||
"02019035\n",
|
||
"02019350\n",
|
||
"02019414\n",
|
||
"2019353\n",
|
||
"2019353\n",
|
||
"2019493\n",
|
||
"02019903\n",
|
||
"02019825\n",
|
||
"02019802\n",
|
||
"03394941\n",
|
||
"02020243\n",
|
||
"2019914\n",
|
||
"02019164\n",
|
||
"02019467\n",
|
||
"03437937\n",
|
||
"02019366\n",
|
||
"02019344\n",
|
||
"2019930\n",
|
||
"02019910\n",
|
||
"02019647\n",
|
||
"02019843\n",
|
||
"01825596\n",
|
||
"02019882\n",
|
||
"03409694\n",
|
||
"02019391\n",
|
||
"02019389\n",
|
||
"02019301\n",
|
||
"02020005\n",
|
||
"02019920\n",
|
||
"02019498\n",
|
||
"02019373\n",
|
||
"02019733\n",
|
||
"2019998\n",
|
||
"02020000\n",
|
||
"02019401\n",
|
||
"02019901\n",
|
||
"03418242\n",
|
||
"02019383\n",
|
||
"02019438\n",
|
||
"02019871\n",
|
||
"02019193\n",
|
||
"02019635\n",
|
||
"02020203\n",
|
||
"02019098\n",
|
||
"02019459\n",
|
||
"02018996\n",
|
||
"02019499\n",
|
||
"02019687\n",
|
||
"02018674\n",
|
||
"02019059\n",
|
||
"02019059\n",
|
||
"2019845\n",
|
||
"2018609\n",
|
||
"02019379\n",
|
||
"02020006\n",
|
||
"2019923\n",
|
||
"02019774\n",
|
||
"02019068\n",
|
||
"020294\n",
|
||
"03403228\n",
|
||
"3019337\n",
|
||
"02018711\n",
|
||
"03462315\n",
|
||
"02019739\n",
|
||
"02019278\n",
|
||
"02019416\n",
|
||
"2018973\n",
|
||
"02019256\n",
|
||
"02019878\n",
|
||
"02019664\n",
|
||
"02019404\n",
|
||
"02019058\n",
|
||
"02019504\n",
|
||
"02019361\n",
|
||
"02019600\n",
|
||
"02019381\n",
|
||
"02019248\n",
|
||
"02019543\n",
|
||
"02019648\n",
|
||
"02020260\n",
|
||
"287 ok\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import json\n",
|
||
"import openpyxl\n",
|
||
"\n",
|
||
"wb = openpyxl.load_workbook('data/宁夏能化干预人员问卷(20251222).xlsx',data_only=True)\n",
|
||
"sheet = wb.active\n",
|
||
"# sheets = wb.sheetnames\n",
|
||
"person = {}\n",
|
||
"for n in range(2, sheet.max_row+1):\n",
|
||
" code = str(sheet.cell(n, 6).value)\n",
|
||
" print(code)\n",
|
||
" person.setdefault(code, {})\n",
|
||
" dict1 = {}\n",
|
||
" dict1['name'] = sheet.cell(n, 3).value\n",
|
||
" dict1['sex'] = sheet.cell(n, 4).value\n",
|
||
" birth = str(sheet.cell(n, 5).value).split()[0]\n",
|
||
" if len(birth)<10:\n",
|
||
" nian = birth.replace('/','-').split('-')[0]\n",
|
||
" yue = birth.replace('/','-').split('-')[1].rjust(2,'0')\n",
|
||
" if len(birth.replace('/','-').split('-')[0]) ==3:\n",
|
||
" ri = birth.replace('/','-').split('-')[2].rjust(2,'0')\n",
|
||
" else:\n",
|
||
" ri = '01' \n",
|
||
" dict1['birth'] = nian+'-'+ yue + '-'+ ri\n",
|
||
" dict1['unit'] = sheet.cell(n, 7).value\n",
|
||
" person[code] = dict1\n",
|
||
"#print(person)\n",
|
||
"\n",
|
||
"filename = 'data/surveys_records_2025-12-22.json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"list1 = []\n",
|
||
"set1 = set()\n",
|
||
"title = ['date_created', 'height', 'weight', 'next_weight', 'waist', 'hip', 'level4', 'level3', 'level2', 'recipe', 'level1', 'last_level4', 'last_level3', 'last_level2', 'last_level1', 'last_recipe', 'last_lose', 'last_sport', 'sport_type', 'sport_duration', 'last_sport_time', 'last_lose_Comment', 'last_recipe_Comment', 'last_lose_weight', 'last_sport_Comment', 'sport_type_Comment']\n",
|
||
"data_title = ['height', 'weight', 'next_weight', 'waist', 'hip', 'level4', 'level3', 'level2', 'recipe', 'level1', 'last_level4', 'last_level3', 'last_level2', 'last_level1', 'last_recipe', 'last_lose', 'last_sport', 'sport_type', 'sport_duration', 'last_sport_time', 'last_lose-Comment', 'last_recipe-Comment', 'last_lose_weight', 'last_sport-Comment', 'sport_type-Comment']\n",
|
||
"for item in dict1: \n",
|
||
" dict2 = {}\n",
|
||
" data = json.loads(item['data'])\n",
|
||
" code = data['code']\n",
|
||
" rq = item['date_created'].split()[0]\n",
|
||
" dict2['height'] = str('%.2f' % data['height'])\n",
|
||
" dict2['weight'] = str('%.2f' %data['weight'])\n",
|
||
" dict2['next_weight'] = str('%.2f' %(data['weight']-data['next_weight']))\n",
|
||
" dict2['bmi'] = str('%.2f' %((data['weight']/data['height'])/data['height']*10000))\n",
|
||
" bmi = (data['weight']/data['height'])/data['height']*10000\n",
|
||
" if bmi<18.5:\n",
|
||
" bmi_zt = '体重偏轻'\n",
|
||
" elif bmi >=18.5 and bmi <24.0:\n",
|
||
" bmi_zt = '体重正常'\n",
|
||
" elif bmi >=24.0 and bmi <28.0:\n",
|
||
" bmi_zt = '体重超重'\n",
|
||
" else:\n",
|
||
" bmi_zt = '体重肥胖'\n",
|
||
" dict2['bmi_zt'] = bmi_zt\n",
|
||
" dict2['waist'] = str('%.2f' %data['waist'])\n",
|
||
" dict2['hip'] = str('%.2f' %data['hip'])\n",
|
||
" whr = data['waist']/data['hip']\n",
|
||
" if data['gender'] =='male' and whr > 0.9:\n",
|
||
" whr_zt = '超标'\n",
|
||
" elif data['gender'] =='female' and whr > 0.85:\n",
|
||
" whr_zt = '超标'\n",
|
||
" else:\n",
|
||
" whr_zt = '正常'\n",
|
||
" dict2['whr'] = str('%.2f' %whr)\n",
|
||
" dict2['whr_zt'] = whr_zt\n",
|
||
" if 'sport_type' in data.keys(): \n",
|
||
" dict2['sport_type'] = ','.join(data['sport_type']).replace('other','其他')\n",
|
||
" dict2['sport_duration'] = str(data['sport_duration'])\n",
|
||
" dict2['recipe'] = data['recipe']\n",
|
||
" if 'last_sport' in data.keys(): \n",
|
||
" dict2['last_sport'] = data['last_sport']\n",
|
||
" if 'last_sport_time' in data.keys():\n",
|
||
" dict2['last_sport_time'] = str(data['last_sport_time'])\n",
|
||
" if 'last_sport-Comment' in data.keys():\n",
|
||
" dict2['last_sport-Comment'] = data['last_sport-Comment']\n",
|
||
" dict2['last_recipe'] = data['last_recipe']\n",
|
||
" if 'last_recipe-Comment' in data.keys(): \n",
|
||
" dict2['last_recipe-Comment'] = data['last_recipe-Comment']\n",
|
||
" if 'last_lose' in data.keys(): \n",
|
||
" dict2['last_lose'] = data['last_lose']\n",
|
||
" if 'last_lose_weight' in data.keys():\n",
|
||
" dict2['last_lose_weight'] = str('%.2f' %data['last_lose_weight'])\n",
|
||
" if 'last_lose-Comment' in data.keys(): \n",
|
||
" dict2['last_lose-Comment'] = data['last_lose-Comment']\n",
|
||
" if code in person.keys():\n",
|
||
" person[code][rq] = dict2\n",
|
||
"filename = 'data/宁夏能化干预人员问卷情况(20251222).json'\n",
|
||
"\n",
|
||
"with open(filename,'w') as fl:\n",
|
||
" json.dump(person, fl, ensure_ascii=False)\n",
|
||
"print(len(person),'ok')\n",
|
||
" "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "0fe62c71-eb58-4204-85bc-dc5497231df4",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 问卷信息汇总导入数据库"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 21,
|
||
"id": "55b20059-ae36-493e-b0c5-c7e35473efc7",
|
||
"metadata": {
|
||
"execution": {
|
||
"iopub.execute_input": "2025-12-22T03:49:42.115930Z",
|
||
"iopub.status.busy": "2025-12-22T03:49:42.114895Z",
|
||
"iopub.status.idle": "2025-12-22T03:49:43.037249Z",
|
||
"shell.execute_reply": "2025-12-22T03:49:43.036729Z",
|
||
"shell.execute_reply.started": "2025-12-22T03:49:42.115872Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import json\n",
|
||
"import openpyxl\n",
|
||
"import psycopg2\n",
|
||
"from datetime import datetime\n",
|
||
"\n",
|
||
"conn = psycopg2.connect(\n",
|
||
" host=\"localhost\",\n",
|
||
" database=\"tice\",\n",
|
||
" user=\"mydata\",\n",
|
||
" password=\"songyi\"\n",
|
||
")\n",
|
||
"cur = conn.cursor()\n",
|
||
"event_id = 1\n",
|
||
"org_id = 1\n",
|
||
"filename = 'data/宁夏能化干预人员问卷情况(20251222).json'\n",
|
||
"with open(filename,'r') as fl:\n",
|
||
" dict1 = json.load(fl)\n",
|
||
"list1 = ['name','sex','birth','unit']\n",
|
||
"xm = ['height','weight','next_weight','bmi','bmi_zt','waist', 'hip','whr','whr_zt' ]\n",
|
||
"data2 = []\n",
|
||
"for k,v in dict1.items():\n",
|
||
" list2 = []\n",
|
||
" \n",
|
||
" for k1,v1 in v.items():\n",
|
||
" #list2 = []\n",
|
||
" if k1 not in list1:\n",
|
||
" list2.append(k1)\n",
|
||
" list2.sort()\n",
|
||
" \n",
|
||
" for rq in list2:\n",
|
||
" for k1, v1 in v[rq].items():\n",
|
||
" code = k\n",
|
||
" \n",
|
||
" sql = \"SELECT id from fitness_person where code = %s and org_id = %s\"\n",
|
||
" params = (code,org_id)\n",
|
||
" cur.execute(sql, params)\n",
|
||
" rows = cur.fetchone()\n",
|
||
" if rows:\n",
|
||
" data1 = []\n",
|
||
" data1.append(event_id)\n",
|
||
" data1.append(rows[0])\n",
|
||
" data1.append(rq)\n",
|
||
" for item in xm:\n",
|
||
" data1.append(v[rq][item])\n",
|
||
" if 'sport_type' in v[rq].keys():\n",
|
||
" data1.append(v[rq]['sport_type'])\n",
|
||
" else:\n",
|
||
" data1.append('')\n",
|
||
" \n",
|
||
" if 'sport_duration' in v[rq].keys():\n",
|
||
" data1.append(v[rq]['sport_duration'])\n",
|
||
" else:\n",
|
||
" data1.append('')\n",
|
||
" if v[rq]['recipe']=='free':\n",
|
||
" data1.append('自行控制')\n",
|
||
" else:\n",
|
||
" data1.append('减脂餐')\n",
|
||
" if v[rq]['last_sport']=='是':\n",
|
||
" data1.append('运动时长:'+str(v[rq]['last_sport_time'])+'分钟')\n",
|
||
" else:\n",
|
||
" data1.append('未运动,'+v[rq]['last_sport-Comment'])\n",
|
||
" if v[rq]['last_recipe']=='是':\n",
|
||
" data1.append('是')\n",
|
||
" else:\n",
|
||
" data1.append('否。'+v[rq]['last_recipe-Comment'])\n",
|
||
" if v[rq]['last_lose']=='是':\n",
|
||
" data1.append('是,减重'+str(v[rq]['last_lose_weight'])+'公斤。')\n",
|
||
" elif 'last_lose-Comment' in v[rq].keys():\n",
|
||
" data1.append('否。'+v[rq]['last_lose-Comment'])\n",
|
||
" else:\n",
|
||
" data1.append('否。')\n",
|
||
" data2.append(tuple(data1))\n",
|
||
" \n",
|
||
"sql = \"INSERT INTO fitness_record_data (event_id,user_id,date_created, height, weight, next_weight, bmi,bmi_zt,waist, hip, whr,whr_zt, sport_type, sport_duration, recipe, last_sport, last_recipe, last_lose_comment)\" \n",
|
||
"sql = sql + \" VALUES (%s,%s, %s, %s,%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s, %s, %s, %s)\"\n",
|
||
"cur.executemany(sql, data2)\n",
|
||
"conn.commit()\n",
|
||
"\n",
|
||
"# 关闭连接\n",
|
||
"cur.close()\n",
|
||
"conn.close()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "c4cbfed3-51cf-47b8-af20-eee4ac61b556",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3 (ipykernel)",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.12.3"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|