Files
jupyter/体测单位/宁夏能化.ipynb
T
2025-12-22 21:13:47 +08:00

69 KiB

体质检测

体测人员导入

In [ ]:
import openpyxl
import json


wb = openpyxl.load_workbook('data/宁夏能化人员.xlsx',data_only=True)
sheet = wb.active
# sheets = wb.sheetnames
person = {}

for n in range(2, sheet.max_row+1):
    code = int(sheet.cell(n, 2).value)
    person.setdefault(code, {})
    dict1 = {}
    dict1['name'] = sheet.cell(n, 3).value
    dict1['sex'] = sheet.cell(n, 4).value
    unit = sheet.cell(n, 9).value
    
    dict1['sub_unit'] = sheet.cell(n, 6).value
    if unit == '管理部门':
        dict1['unit'] = sheet.cell(n, 6).value
    else:
        dict1['unit'] = sheet.cell(n, 9).value
    
    dict1['birth'] = str(sheet.cell(n, 5).value).replace('/','-').split(' ')[0] 
    person[code] = dict1
filename = 'data/宁夏能化人员-1.json'
with open(filename, 'w') as fl:
    json.dump(person, fl, ensure_ascii=False)
print(len(person),'ok')
In [ ]:
import openpyxl
import json


wb = openpyxl.load_workbook('data/宁夏能化-全部名单.xlsx',data_only=True)
sheet = wb.active
# sheets = wb.sheetnames
person = {}

for n in range(2, sheet.max_row+1):
    code = int(sheet.cell(n, 2).value)
    person.setdefault(code, {})
    dict1 = {}
    dict1['name'] = sheet.cell(n, 3).value
    dict1['sex'] = sheet.cell(n, 4).value
    unit = sheet.cell(n, 8).value
    
    dict1['sub_unit'] = sheet.cell(n, 6).value
    if unit == '管理部门':
        dict1['unit'] = sheet.cell(n, 9).value
    else:
        dict1['unit'] = sheet.cell(n, 8).value
    
    dict1['birth'] = str(sheet.cell(n, 5).value).replace('/','-').split(' ')[0] 
    person[code] = dict1
filename = 'data/宁夏能化人员all.json'
with open(filename, 'w') as fl:
    json.dump(person, fl, ensure_ascii=False)
print(len(person),'ok')

生成读卡系统文件

In [ ]:
import json

filename = 'data/宁夏能化人员.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
list1 = []
for k, v in dict1.items():
    dict2 = {}
    #if dict1['sex'] =='男':
    #    sex = 1
    
    dict2 = {'id':k,'name':v['name'],'gender':v['sex'],'birth':v['birth'],'unit':v['unit']}
    list1.append(dict2)
json_data = json.dumps(list1,ensure_ascii=False, indent=4)  

# 将 json 数据写入文件
with open("data/data_宁夏能化人员.json", "w",encoding = 'utf-8') as file:
    file.write(json_data)   
print('ok')

获取人员测试成绩

In [ ]:
import json
import datetime
import csv
from datetime import date
import my_module as My

filename = 'data/宁夏能化人员all.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl) 
filename = 'data/marks_20250619.csv'
re_ta = My.get_result(filename,dict1)


filename = 'data/result_宁夏能化人员all.json'
with open(filename,'w') as fl:
    json.dump(re_ta, fl, ensure_ascii=False) 
print(len(re_ta))
In [ ]:
import json
import datetime
import csv
from datetime import date
import my_module as My

filename = 'data/宁夏能化人员.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl) 
filename = 'data/marks_20250619.csv'
re_ta = My.get_result(filename,dict1)


filename = 'data/result_宁夏能化人员.json'
with open(filename,'w') as fl:
    json.dump(re_ta, fl, ensure_ascii=False) 
print(len(re_ta))
In [ ]:
filename = 'data/result_宁夏能化人员.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl) 

filename = 'data/result_宁夏能化人员all.json'
with open(filename,'r') as fl:
    dict2 = json.load(fl) 
list1 = []
for k,v in dict2.items():
    if k not in dict1.keys():
        #print(k,v['name'],v['sex'],v['unit'])
        #print(k,v)
        list1.append(str(k))
print(list1)

生成测试得分

In [ ]:
import json
import time
import my_module as My

#list_item = ['lung','grip','flexion','jump','pushup','balance','reaction','step','situp','height','weight']
list_item = ['lung','grip','flexion','jump','pushup','balance','reaction','step','situp']
filename = 'data/result_宁夏能化人员all.json'
with open(filename,'r') as fl:
    dict2 = json.load(fl) 
for k, v in dict2.items():
    #print(k)
    if v['sex'] == '男':
        sex = 'M'
    else:
        sex = 'F'            
    if 'bmi' in v.keys():
        #bmi_data = v['height']['成绩'].split()[0]+','+ v['weight']['成绩'].split()[0]
        bmi_data = v['bmi']['成绩']
        data1 = {'code':k,'sex':sex,'age':v['age'],'item':'HeightWeight','result':bmi_data}
        dict2[k]['bmi'] = {}
        dict2[k]['bmi']['成绩'] = bmi_data
        dict2[k]['bmi']['score'] = My.cal_bmi(data1)
    for item_en in list_item:
        if item_en in v.keys():            
            data1 = {'code':k,'sex':sex,'age':v['age'],'item':item_en,'result':float(v[item_en]['成绩'].split()[0])}
            #print(k,v['name'])
            dict2[k][item_en]['score'] = My.cal_score(data1)
            #print(k,v[item_en]['成绩'],cal_score(data1))

filename = f'data/result_宁夏能化人员all.json'
with open(filename,'w') as fl:
    json.dump(dict2,fl , ensure_ascii=False) 
print('ok!')        

导出测试人员信息

In [ ]:
import json
import openpyxl

items = ['lung','grip','flexion','jump','pushup','situp','balance','reaction','step']
title = ['编号','姓名','性别','单位','部门','身高','体重','肺活量','握力','坐位体前屈','纵跳','俯卧撑','一分钟仰卧起坐','单脚站立','选择反应时','台阶指数']

filename = 'data/result_宁夏能化人员all.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)

filename = 'data/宁夏能化人员all.json'
with open(filename,'r') as fl:
    dict2 = json.load(fl)
    
list1 = []
for k, v in dict1.items():
    list2 = []
    list2.append(str(k).rjust(5,'0'))
    list2.append(v['name'])    
    list2.append(dict2[k]['sex'])
    list2.append(dict2[k]['unit'])
    list2.append(dict2[k]['sub_unit'])
    if 'bmi' in v.keys():
        height = v['bmi']['成绩'].split(',')[0]
        weight = v['bmi']['成绩'].split(',')[1]
        list2.append(height)
        list2.append(weight)
    else:
        list2.append('')
        list2.append('')
    for item in items:
        if item in v.keys():
            list2.append(v[item]['成绩']) 
        elif item =='name':
            list2.append(v[item])
        else:
            list2.append('')
    
    list1.append(list2)
filename = 'data/宁夏能化测试情况明细表.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(title)
for row in list1:
    sheet.append(row)
    
wb.save(filename)
In [ ]:
import json
import openpyxl

items = ['lung','grip','flexion','jump','pushup','situp','balance','reaction','step']
title = ['编号','姓名','性别','单位','部门','身高','体重','肺活量','握力','坐位体前屈','纵跳','俯卧撑','一分钟仰卧起坐','单脚站立','选择反应时','台阶指数']

filename = 'data/result_宁夏能化人员all.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)

filename = 'data/宁夏能化人员all.json'
with open(filename,'r') as fl:
    dict2 = json.load(fl)
    
list1 = []
for k, v in dict1.items():
    list2 = []
    list2.append(str(k).rjust(5,'0'))
    list2.append(v['name'])    
    list2.append(v['sex'])
    list2.append(v['unit'])
    list2.append(v['age'])
    
    list1.append(list2)
filename = 'data/宁夏能化测试人员年龄情况明细表.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(title)
for row in list1:
    sheet.append(row)
    
wb.save(filename)

统计各部门测试人员情况

In [ ]:
import json

filename = 'data/result_宁夏能化人员.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)

filename = 'data/宁夏能化人员.json'
with open(filename,'r') as fl:
    dict2 = json.load(fl)

list1 = []
for k, v in dict2.items():
    if k not in dict1.keys():
        list2 = []
        unit = v['unit']
        if unit=='管理部门':
            sub_unit = v['sub_unit']
        else:
            sub_unit = unit
        list2 = [k,v['name'],v['sex'],sub_unit]
        list1.append(list2)
    



filename = 'data/宁夏能化未体测人员.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(title)
for row in list1:
    sheet.append(row)
    
wb.save(filename)

统计各部门已测试情况

In [ ]:
import json

filename = 'data/result_宁夏能化人员.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)

filename = 'data/宁夏能化人员.json'
with open(filename,'r') as fl:
    dict2 = json.load(fl)

dict3 = {}


title =['单位','体测人数']
list1 = []
for k,v in dict1.items():
    unit = v['unit']
    if unit=='管理部门':
        sub_unit = dict2[k]['sub_unit']
    else:
        sub_unit = unit
    dict3.setdefault(unit,{})    
    dict3[unit].setdefault(sub_unit,{})
    dict3[unit][sub_unit].setdefault('人数',0)
    dict3[unit][sub_unit]['人数'] = dict3[unit][sub_unit]['人数'] + 1

for k, v in dict3.items():
    list2 = []
    for k1,v1 in v.items():
        list2 = [k,k1,v1['人数']]
        list1.append(list2)

filename = 'data/宁夏能化部门测试情况表.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(title)
for row in list1:
    sheet.append(row)
    
wb.save(filename)
In [ ]:
import json

filename = 'data/result_宁夏能化人员.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)

filename = 'data/宁夏能化人员.json'
with open(filename,'r') as fl:
    dict2 = json.load(fl)

dict3 = {}


title =['单位','体测人数']
list1 = []
for k,v in dict2.items():
    unit = v['unit']
    if unit=='管理部门':
        sub_unit = dict2[k]['sub_unit']
    else:
        sub_unit = unit
    dict3.setdefault(unit,{})    
    dict3[unit].setdefault(sub_unit,{})
    dict3[unit][sub_unit].setdefault('人数',0)
    dict3[unit][sub_unit]['人数'] = dict3[unit][sub_unit]['人数'] + 1

for k, v in dict3.items():
    list2 = []
    for k1,v1 in v.items():
        list2 = [k,k1,v1['人数']]
        list1.append(list2)

filename = 'data/宁夏能化部门人员情况表.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(title)
for row in list1:
    sheet.append(row)
    
wb.save(filename)

生成体测成绩明细表

In [ ]:
import json
import openpyxl

items = ['lung','grip','flexion','jump','pushup','situp','balance','reaction','step']
bmi = ['height','weight']
title = ['编号','姓名','性别','单位/部门','身高','体重','bmi','肺活量','得分','握力','得分','坐位体前屈','得分','纵跳','得分','俯卧撑','得分','一分钟仰卧起坐','得分','单脚站立','得分','选择反应时','得分','台阶指数','得分']

filename = 'data/result_宁夏能化人员all.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)

list1 = []
for k, v in dict1.items():    #print(k,dict2[str(k)]['name'])
    list2 = []
    list2.append(str(k).rjust(5,'0'))
    list2.append(dict1[k]['name'])    
    list2.append(dict1[k]['sex'])
    list2.append(dict1[k]['unit'])
    i = 0
    if 'bmi' in dict1[k].keys():
        list2.append(dict1[k]['bmi']['成绩'].split(',')[0])
        list2.append(dict1[k]['bmi']['成绩'].split(',')[1])
        list2.append(dict1[k]['bmi']['score'])
        i+=1
    else:
        list2.append('') 
        list2.append('') 
        list2.append('') 
           
        
    for item in items:
        if item in dict1[k].keys():
            list2.append(dict1[k][item]['成绩'])
            list2.append(dict1[k][item]['score']) 
            i+=1
        elif item =='name':
            list2.append(dict1[k][item])
        else:
            list2.append('') 
            list2.append('') 
    if i>2:
        list1.append(list2)
filename = 'data/宁夏能化体测情况明细表.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(title)
for row in list1:
    sheet.append(row)
    
wb.save(filename)
print('ok')       

生成报告

In [ ]:
import requests
import json
import openpyxl


headers = {
    "Content-Type": "application/json; charset=UTF-8"
    }
filename = 'data/result_宁夏能化人员.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
list1 = []
file_path ='./宁夏能化人员/'
list_item = ['lung','grip','flexion','jump','pushup','balance','reaction','step','situp','bmi']
i=0
list2 = []
for  k, v in dict1.items():
    list1 = []
    mydata = {}
    
    id = str(k).rjust(4,"0")
    mydata['path'] = file_path+id+'-'+ v['name']+'.pdf'
    mydata['title'] = '中石化宁夏能化公司'
    mydata['subtitle'] = v['unit']
    mydata['id'] = id
    mydata['name'] = v['name']
    if v['sex'] == '男':
        mydata['gender'] = 'male'
    else:
        mydata['gender'] = 'female'
    
    mydata['month'] = v['month']
    mydata['fits'] = {}
    survey_list = ['tcm','psy57','spine']
    for item in survey_list:
        if item in  v.keys():
            mydata.setdefault('surveys',{})
            mydata['surveys'][item] = v[item]
            
    
    #mydata['fits'] = {}
    for item in list_item:
        if item in v.keys():
            mydata.setdefault('fits',{})
            if item in ['lung','pushup','step','situp']:
                mark = v[item]['成绩'].split()[0].split('.')[0]
            else:
                mark = v[item]['成绩'].split()[0]
            mydata['fits'][item] = {'mark':mark,'score':v[item]['score']}
    #if len(mydata['fits']) >2 or len(mydata['surveys']) >0:
    if len(mydata['fits']) >2 :    
        list1.append(mydata)
        list2.append([k,v['name']])
        i+=1
        x = requests.post('http://localhost:3003', data = json.dumps(list1), headers=headers)
        #print(id,v['name'],x.text)
        #print(mydata)
        #x.close()
print(i)
In [ ]:
import requests
import json
import openpyxl


headers = {
    "Content-Type": "application/json; charset=UTF-8"
    }
filename = 'data/result_宁夏能化人员all.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
list1 = []
file_path ='./宁夏能化人员/'
list_item = ['lung','grip','flexion','jump','pushup','balance','reaction','step','situp','bmi']
i=0
list2 = []
for  k, v in dict1.items():
    list1 = []
    mydata = {}
    if str(k) in ['2018652', '2019665', '2019160', '2019512', '3427477', '2019423', '2019494', '2019901']:
        
        id = str(k).rjust(4,"0")
        mydata['path'] = file_path+id+'-'+ v['name']+'.pdf'
        mydata['title'] = '中石化宁夏能化公司'
        mydata['subtitle'] = v['unit']
        mydata['id'] = id
        mydata['name'] = v['name']
        if v['sex'] == '男':
            mydata['gender'] = 'male'
        else:
            mydata['gender'] = 'female'
        
        mydata['month'] = v['month']
        mydata['fits'] = {}
        survey_list = ['tcm','psy57','spine']
        for item in survey_list:
            if item in  v.keys():
                mydata.setdefault('surveys',{})
                mydata['surveys'][item] = v[item]
                
        
        #mydata['fits'] = {}
        for item in list_item:
            if item in v.keys():
                mydata.setdefault('fits',{})
                if item in ['lung','pushup','step','situp']:
                    mark = v[item]['成绩'].split()[0].split('.')[0]
                else:
                    mark = v[item]['成绩'].split()[0]
                mydata['fits'][item] = {'mark':mark,'score':v[item]['score']}
        #if len(mydata['fits']) >2 or len(mydata['surveys']) >0:
        if len(mydata['fits']) >2 :    
            list1.append(mydata)
            list2.append([k,v['name']])
            i+=1
            x = requests.post('http://localhost:3003', data = json.dumps(list1), headers=headers)
            #print(id,v['name'],x.text)
            #print(mydata)
            #x.close()
print(i)

体测报告按部门分类

In [ ]:
import os,sys,shutil
import json
import glob
from pathlib import Path

fi_path = '/home/songyi/pdf-typescript-old2/宁夏能化人员'
new_path = 'file/宁夏能化'
old = []
dict2 = {}

filename = 'data/result_宁夏能化人员all.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
fls = glob.glob(f'{fi_path}/*.pdf')
print(len(fls))
for fn in fls:
    fi_name =Path(fn).stem.split('-')[0]
    code = int(fi_name)    
    unit_path = Path(new_path,dict1[str(code)]['unit'])
    unit_path.mkdir(parents = True, exist_ok = True)
    n_name = Path(unit_path,Path(fn).stem+'.pdf')
    #if not os.path.exists(n_name):
    shutil.copyfile(fn,n_name)
    #print(fn)

生成体测报告打印明细表

In [ ]:
import os,sys,shutil
import json
import glob
from pathlib import Path
import openpyxl

fi_path = '/home/songyi/pdf-typescript-old2/宁夏能化人员'

filename = 'data/result_宁夏能化人员all.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
fls = glob.glob(f'{fi_path}/*.pdf')
list1 = []
title = ['测试编号','姓名','性别','部门']
for fn in fls:
    list2 = []
    fi_name =Path(fn).stem.split('-')[0]    
    code = int(fi_name)
    sex = dict1[str(code)]['sex']
    unit = dict1[str(code)]['unit']
    list2 = [fi_name,Path(fn).stem.split('-')[1],sex,unit]
    
    list1.append(list2)

filename = 'data/宁夏能化体测报告明细表.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(title)
for row in list1:
    sheet.append(row)
    
wb.save(filename) 
print('ok')

统计体质检测等级明细表

In [ ]:
import json
import openpyxl


filename = 'data/data_宁夏能化人员.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
list1 = []
for k, v in dict1.items():
    list1.append([k,v['name'],v['unit'],v['sex'],v['age'],v['level'],v['score']])
    
filename = 'data/宁夏能化等级情况表.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
#sheet.append(title)
for row in list1:
    sheet.append(row)
    
wb.save(filename)  
    

导出体检报告阳性数据

In [ ]:
import openpyxl
import json


wb = openpyxl.load_workbook('data/2025年中国石化长城能源化工(宁夏)有限公司体检数据.xlsx',data_only=True)
sheet = wb.active
# sheets = wb.sheetnames
person = {}

for n in range(2, sheet.max_row+1):
    code = int(sheet.cell(n, 1).value)
    person.setdefault(code, {})
    dict1 = {}
    dict1['name'] = sheet.cell(n, 3).value
    dict1['sex'] = sheet.cell(n, 4).value
    dict1['unit'] = sheet.cell(n, 2).value
    dict1['age'] = sheet.cell(n, 5).value
    content = sheet.cell(n, 6).value
    dict1['signs'] = content.split(',')
    person[code] = dict1
filename = 'data/宁夏能化体检数据2025.json'
with open(filename, 'w') as fl:
    json.dump(person, fl, ensure_ascii=False)
print(len(person),'ok')

筛选体重相关异常数据人员

In [ ]:
import json
import openpyxl

filename = 'data/宁夏能化体检数据2025.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
list1 = []
list2 = ['肥胖','超重','脂肪肝','血脂','高脂血症','动脉粥样硬化']
for k, v in dict1.items():
    zz = set()
    for item in v['signs']:
        for xm in list2:
            if xm in item:
                zz.add(item)
    if len(zz) > 0:
        list1.append([k,v['name'],v['unit'],v['sex'],v['age'],','.join(zz)])

filename = 'data/宁夏能化体检体重相关人员.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
#sheet.append(title)
for row in list1:
    sheet.append(row)
    
wb.save(filename)  
    

高危风险干预

导入干预人员名单

In [ ]:
import json
import csv
import openpyxl
import time
from datetime import date

wb = openpyxl.load_workbook('data/宁夏能化高危风险人群最终干预人员名单.xlsx',data_only=True)
sheet = wb.active
# sheets = wb.sheetnames
person = {}
for n in range(2, sheet.max_row+1):
    code = str(int(sheet.cell(n, 2).value))
    person.setdefault(code, {})
    dict1 = {}
    dict1['name'] = sheet.cell(n, 3).value
    dict1['sex'] = sheet.cell(n, 4).value
    dict1['unit'] = sheet.cell(n, 6).value  
    dict1['age'] = int(sheet.cell(n, 5).value)
    dict1['symptoms'] = sheet.cell(n, 8).value
    person[code] = dict1
filename = 'data/宁夏能化高危风险干预人员.json'
with open(filename, 'w') as fl:
    json.dump(person, fl, ensure_ascii=False)
print(len(person),'ok')

获取问卷人员信息

In [28]:
import json
import csv
import openpyxl
import time
from datetime import date

filename = 'data/宁夏能化高危风险干预人员.json'
with open(filename,'r') as fl:
    dict3 = json.load(fl)
    
dict1 = {}
list1 = []
filename = 'data/sql_20251209.csv'
with open(filename,'r',newline='') as csv_file:
    fl = csv.reader(csv_file,delimiter=',')
    header = next(fl)    
    for line in fl:
        list1.append(line)

nn = 0
for item in list1:
    dict2 = {}
    name = item[2]
    phone = str(item[3])[2:]
    birth = str(item[5])
    content = json.loads(item[1])
    tcm = []
    for i in range(0,60):
        tcm.append(0)
    for k, v in content.items():           
        if 'tcm' not in k:
            dict2[k] = v
        else:
            i = int(k[3:])
            tcm[i-1] = int(v)
    
    code = str(int(dict2['code']))
    if code in dict3.keys():
        dict1.setdefault(code,{})
        dict1[code]['name'] = dict2['name']
        if dict2['gender'] == 'm':
            dict1[code]['sex'] = '男'
        else:
            dict1[code]['sex'] = '女'
        dict1[code]['birth'] = str(item[4])
        dict1[code]['unit'] = dict2['unit']
        dict1[code]['age'] = dict3[code]['age']
        dict1[code]['phone'] = phone
        dict1[code]['waist'] = dict2['waist']
        dict1[code]['hip'] = dict2['hip']
        dict1[code]['tcm'] = tcm
filename = 'data/survey_宁夏能化高危风险干预人员.json'

with open(filename,'w') as fl:
    json.dump(dict1, fl, ensure_ascii=False)
print(len(dict1),'ok')
204 ok
In [25]:
import json
import csv
import openpyxl
import time
from datetime import date

filename = 'data/宁夏能化高危风险干预人员.json'
with open(filename,'r') as fl:
    dict3 = json.load(fl)
    
dict1 = {}
list1 = []
filename = 'data/sql_20251222.csv'
with open(filename,'r',newline='') as csv_file:
    fl = csv.reader(csv_file,delimiter=',')
    header = next(fl)    
    for line in fl:
        list1.append(line)

nn = 0
for item in list1:
    dict2 = {}
    
    phone = str(item[0])[2:]
    content = json.loads(json.loads(item[1]))
    tcm = []
    for i in range(0,60):
        tcm.append(0)
    for k, v in content.items():           
        if 'tcm' not in k:
            dict2[k] = v
        else:
            i = int(k[3:])
            tcm[i-1] = int(v)
    
    code = str(int(dict2['code']))
    if code in dict3.keys():
        dict1[code] = dict3[code]
        dict1[code]['waist'] = dict2['waist']
        dict1[code]['hip'] = dict2['hip']
        dict1[code]['tcm'] = tcm   
        

filename = 'data/survey_宁夏能化高危风险干预人员(202512).json'

with open(filename,'w') as fl:
    json.dump(dict1, fl, ensure_ascii=False)
print(len(dict1),'ok')
111 ok

常用参数及自定义函数

In [26]:
import json

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]]

kinds = ['平和','气虚','阳虚','阴虚','痰湿','湿热','血瘀','气郁','特禀']

def tcm_calc(arr):
  qa = [8, 8, 7, 8, 8, 6, 7, 7, 7]
  s = [0] * 9
  for i in range(len(questions)):
    m = arr[i] - 1
    for v in questions[i]:
      if v < 0:
        s[-v - 1] += 4 - m
      else:
        s[v - 1] += m
  return [int((v / qa[i]) * 25) for i, v in enumerate(s)]

def tcm_kind(score):
  kind = 0
  near = False
  max_kind = 0
  max_score = 0
  for i in range(1, 9):
    if score[i] > max_score:
      max_kind = i
      max_score = score[i]
  if score[0] >= 60 and max_score < 40:
    if max_score >= 30:
      near = True
      kind = max_kind
  else:
    kind = max_kind
  return {
    "kind": kind,
    "near": near
  }
list2 = ['成就感','愉快心境','放松程度','压力应对','体力充沛','情感充沛度']
list3 = [[5,7],[7,4],[7,4],[7,4],[8,1],[6,1]] 
list4 = ['颈椎','胸椎','腰椎','骶尾椎']
list5 = [[0,10,10],[10,17,7],[17,24,6],[24,26,2]]
qb = [
      1, 1, 1, 1, 1,
      4, 3, 2, 3, 2, 4, 3,
      4, 3, 2, 4, 4, 2, 4,
      3, 2, 2, 4, 3, 3, 2,
      5, 5, 5, 5, 5, 5, 5, 5,
      6, 6, 6, 6, 6, 6
    ]

导出问卷监测情况表

In [27]:
import openpyxl


filename = 'data/survey_宁夏能化高危风险干预人员(202512).json'
with open(filename,'r') as fl:
    dict1 = json.load(fl) 
data_list = []
for k, v in dict1.items():
    list6 = []
    list6.append(str(k).rjust(4,'0'))
    list6.append(v['name'])
    list6.append(v['sex'])
    list6.append(v['unit'])
    list6.append(v['age'])
    list6.append(v['waist'])
    list6.append(v['hip'])
    
    if 'tcm' in v.keys():
        list1 = []
        tcm =v['tcm']
        for item in tcm:
            list1.append(item)
        score = tcm_calc(list1)

        result = tcm_kind(score)
        kind = result['kind']
        near = result['near']
        #print(k,kinds[kind], near, score)
        list6.append(kinds[kind])
        if near:
            list6.append('是')
        else:
            list6.append('')
        for item in score:
            list6.append(item)
    else:
        for i in range(0,11):
            list6.append('')
            i+=1
    
    data_list.append(list6)
filename = 'data/宁夏能化高危风险干预人员体质问卷明细表(20251222).xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
title = ['编号', '姓名', '性别', '单位/部门', '年龄', '腰围', '臀围', '中医体质', '是否倾向', '平和', '气虚', '阳虚', '阴虚', '痰湿', '湿热', '血瘀', '气郁', '特禀']
sheet.append(title)
for row in data_list:
    sheet.append(row)
    
wb.save(filename)    

导出问卷人员信息

In [ ]:
import json
import openpyxl


title = ['员工编号','姓名','性别','部门']

filename = 'data/survey_宁夏能化干预人员.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)

filename = 'data/宁夏能化干预人员.json'
with open(filename,'r') as fl:
    dict2 = json.load(fl)
    
list1 = []
for k, v in dict2.items():
    
    if str(k) not in dict1.keys():
        list2 = []
        list2 = [k,v['name'],v['sex'],v['unit']]      
        list1.append(list2)
filename = 'data/宁夏能化干预人员未参加问卷人员(20251010).xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(title)
for row in list1:
    sheet.append(row)
    
wb.save(filename)

体重管理系统

问卷转换

In [15]:
import json
import openpyxl


title = []

filename = 'data/surveys_records_2025-12-22.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
list1 = []
title = ['id', 'date_created']
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']
for item in dict1:
    list2 = []
    for xm in title:
        if xm in item.keys():
            list2.append(item[xm])
        else:
            list2.append('')
    for xm in data_title:
        data = json.loads(item['data'])
        if xm in data.keys():
            if xm =='sport_type':
                list2.append(','.join(data[xm]))
            else:
                list2.append(data[xm])
        else:
            list2.append('')
        
    list1.append(list2)
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']
filename = 'data/宁夏能化干预人员问卷(20251222).xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(all_title)
for row in list1:
    sheet.append(row)
    
wb.save(filename)        

问卷导入数据库

核对新增人员

In [16]:
import json
import openpyxl
import psycopg2

conn = psycopg2.connect(
    host="localhost",
    database="tice",
    user="mydata",
    password="songyi"
)
cur = conn.cursor()
wb = openpyxl.load_workbook('data/宁夏能化干预人员问卷(20251222).xlsx',data_only=True)
sheet = wb.active
# sheets = wb.sheetnames
org_id = 1
event_id = 1
sql = "SELECT code from fitness_person where org_id = %s"
params = (org_id,)
cur.execute(sql, params)
rows = cur.fetchall()
codes = []
for row in rows:
    codes.append(row[0])

person = {}
for n in range(2, sheet.max_row+1):
    code = str(sheet.cell(n, 6).value)
    if code not in codes:
        person.setdefault(code, {})
        dict1 = {}
        dict1['name'] = sheet.cell(n, 3).value
        sex = sheet.cell(n, 4).value
        if sex =='male':
            dict1['sex'] = '男'
        else:
            dict1['sex'] = '女'
        birth = str(sheet.cell(n, 5).value).split()[0]
        
        if len(birth)<10:
            nian = birth.split('-')[0]
            yue = birth.split('-')[1].rjust(2,'0')
            if len(birth.split('-')[0]) ==3:
                ri = birth.split('-')[2].rjust(2,'0')
            else:
                ri = '01' 
            dict1['birth'] = nian+'-'+ yue + '-'+ ri
        else:
            dict1['birth'] = birth
        dict1['unit'] = sheet.cell(n, 7).value
        person[code] = dict1
org_id = 1
data1 = []
for k,v in person.items():
    tuple1 = (v['name'],v['sex'],v['unit'],v['birth'],k,org_id)
    print(tuple1)
    data1.append(tuple1)
print(len(data1))
sql = "INSERT INTO fitness_person ( name, sex,unit,birth,code,org_id) VALUES (%s, %s, %s,%s, %s, %s)"  
#print(data1)
cur.executemany(sql, data1)
conn.commit()

# 关闭连接
cur.close()
conn.close()
('汪晖', '男', '销售中心', '1971-02-01', '02019646', 1)
('马燕金', '男', '销售中心', '1983-04-01', '02018528', 1)
('孙皓', '男', '销售中心', '1990-10-01', '02019111', 1)
('李润良', '男', '环保建材运行部', '1992-06-01', '03427471', 1)
('王春光', '男', '环保建材运行部', '1969-12-01', '02020528', 1)
('户利平', '男', '环保建材运行部', '1969-08-01', '02020512', 1)
('马婉婷', '女', '公用工程运行部', '2001-11-01', '03561578', 1)
('储方伟', '男', '环保建材运行部', '1976-01-01', '02020483', 1)
('马斯勇', '男', '环保建材运行部', '1987-08-01', '02020525', 1)
('康磊', '男', '公用工程运行部', '1987-07-01', '02020289', 1)
('王涛', '男', '环保建材运行部', '1982-07-01', '00000521', 1)
('杨德忠', '男', '公用工程运行部', '1972-11-01', '02020306', 1)
('杨思国', '男', '电气仪表中心', '1981-04-01', '02018877', 1)
('党金广', '男', '电气仪表中心', '1987-08-01', '02018839', 1)
('张锋', '男', '电气仪表中心', '1971-05-01', '02020003', 1)
('张磊', '男', '电气仪表中心', '1989-12-01', '02018949', 1)
('秦国振', '男', '电气仪表中心', '1997-07-01', '03462294', 1)
('陈龙', '男', '电气仪表中心', '1981-05-01', '02018914', 1)
('朱岳峰', '男', '电气仪表中心', '1988-02-01', '02018865', 1)
19

导入问卷信息(已作废)

In [ ]:
import json
import openpyxl
import psycopg2

conn = psycopg2.connect(
    host="localhost",
    database="tice",
    user="mydata",
    password="songyi"
)
cur = conn.cursor()

org_id = 1
event_id = 1
filename = 'data/surveys_records_宁夏能化.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
list1 = []
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']
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']
for item in dict1:
    
    code = item['data']['code']
    sql = "SELECT id from fitness_person where code = %s and org_id = %s"
    params = (code,org_id)
    cur.execute(sql, params)
    rows = cur.fetchone()
    if rows:
        list2 = []
        id = rows[0]
        list2.append(event_id)
        list2.append(id)
        date_created = item['date_created']
        list2.append(date_created)
        for xm in data_title:
            if xm in item['data'].keys():
                if xm =='sport_type':
                    list2.append(','.join(item['data'][xm]))
                else:
                    list2.append(item['data'][xm])
            else:
                list2.append('')
        list1.append(tuple(list2))
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)" 
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)"
cur.executemany(sql, list1)
conn.commit()

# 关闭连接
cur.close()
conn.close()

问卷导入JSON

In [20]:
import json
import openpyxl

wb = openpyxl.load_workbook('data/宁夏能化干预人员问卷(20251222).xlsx',data_only=True)
sheet = wb.active
# sheets = wb.sheetnames
person = {}
for n in range(2, sheet.max_row+1):
    code = str(sheet.cell(n, 6).value)
    print(code)
    person.setdefault(code, {})
    dict1 = {}
    dict1['name'] = sheet.cell(n, 3).value
    dict1['sex'] = sheet.cell(n, 4).value
    birth = str(sheet.cell(n, 5).value).split()[0]
    if len(birth)<10:
        nian = birth.replace('/','-').split('-')[0]
        yue = birth.replace('/','-').split('-')[1].rjust(2,'0')
        if len(birth.replace('/','-').split('-')[0]) ==3:
            ri = birth.replace('/','-').split('-')[2].rjust(2,'0')
        else:
            ri = '01' 
        dict1['birth'] = nian+'-'+ yue + '-'+ ri
    dict1['unit'] = sheet.cell(n, 7).value
    person[code] = dict1
#print(person)

filename = 'data/surveys_records_2025-12-22.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
list1 = []
set1 = set()
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']
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']
for item in dict1:  
    dict2 = {}
    data = json.loads(item['data'])
    code = data['code']
    rq = item['date_created'].split()[0]
    dict2['height'] = str('%.2f' % data['height'])
    dict2['weight'] = str('%.2f' %data['weight'])
    dict2['next_weight'] = str('%.2f' %(data['weight']-data['next_weight']))
    dict2['bmi'] = str('%.2f' %((data['weight']/data['height'])/data['height']*10000))
    bmi = (data['weight']/data['height'])/data['height']*10000
    if bmi<18.5:
        bmi_zt = '体重偏轻'
    elif bmi >=18.5 and bmi <24.0:
        bmi_zt = '体重正常'
    elif bmi >=24.0 and bmi <28.0:
        bmi_zt = '体重超重'
    else:
        bmi_zt = '体重肥胖'
    dict2['bmi_zt'] = bmi_zt
    dict2['waist'] = str('%.2f' %data['waist'])
    dict2['hip'] = str('%.2f' %data['hip'])
    whr = data['waist']/data['hip']
    if data['gender'] =='male' and whr > 0.9:
        whr_zt = '超标'
    elif data['gender'] =='female' and whr > 0.85:
        whr_zt = '超标'
    else:
        whr_zt = '正常'
    dict2['whr'] = str('%.2f' %whr)
    dict2['whr_zt'] = whr_zt
    if 'sport_type' in  data.keys():        
        dict2['sport_type'] = ','.join(data['sport_type']).replace('other','其他')
        dict2['sport_duration'] = str(data['sport_duration'])
    dict2['recipe'] = data['recipe']
    if 'last_sport' in  data.keys():  
        dict2['last_sport'] = data['last_sport']
    if 'last_sport_time' in  data.keys():
        dict2['last_sport_time'] = str(data['last_sport_time'])
    if 'last_sport-Comment' in  data.keys():
        dict2['last_sport-Comment'] = data['last_sport-Comment']
    dict2['last_recipe'] = data['last_recipe']
    if 'last_recipe-Comment' in  data.keys():  
        dict2['last_recipe-Comment'] = data['last_recipe-Comment']
    if 'last_lose' in  data.keys(): 
        dict2['last_lose'] = data['last_lose']
    if 'last_lose_weight' in  data.keys():
        dict2['last_lose_weight'] = str('%.2f' %data['last_lose_weight'])
    if 'last_lose-Comment' in  data.keys(): 
        dict2['last_lose-Comment'] = data['last_lose-Comment']
    if code in person.keys():
        person[code][rq] = dict2
filename = 'data/宁夏能化干预人员问卷情况(20251222).json'

with open(filename,'w') as fl:
    json.dump(person, fl, ensure_ascii=False)
print(len(person),'ok')
    
02019646
2018434
02018586
02018444
02020443
02020415
02018446
2018447
03533284
02020243
02019374
02020238
02019952
03497967
02018709
02019342
02019620
02020203
02018528
02019111
02018482
02018481
02018628
02018498
02018499
964089
0214545
02020481
2018609
01348929
02020526
03427471
02020196
02020528
2019094
02020512
02020476
03561578
0003533364
02019294
02020483
02020527
02020467
02020296
02020525
02020289
03434760
02018435
2019094
02019688
02019688
03561564
02020346
02020458
00000521
02020306
02019655
02020189
02019787
02019787
02018611
02018566
02018645
02019485
02020127
02019054
2019006
028415
03462271
02028323
02020492
02018794
02018877
02018926
02018809
02018938
02020083
02018839
2018985
02019866
02018793
03428863
02019017
02018981
02020003
03462285
02018626
02018604
02018892
02018827
02018949
02019599
02019622
02018575
2018438
02019613
03363370
01702447
020186
02018709
03497967
0201968
02019641
02018709
02019697
02019021
0435
02018685
02018772
02019620
03497960
02019888
02019682
03391642
03462294
02018914
2019961
02018859
02019377
02017961
02018607
02018865
02020277
02020251
02020321
03391661
02020276
02019062
02019887
03148213
2019822
02019840
02019790
02019819
02019754
03437945
02019749
02019111
03418292
02019764
02019744
02019734
02019374
02019778
02019788
02019725
02017961
02019751
02020282
02019856
02019832
02019848
03437965
02019954
02020052
02019820
02019893
02019752
02019784
02020343
02019810
2019735
02019023
02020327
02019852
02018538
02018977
2018942
02019898
02019794
02019805
02019342
02019895
02019154
02019775
3391687
02019917
02018754
03418302
02019738
03462274
03561561
02019783
02018967
2019324
02019355
02019003
02019377
02019803
02019311
02019346
02019525
02020502
03497957
02020242
02019347
3418215
02019340
02019773
0219181
02019336
02019340
02018999
02019357
02019100
02019430
02019356
03418230
03437952
03437952
02019231
03497937
03497917
02019318
03462262
02019110
2019919
02019382
03497562
02019369
03391686
02019736
02019786
02019411
3391674
02018727
02019035
02019350
02019414
2019353
2019353
2019493
02019903
02019825
02019802
03394941
02020243
2019914
02019164
02019467
03437937
02019366
02019344
2019930
02019910
02019647
02019843
01825596
02019882
03409694
02019391
02019389
02019301
02020005
02019920
02019498
02019373
02019733
2019998
02020000
02019401
02019901
03418242
02019383
02019438
02019871
02019193
02019635
02020203
02019098
02019459
02018996
02019499
02019687
02018674
02019059
02019059
2019845
2018609
02019379
02020006
2019923
02019774
02019068
020294
03403228
3019337
02018711
03462315
02019739
02019278
02019416
2018973
02019256
02019878
02019664
02019404
02019058
02019504
02019361
02019600
02019381
02019248
02019543
02019648
02020260
287 ok

问卷信息汇总导入数据库

In [21]:
import json
import openpyxl
import psycopg2
from datetime import datetime

conn = psycopg2.connect(
    host="localhost",
    database="tice",
    user="mydata",
    password="songyi"
)
cur = conn.cursor()
event_id = 1
org_id = 1
filename = 'data/宁夏能化干预人员问卷情况(20251222).json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
list1 = ['name','sex','birth','unit']
xm = ['height','weight','next_weight','bmi','bmi_zt','waist', 'hip','whr','whr_zt' ]
data2 = []
for k,v in dict1.items():
    list2 = []
    
    for k1,v1 in v.items():
        #list2 = []
        if k1 not in list1:
            list2.append(k1)
    list2.sort()
   
    for rq in list2:
        for k1, v1 in v[rq].items():
            code = k
            
            sql = "SELECT id from fitness_person where code = %s and org_id = %s"
            params = (code,org_id)
            cur.execute(sql, params)
            rows = cur.fetchone()
            if rows:
                data1 = []
                data1.append(event_id)
                data1.append(rows[0])
                data1.append(rq)
                for item in xm:
                    data1.append(v[rq][item])
                if 'sport_type' in  v[rq].keys():
                    data1.append(v[rq]['sport_type'])
                else:
                    data1.append('')
                
                if 'sport_duration' in  v[rq].keys():
                    data1.append(v[rq]['sport_duration'])
                else:
                    data1.append('')
                if v[rq]['recipe']=='free':
                    data1.append('自行控制')
                else:
                    data1.append('减脂餐')
                if v[rq]['last_sport']=='是':
                    data1.append('运动时长:'+str(v[rq]['last_sport_time'])+'分钟')
                else:
                    data1.append('未运动,'+v[rq]['last_sport-Comment'])
                if v[rq]['last_recipe']=='是':
                    data1.append('是')
                else:
                    data1.append('否。'+v[rq]['last_recipe-Comment'])
                if v[rq]['last_lose']=='是':
                    data1.append('是,减重'+str(v[rq]['last_lose_weight'])+'公斤。')
                elif 'last_lose-Comment' in v[rq].keys():
                    data1.append('否。'+v[rq]['last_lose-Comment'])
                else:
                    data1.append('否。')
        data2.append(tuple(data1))
            
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)" 
sql = sql + " VALUES (%s,%s, %s, %s,%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s, %s, %s, %s)"
cur.executemany(sql, data2)
conn.commit()

# 关闭连接
cur.close()
conn.close()
In [ ]: