52 KiB
52 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 = 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)
person[code] = dict1
filename = 'data/宁夏能化干预人员.json'
with open(filename, 'w') as fl:
json.dump(person, fl, ensure_ascii=False)
print(len(person),'ok')In [ ]:
import json
import csv
import openpyxl
import time
from datetime import date
dict1 = {}
list1 = []
filename = 'data/sql_20251009.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[1]
phone = str(item[2])[2:]
birth = str(item[4])
content = json.loads(item[0])
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 = int(dict2['code'])
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]['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')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 [ ]:
import json
import openpyxl
title = []
filename = 'data/surveys_records_2025-12-07.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/宁夏能化干预人员问卷(20251207).xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(all_title)
for row in list1:
sheet.append(row)
wb.save(filename) In [ ]:
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/宁夏能化干预人员问卷(20251207).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)==7:
dict1['birth'] = birth+'-01'
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()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()In [ ]:
import json
import openpyxl
wb = openpyxl.load_workbook('data/宁夏能化干预人员问卷(20251207).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)==7:
dict1['birth'] = birth+'-01'
else:
dict1['birth'] = birth
dict1['unit'] = sheet.cell(n, 7).value
person[code] = dict1
#print(person)
filename = 'data/surveys_records_2025-12-07.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/宁夏能化干预人员问卷情况(20251207).json'
with open(filename,'w') as fl:
json.dump(person, fl, ensure_ascii=False)
print(len(person),'ok')
In [ ]:
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/宁夏能化干预人员问卷情况(20251207).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 [ ]: