Files
jupyter/体测单位/宁夏能化.ipynb
T
2025-12-15 08:11:54 +08:00

61 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 [30]:
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 [32]:
import openpyxl


filename = 'data/survey_宁夏能化高危风险干预人员.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/宁夏能化高危风险干预人员体质问卷明细表(20251209).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 [1]:
import json
import openpyxl


title = []

filename = 'data/surveys_records_2025-12-15.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/宁夏能化干预人员问卷(20251215).xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(all_title)
for row in list1:
    sheet.append(row)
    
wb.save(filename)        

问卷导入数据库

核对新增人员

In [2]:
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/宁夏能化干预人员问卷(20251215).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()
('梁正武', '男', '电气仪表中心', '1984-05-01', '02018801', 1)
('张艳萍', '女', '公用工程运行部', '1978-09-01', '02020548', 1)
('叶云飞', '男', '公用工程运行部', '1985-11-01', '02019460', 1)
('李效龙', '男', 'BDO运行部', '1987-09-01', '0201968', 1)
('何富来', '男', '宁夏能化', '1983-03-01', '02019507', 1)
('马荣', '女', '公用工程运行部', '1984-03-01', '02020205', 1)
('刘永清', '男', '热电运行部', '1968-10-01', '02019958', 1)
('马学锋', '男', '热电运行部', '1974-12-01', '02020025', 1)
('段庆霖', '男', '热电运行部', '1990-02-01', '02020062', 1)
('周卫星', '男', '热电运行部', '1970-05-01', '02019986', 1)
('姚户昌', '男', '热电运行部', '1970-12-01', '02019968', 1)
('姜德溪', '男', '热电运行部', '1970-01-01', '2019970', 1)
('陈海宝', '男', '乙炔运行部', '1990-07-01', '02019346', 1)
('丁红忠', '男', '热电运行部', '1990-09-01', '02020160', 1)
('孙柏', '男', '环保建材运行部', '1982-03-01', '02020446', 1)
('黄博', '男', '物资采购中心', '1995-12-01', '03391681', 1)
('王正东', '男', '热电运行部', '1990-01-01', '03428864', 1)
17

导入问卷信息(已作废)

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 [3]:
import json
import openpyxl

wb = openpyxl.load_workbook('data/宁夏能化干预人员问卷(20251215).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)
    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.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
    dict1['unit'] = sheet.cell(n, 7).value
    person[code] = dict1
#print(person)

filename = 'data/surveys_records_2025-12-10.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/宁夏能化干预人员问卷情况(20251215).json'

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

问卷信息汇总导入数据库

In [4]:
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/宁夏能化干预人员问卷情况(20251215).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 [ ]: