Files
jupyter/体测单位/天津石化.ipynb
T
2022-11-03 20:10:34 +08:00

435 lines
12 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "markdown",
"id": "ae533fe9-e20e-4e44-b9bb-030c4fa4e714",
"metadata": {},
"source": [
"## 体测人员导入"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "1144aaac-92a0-4bcf-aba7-096f7dd3ad3b",
"metadata": {
"execution": {
"iopub.execute_input": "2022-11-03T11:59:47.933493Z",
"iopub.status.busy": "2022-11-03T11:59:47.932972Z",
"iopub.status.idle": "2022-11-03T11:59:49.278241Z",
"shell.execute_reply": "2022-11-03T11:59:49.276741Z",
"shell.execute_reply.started": "2022-11-03T11:59:47.933444Z"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ok\n"
]
}
],
"source": [
"import openpyxl\n",
"import json\n",
"\n",
"\n",
"wb = openpyxl.load_workbook('data/天津石化员工检测花名册 (20221014).xlsx')\n",
"sheet = wb.active\n",
"# sheets = wb.sheetnames\n",
"person = {}\n",
"\n",
"for n in range(2, sheet.max_row+1):\n",
" code = int(sheet.cell(n, 6).value)\n",
" person.setdefault(code, {})\n",
" dict1 = {}\n",
" dict1['name'] = sheet.cell(n, 3).value\n",
" dict1['sex'] = sheet.cell(n, 4).value\n",
" dict1['unit'] = sheet.cell(n, 1).value\n",
" dict1['sub_unit'] = sheet.cell(n, 2).value\n",
" if sheet.cell(n,5).value is not None:\n",
" dict1['id_num'] = sheet.cell(n,5).value\n",
" person[code] = dict1\n",
"filename = 'data/天津石化人员名单.json'\n",
"with open(filename, 'w') as fl:\n",
" json.dump(person, fl, ensure_ascii=False)\n",
"print('ok')"
]
},
{
"cell_type": "markdown",
"id": "5e648b69-40bf-4494-9079-e4e2b6ab8866",
"metadata": {},
"source": [
"## 获取人员测试成绩"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "a57ebb22-543c-4804-8d44-89984058ec1d",
"metadata": {
"execution": {
"iopub.execute_input": "2022-11-03T12:04:25.033604Z",
"iopub.status.busy": "2022-11-03T12:04:25.033082Z",
"iopub.status.idle": "2022-11-03T12:04:25.402569Z",
"shell.execute_reply": "2022-11-03T12:04:25.401118Z",
"shell.execute_reply.started": "2022-11-03T12:04:25.033556Z"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ok\n"
]
}
],
"source": [
"import json\n",
"import time\n",
"import csv\n",
"\n",
"filename = '../item.json'\n",
"item = {}\n",
"unit = {}\n",
"with open(filename,'r') as fl:\n",
" dict1 = json.load(fl) \n",
"for k,v in dict1.items():\n",
" item[k] = v\n",
"#SQL语句为:\n",
"# SELECT a.item_id,a.performance,a.score,a.date AS DATE1,a.avatar_id,b.unit,b.name FROM places_result AS a,_zgshhgxs AS b WHERE a.place_id=135 AND a.avatar_id=b.id AND a.avatar_id < 4999\n",
"\n",
"re_ta = {}\n",
"dict1 = {}\n",
"list1 = []\n",
"#print(\"\\n运动项目信息:\")\n",
"filename = 'data/134_2210.csv'\n",
"with open(filename,'r',newline='') as csv_file:\n",
" fl = csv.reader(csv_file,delimiter=',')\n",
" header = next(fl) \n",
" for line in fl:\n",
" #line = re.sub('[\\r\\n\\f ]{1,}', '', line)\n",
" list1.append(line)\n",
"#print(list1)\n",
"for result in list1:\n",
" user = str(result[4])\n",
" m_item = str(result[0]) \n",
" re_ta.setdefault(user,{}) \n",
" re_ta[user]['name'] = str(result[6])\n",
" re_ta[user]['unit'] = str(result[5]) \n",
" item_name = item[m_item]['name']\n",
" re_ta[user].setdefault(item_name,{}) \n",
" score = int(result[1])/item[m_item]['divisor'] \n",
" re_ta[user][item_name]['成绩'] = f'{score} {item[m_item][\"unit\"]}'\n",
" re_ta[user][item_name]['得分'] =result[2]\n",
"filename = 'data/result_天津.json'\n",
"with open(filename,'w') as fl:\n",
" json.dump(re_ta, fl) \n",
"print('ok')"
]
},
{
"cell_type": "markdown",
"id": "55d961e5-a512-44b2-808e-7c55d7000d52",
"metadata": {},
"source": [
"## 导出测试成绩"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "38397ad4-2bb3-4b5e-8ece-9ea11e801890",
"metadata": {
"execution": {
"iopub.execute_input": "2022-11-03T12:04:34.987311Z",
"iopub.status.busy": "2022-11-03T12:04:34.986720Z",
"iopub.status.idle": "2022-11-03T12:04:36.000903Z",
"shell.execute_reply": "2022-11-03T12:04:35.999784Z",
"shell.execute_reply.started": "2022-11-03T12:04:34.987262Z"
},
"tags": []
},
"outputs": [],
"source": [
"import json\n",
"import openpyxl\n",
"\n",
"items = ['身高','体重','肺活量','握力','坐位体前屈','纵跳','俯卧撑','一分钟仰卧起坐','单脚站立','选择反应时','台阶指数']\n",
"title = ['编号','姓名','性别','单位/部门','车间/科室','身高','体重','肺活量','握力','坐位体前屈','纵跳','俯卧撑','一分钟仰卧起坐','单脚站立','选择反应时','台阶指数']\n",
"filename = 'data/result_天津.json'\n",
"with open(filename,'r') as fl:\n",
" dict1 = json.load(fl)\n",
"\n",
"filename = 'data/天津石化人员名单.json'\n",
"with open(filename,'r') as fl:\n",
" dict2 = json.load(fl)\n",
" \n",
"list1 = []\n",
"for k, v in dict1.items():\n",
" list2 = []\n",
" list2.append(str(k).rjust(8,'0'))\n",
" list2.append(v['name']) \n",
" list2.append(dict2[k]['sex'])\n",
" list2.append(dict2[k]['unit'])\n",
" list2.append(dict2[k]['sub_unit'])\n",
" \n",
" for item in items:\n",
" if item in v.keys():\n",
" list2.append(v[item]['成绩'])\n",
" \n",
" elif item =='name':\n",
" list2.append(v[item])\n",
" else:\n",
" list2.append('') \n",
" \n",
" list1.append(list2)\n",
"filename = 'data/天津石化体测情况表(截至20221103).xlsx'\n",
"wb = openpyxl.Workbook()\n",
"sheet = wb.active\n",
"sheet.append(title)\n",
"for row in list1:\n",
" sheet.append(row)\n",
" \n",
"wb.save(filename)"
]
},
{
"cell_type": "markdown",
"id": "1c7b219f-43cd-4bf0-971f-2c398f8db8ed",
"metadata": {},
"source": [
"## 按照日期进行报告分类"
]
},
{
"cell_type": "markdown",
"id": "3d57ff66-69c3-4c13-811b-f1e13b404077",
"metadata": {},
"source": [
"### 按照体测明细分类"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29c0f15b-1345-4b46-ac1d-b45ddede9641",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import json\n",
"import time\n",
"import csv\n",
"import os,sys,shutil\n",
"import glob\n",
"\n",
"dict1 = {}\n",
"list1 = []\n",
"filename = 'data/134_2210.csv'\n",
"with open(filename,'r',newline='') as csv_file:\n",
" fl = csv.reader(csv_file,delimiter=',')\n",
" header = next(fl) \n",
" for line in fl:\n",
" #line = re.sub('[\\r\\n\\f ]{1,}', '', line)\n",
" list1.append(line)\n",
"for result in list1:\n",
" user = str(result[4])\n",
" dict1.setdefault(user,'2022-10-01')\n",
" m_date = result[7]\n",
" if m_date> dict1[user]:\n",
" dict1[user] = m_date\n",
"rq = set() \n",
"for k, v in dict1.items():\n",
" rq.add(str(v).split(' ')[0].replace('-', '', 2))\n",
"# 创建目录\n",
"m_path = 'file/134'\n",
"for pn in rq:\n",
" if not os.path.exists(m_path + '/' + pn):\n",
" os.mkdir(m_path + '/' + pn)\n",
"fls = glob.glob(f'file/*.pdf')\n",
"for fn in fls:\n",
" #old = os.path.basename(fn).split('.')[0].rjust(8,'0') \n",
" old = os.path.basename(fn).split('.')[0]\n",
" mrq = str(dict1[old]).split(' ')[0].replace('-', '', 2)\n",
" n_name = f'{m_path}/{mrq}/{str(old).rjust(8,\"0\")}_{mrq}.pdf'\n",
" if not os.path.exists(n_name):\n",
" shutil.copyfile(fn,n_name)\n",
"print('ok!')"
]
},
{
"cell_type": "markdown",
"id": "ae2b17bd-38b9-4809-affb-01441a5581eb",
"metadata": {},
"source": [
"### 按照报告生成日期分类"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb24c1be-b5d3-4e61-a432-854a887746d4",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import json\n",
"import time\n",
"import csv\n",
"import os,sys,shutil\n",
"import glob\n",
"\n",
"dict1 = {}\n",
"list1 = []\n",
"\n",
"m_path = 'file/134'\n",
"mrq = '20221028'\n",
"if not os.path.exists(m_path + '/' + mrq):\n",
" os.mkdir(m_path + '/' + mrq)\n",
"fls = glob.glob(f'file/new/*.pdf')\n",
"for fn in fls:\n",
" #old = os.path.basename(fn).split('.')[0].rjust(8,'0') \n",
" old = os.path.basename(fn).split('.')[0] \n",
" n_name = f'{m_path}/{mrq}/{str(old).rjust(8,\"0\")}_{mrq}.pdf'\n",
" if not os.path.exists(n_name):\n",
" shutil.copyfile(fn,n_name)\n",
"print('ok!')"
]
},
{
"cell_type": "markdown",
"id": "838ec7d3-137c-4ccd-9f16-7b6312d1ffa6",
"metadata": {},
"source": [
"### PDF文件压缩"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a6ab0943-b018-4dd6-9176-85a762f9f60c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import fitz\n",
"from pdf2image import convert_from_path, convert_from_bytes\n",
"import os,sys\n",
"import tempfile\n",
"from pdf2image.exceptions import (\n",
" PDFInfoNotInstalledError,\n",
" PDFPageCountError,\n",
" PDFSyntaxError\n",
")\n",
"import img2pdf \n",
"import glob\n",
"import shutil\n",
"\n",
"def covert2pic(old_fn):\n",
" if os.path.exists('.pdf'): # 临时文件,需为空\n",
" shutil.rmtree('.pdf')\n",
" os.mkdir('.pdf')\n",
" with tempfile.TemporaryDirectory() as path:\n",
" images_from_path = convert_from_path(old_fn, dpi=100,fmt='jpg', output_folder='.pdf')\n",
"\n",
"def pic2pdf(new_fn):\n",
" fl1=glob.glob('.pdf/*.jpg')\n",
" fl1.sort()\n",
" a4inpt = (img2pdf.mm_to_pt(210),img2pdf.mm_to_pt(297))\n",
" layout_fun = img2pdf.get_layout_fun(a4inpt)\n",
" with open(new_fn,\"wb\") as f:\n",
" f.write(img2pdf.convert(fl1,layout_fun=layout_fun))\n",
" print(f'{new_fn}转换成功!')\n",
" \n",
"\n",
"\n",
"def pdfz(sor, obj, zoom): \n",
" covert2pic(zoom)\n",
" pic2pdf(obj)\n",
" \n",
"fi_path = 'file/134/20221028/'\n",
"fl = glob.glob(f'{fi_path}*.pdf')\n",
"\n",
"for fn in fl:\n",
" new_fn = fi_path+'new/'+os.path.basename(fn)\n",
" covert2pic(fn)\n",
" pic2pdf(new_fn)\n",
" shutil.rmtree('.pdf')\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "72c693e1-dd7c-40fa-8948-4fd6fbea8ddc",
"metadata": {},
"source": [
"### 区分新文件"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d06d4f0-364b-438c-844b-7d5badaf3496",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import os,sys,shutil\n",
"import glob\n",
"import time\n",
"\n",
"fi_path = 'file/'\n",
"fls = glob.glob(f'{fi_path}*.pdf')\n",
"m_date = time.strptime('2022-10-28','%Y-%m-%d')\n",
"for fn in fls:\n",
" c_time = time.gmtime(os.path.getctime(fn))\n",
" if c_time > m_date:\n",
" n_name = f'{fi_path}new/{os.path.basename(fn)}'\n",
" if not os.path.exists(n_name):\n",
" shutil.copyfile(fn,n_name)\n",
" print(n_name)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "32b61559-4495-4fbe-8eb6-2614e6225b5c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}