{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0616003B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0616003B_学生用_20221012.tex\n", "0\n", "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0616004B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0616004B_学生用_20221012.tex\n", "0\n", "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0617002B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0617002B_学生用_20221012.tex\n", "0\n", "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0617004B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0617004B_学生用_20221012.tex\n", "0\n", "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0617006B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0617006B_学生用_20221012.tex\n", "0\n", "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0617007B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0617007B_学生用_20221012.tex\n", "0\n", "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0619003B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0619003B_学生用_20221012.tex\n", "0\n", "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0619004B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0619004B_学生用_20221012.tex\n", "0\n", "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0619005B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0619005B_学生用_20221012.tex\n", "0\n", "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0620002B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0620002B_学生用_20221012.tex\n", "0\n", "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0620004B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0620004B_学生用_20221012.tex\n", "0\n", "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0620005B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0620005B_学生用_20221012.tex\n", "0\n", "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0623001B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0623001B_学生用_20221012.tex\n", "0\n", "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0623002B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0623002B_学生用_20221012.tex\n", "0\n", "开始编译教师版本pdf文件: 临时文件/批量生成题目/K0623004B_教师用_20221012.tex\n", "0\n", "开始编译学生版本pdf文件: 临时文件/批量生成题目/K0623004B_学生用_20221012.tex\n", "0\n" ] } ], "source": [ "import os,re,time,json,sys\n", "\n", "\"\"\"\n", "模板文件目录下 题目清单.tex 文件不能缺失\n", "\"\"\"\n", "\n", "\"\"\"---设置题目列表---\"\"\"\n", "#字典字段为文件名, 之后为内容的题号\n", "problems_dict = {\n", "\"K0616003B\":\"000204,004061,009211,009212,010496,010498,010505\",\n", "\"K0616004B\":\"009710,009711\",\n", "\"K0617002B\":\"000210,000690,004084\",\n", "\"K0617004B\":\"010499,010500\",\n", "\"K0617006B\":\"003475,004994,009209,009210,009988,010497\",\n", "\"K0617007B\":\"009712,010521,010522\",\n", "\"K0619003B\":\"000199,000202,000211,000212,000216,000364,000411,005136,009214,009218,009400,010507,010514,010519,010524\",\n", "\"K0619004B\":\"009868,010515,010520\",\n", "\"K0619005B\":\"009720\",\n", "\"K0620002B\":\"000352,004669,004995,010513\",\n", "\"K0620004B\":\"000217,000372,000402,009207,010511,010516,010518\",\n", "\"K0620005B\":\"010517\",\n", "\"K0623001B\":\"004196\",\n", "\"K0623002B\":\"000215,000394,009242,009731,010814,011332\",\n", "\"K0623004B\":\"000200,000201,000205,000213,000419,009399,009417,009732,010532\"\n", "\n", "}\n", "\n", "\"\"\"---设置题目列表结束---\"\"\"\n", "\n", "\"\"\"---设置文件保存路径---\"\"\"\n", "#目录和文件的分隔务必用/\n", "directory = \"临时文件/批量生成题目/\"\n", "\"\"\"---设置文件名结束---\"\"\"\n", "\n", "\n", "\n", "#生成数码列表, 逗号分隔每个区块, 区块内部用:表示整数闭区间\n", "def generate_number_set(string,dict):\n", " string = re.sub(r\"[\\n\\s]\",\"\",string)\n", " string_list = string.split(\",\")\n", " numbers_list = []\n", " for s in string_list:\n", " if not \":\" in s:\n", " numbers_list.append(s.zfill(6))\n", " else:\n", " start,end = s.split(\":\")\n", " for ind in range(int(start),int(end)+1):\n", " numbers_list.append(str(ind).zfill(6))\n", " return numbers_list\n", "\n", "#将正确率转化为含颜色代码的字符串\n", "def get_color(value):\n", " value = float(value)\n", " if value>=0.5:\n", " (r,g,b)=(1,2-2*value,0)\n", " else:\n", " (r,g,b)=(2*value,1,0)\n", " return \"{\" + \"%.3f\" %(r) + \",\" + \"%.3f\" %(g) + \",0}\"\n", "\n", "\n", "def color_value(matchobj):\n", " value = matchobj.group(1)\n", " return \"\\t\"+\"\\\\fcolorbox[rgb]{0,0,0}\"+ get_color(value) +\"{\" + value +\"}\"\n", "\n", "\n", "#读取题库json文件并转化为字典\n", "with open(r\"../题库0.3/Problems.json\",\"r\",encoding = \"utf8\") as f:\n", " database = f.read()\n", "pro_dict = json.loads(database)\n", "\n", "#读取目标数据库json并转化为字典\n", "with open(r\"../题库0.3/LessonObj.json\",\"r\",encoding = \"utf8\") as f:\n", " database = f.read()\n", "obj_dict = json.loads(database)\n", "\n", "try:\n", " os.mkdir(directory)\n", "except:\n", " pass\n", "\n", "#读取系统日期\n", "current_time = time.localtime()\n", "time_string = \"_\"+str(current_time.tm_year).zfill(4)+str(current_time.tm_mon).zfill(2)+str(current_time.tm_mday).zfill(2)\n", "\n", "for filename in problems_dict:\n", " problems = problems_dict[filename]\n", " teachers_latex_file = directory + filename + \"_教师用\" + time_string + \".tex\"\n", " students_latex_file = directory + filename + \"_学生用\" + time_string + \".tex\"\n", "\n", " #生成题目列表\n", " problem_list = [id for id in generate_number_set(problems.strip(),pro_dict) if id in pro_dict]\n", "\n", " data_teachers = \"\"\n", " data_students = \"\"\n", " id_list = \"\"\n", "\n", " #生成教师题目字符串与学生题目字符串, 准备替换至latex文件\n", " for id in problem_list:\n", " problemset = pro_dict[id]\n", " problem = problemset[\"content\"]\n", " solution = (problemset[\"solution\"] if problemset[\"solution\"] != \"\" else \"暂无解答与提示\")\n", " answer = \"\\\\textcolor{red}{\" + (problemset[\"ans\"] if problemset[\"ans\"] != \"\" else \"暂无答案\") + \"}\"\n", " remarks = (problemset[\"remark\"] if problemset[\"remark\"] != \"\" else \"暂无备注\")\n", " usages_list = problemset[\"usages\"]\n", " if len(usages_list) > 0:\n", " usage = re.sub(\"\\\\t([\\d]\\.[\\d]{0,10})\",color_value,\"\\n\\n\".join(usages_list))\n", " usage = re.sub(\"[\\\\t ]([\\d]\\.[\\d]{0,10})\",color_value,usage)\n", " else:\n", " usage = \"暂无使用记录\"\n", " origin = (problemset[\"origin\"] if problemset[\"origin\"] != \"\" else \"出处不详\")\n", " objects = problemset[\"objs\"]\n", " if len(objects) == 0:\n", " objects = \"暂未关联目标\\n\\n\"\n", " elif \"KNONE\" in [o.upper() for o in objects]:\n", " objects = \"该题的考查目标不在目前的集合中\\n\\n\"\n", " else:\n", " objects_string = \"\"\n", " for obj in objects:\n", " if not obj in obj_dict:\n", " objects_string = \"目标\" + obj + \"有误\\n\\n\"\n", " break\n", " else:\n", " objects_string += \"\\\\textcolor{blue}{\" + obj + \"|\" + obj_dict[obj][\"content\"] + \"}\\n\\n\"\n", " objects = objects_string\n", " space = (\"\" if problemset[\"space\"] == \"\" else r\"\\vspace*{\"+problemset[\"space\"]+\"}\\n\")\n", " tags = (\"|\".join(problemset[\"tags\"]) if len(problemset[\"origin\"])>0 else \"暂无标签\")\n", " raw_string = \"\\\\item \" + \"{\\\\tiny (\"+id+\")}\"+problem\n", " teachers_string = raw_string.replace(\"\\\\tiny\",\"\")+\"\\n\\n关联目标:\\n\\n\"+ objects + \"\\n\\n标签: \" + tags + \"\\n\\n答案: \"+answer + \"\\n\\n\" + \"解答或提示: \" + solution + \"\\n\\n使用记录:\\n\\n\"+ usage + \"\\n\" + \"\\n\\n出处: \"+origin + \"\\n\"\n", " students_string = raw_string + space + \"\\n\\n\"\n", " data_teachers += teachers_string\n", " data_students += students_string\n", "\n", "\n", "\n", " #替换latex文件的内容并编译\n", " with open(\"模板文件/题目清单.tex\",\"r\",encoding = \"utf8\") as f:\n", " latex_raw = f.read()\n", " #识别操作系统\n", " if sys.platform != \"win32\":\n", " latex_raw = re.sub(r\"fontset[\\s]*=[\\s]*windows\",\"fontset = fandol\",latex_raw)\n", " latex_raw = re.sub(r\"\\\\setCJKmainfont\",r\"% \\\\setCJKmainfont\",latex_raw)\n", " latex_teachers = latex_raw.replace(\"编译模板\",data_teachers)\n", " with open(teachers_latex_file,\"w\",encoding = \"utf8\") as f:\n", " f.write(latex_teachers)\n", " print(\"开始编译教师版本pdf文件: \", teachers_latex_file)\n", " os.system(\"xelatex -interaction=batchmode -output-directory=\" + directory + \" \"+ teachers_latex_file)\n", " print(os.system(\"xelatex -interaction=batchmode -output-directory=\" + directory + \" \"+ teachers_latex_file))\n", "\n", " latex_students = latex_raw.replace(\"编译模板\",data_students)\n", " with open(students_latex_file,\"w\",encoding = \"utf8\") as f:\n", " f.write(latex_students)\n", " print(\"开始编译学生版本pdf文件: \", students_latex_file)\n", " os.system(\"xelatex -interaction=batchmode -output-directory=\" + directory + \" \"+ students_latex_file)\n", " print(os.system(\"xelatex -interaction=batchmode -output-directory=\" + directory + \" \"+ students_latex_file))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.7 ('base')", "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.9.7" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "e4cce46d6be9934fbd27f9ca0432556941ea5bdf741d4f4d64c6cd7f8dfa8fba" } } }, "nbformat": 4, "nbformat_minor": 2 }