Files
jupyter/体测单位/天宫院社区.ipynb
T
2024-07-13 08:32:59 +08:00

18 KiB

导入人员信息

In [ ]:
import openpyxl
import json


wb = openpyxl.load_workbook('data/天宫院街道2024.xlsx')
sheet = wb.active
# sheets = wb.sheetnames
person = {}

for n in range(2, sheet.max_row+1):
    if sheet.cell(n,1).value is not None:
        code = int(sheet.cell(n, 1).value)
        person.setdefault(code, {})
        dict1 = {}
        dict1['name'] = sheet.cell(n, 2).value
        
        
        dict1['sex'] = sheet.cell(n,3).value
        birth = str(sheet.cell(n, 4).value).split()[0]
        dict1['birth'] = birth.replace('/','-')
        
        person[code] = dict1
        if sheet.cell(n,5).value is not None:
            dict1['phone'] = sheet.cell(n,5).value

        person[code] = dict1
filename = 'data/天宫院街道2024.json'
with open(filename, 'w') as fl:
    json.dump(person, fl, ensure_ascii=False)
print('ok')
In [8]:
import openpyxl
import json


wb = openpyxl.load_workbook('data/天宫院街道.xlsx')
sheet = wb.active
# sheets = wb.sheetnames
person = {}

for n in range(2, sheet.max_row+1):
    if sheet.cell(n,1).value is not None:
        code = int(sheet.cell(n, 1).value)
        person.setdefault(code, {})
        dict1 = {}
        dict1['name'] = sheet.cell(n, 2).value
        person[code] = dict1
filename = 'data/天宫院街道all.json'
with open(filename, 'w') as fl:
    json.dump(person, fl, ensure_ascii=False)
print('ok')
ok

生成手工记录插入格式

In [ ]:
import openpyxl
import json

filename = 'data/天宫院社区人员名单.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl) 
filename = 'data/天宫院手工记录成绩.xlsx'
wb = openpyxl.load_workbook(filename)
sheet = wb.active

s = ''
place = "587256"
sql = []
for n in range(2, sheet.max_row+1):
    if sheet.cell(n,1).value is not None:
        code = str(sheet.cell(n, 1).value)
        print(code)
        if dict1[code]['sex'] == '男':
            item = 5
        else:
            item = 9
        sj = '2023-04-20 22:00:00'
        s = f'({place},{code},{item},{sheet.cell(n, 7).value},"2023-04-20",0,"{sj}")'
        sql.append(s)
s = ','.join(sql)
print(s)

获取人员测试成绩

In [ ]:
import json
import time
import csv

filename = '../item.json'
item = {}
unit = {}
with open(filename,'r') as fl:
    dict1 = json.load(fl) 
for k,v in dict1.items():
    item[k] = v
re_ta = {}
dict1 = {}
list1 = []
filename = 'data/天宫院街道2024.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl) 

filename = 'data/places_result_20240710.csv'
with open(filename,'r',newline='') as csv_file:
    fl = csv.reader(csv_file,delimiter=',')
    header = next(fl)    
    for line in fl:
        #line = re.sub('[\r\n\f ]{1,}', '', line)
        list1.append(line)
#print(list1)
for result in list1:
    user = str(result[2])
    if user in dict1.keys():        
        m_item = str(result[3])    
        re_ta.setdefault(user,{})    
        re_ta[user]['name'] = dict1[user]['name']
        re_ta[user]['sex'] = dict1[user]['sex'] 
        item_name = item[m_item]['name']
        re_ta[user].setdefault(item_name,{})  
        score = int(result[4])/item[m_item]['divisor']    
        re_ta[user][item_name]['成绩'] = f'{score} {item[m_item]["unit"]}'
print(len(re_ta))
filename = 'data/result_天宫院街道2024.json'

with open(filename,'w') as fl:
    json.dump(re_ta, fl, ensure_ascii=False) 
print(len(re_ta))
In [9]:
import json
import time
import csv

filename = '../item.json'
item = {}
unit = {}
with open(filename,'r') as fl:
    dict1 = json.load(fl) 
for k,v in dict1.items():
    item[k] = v
re_ta = {}
dict1 = {}
list1 = []
filename = 'data/天宫院街道all.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl) 

filename = 'data/places_result_20240710.csv'
with open(filename,'r',newline='') as csv_file:
    fl = csv.reader(csv_file,delimiter=',')
    header = next(fl)    
    for line in fl:
        #line = re.sub('[\r\n\f ]{1,}', '', line)
        list1.append(line)
#print(list1)
for result in list1:
    user = str(result[2])
    if user in dict1.keys():        
        m_item = str(result[3])    
        re_ta.setdefault(user,{})    
        re_ta[user]['name'] = dict1[user]['name']
        item_name = item[m_item]['name']
        re_ta[user].setdefault(item_name,{})  
        score = int(result[4])/item[m_item]['divisor']    
        re_ta[user][item_name]['成绩'] = f'{score} {item[m_item]["unit"]}'
print(len(re_ta))
filename = 'data/result_天宫院街道all.json'

with open(filename,'w') as fl:
    json.dump(re_ta, fl, ensure_ascii=False) 
print(len(re_ta))
61
61

导出测试成绩

In [10]:
import json
import openpyxl

items = ['身高','体重','肺活量','握力','坐位体前屈','纵跳','俯卧撑','一分钟仰卧起坐','单脚站立','选择反应时','台阶指数']
title = ['编号','姓名','性别','身高','体重','肺活量','握力','坐位体前屈','纵跳','俯卧撑','一分钟仰卧起坐','单脚站立','选择反应时','台阶指数']
filename = 'data/result_天宫院街道.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 = []
    print(k,v['name'])
    list2.append(str(k).rjust(3,'0'))
    list2.append(v['name'])    
    list2.append(dict2[k]['sex'])
      
    
    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/天宫院社区体测情况表_all.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(title)
for row in list1:
    sheet.append(row)
    
wb.save(filename)
101 李先生
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[10], line 20
     18 list2.append(str(k).rjust(3,'0'))
     19 list2.append(v['name'])    
---> 20 list2.append(dict2[k]['sex'])
     23 for item in items:
     24     if item in v.keys():

KeyError: 'sex'
In [11]:
import json
import openpyxl

items = ['身高','体重','肺活量','握力','坐位体前屈','纵跳','俯卧撑','一分钟仰卧起坐','单脚站立','选择反应时','台阶指数']
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 = []
    print(k,v['name'])
    list2.append(str(k).rjust(3,'0'))
    list2.append(v['name'])  
    
    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/天宫院社区体测情况表_all.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(title)
for row in list1:
    sheet.append(row)
    
wb.save(filename)
101 李有才
44 星众豪
94 谢红印
14 孙一萂
132 梁正文
123 刘
93 李洪芝
71 李羽馨
91 孙婧祎
8 王红晶
126 马
125 陈
6 刘艳
96 薛颖
2 刘书本
40 杨春华
9 霍思诺
124 苗
128 张
78 朱蕊
10 李晨曦
5 李芳
100 聂彦芹
4 富红
68 冯瑞文
108 杨
130 徐
86 阎树芹
83 张万成
116 白景香
103 王梓涵
15 王慧兰
36 冯玉红
118 王玉芬
102 王岩东
19 张竹辰
38 路德慧
111 张
37 张影
25 孟现春
41 朱朝云
85 武宜国
107 刘连英
20 王韵琪
7 李雪
18 王真秀
98 张建霞
131 陈艳莉
106 沈建新
43 杨桂兰
127 张
39 韩润壮
129 薛
110 赵
79 郭忠臣
81 赵淑芬
115 朱
114 王淑云
62 赵秀香
31 张成林
99 张东喜

体测报告更名分组

In [ ]:
import os,sys,shutil
import json
import math
import openpyxl

fi_path = 'file/天宫院'
old = []
fl=os.listdir(fi_path)

for fn in fl:
    if os.path.isfile(fi_path + '/' + fn):
        ofn = int(fn.split('.')[0])
        old.append(ofn) 
old.sort()
for n in old:    
    o_name = f'{fi_path}/{n}.pdf'
    name = dict1[str(n)]['name']
    n_name = f'{m_path}/{str(n).rjust(3,"0")}-{name}.pdf'
    #print(o_name,n_name)
    if not os.path.exists(n_name):
        shutil.copyfile(o_name,n_name)
        print(n_name)

生成下载文件JSON文件

In [ ]:
import os,sys,shutil
import json
import math
import openpyxl

filename = 'data/天宫院社区人员名单.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
fi_path = 'file/天宫院'
old = []
fl=os.listdir(fi_path)

for fn in fl:
    if os.path.isfile(fi_path + '/' + fn):
        ofn = int(fn.split('.')[0])
        old.append(ofn) 
old.sort()
dict2 = {}
for n in old:
    if str(n) in dict1.keys() and 'phone' in dict1[str(n)].keys():
        dict2[n] = {'name':dict1[str(n)]['name'].strip(),'phone':str(dict1[str(n)]['phone']).strip()}
        
filename = 'data/tiangongyuan.json'
with open(filename,'w') as fl:
    json.dump(dict2, fl, ensure_ascii=False) 
print('ok') 
    
In [ ]: