{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "#在 临时文件/赋能答题纸 目录中保留一个pdf(赋能试卷的答题纸), 不留别的pdf文件. \n",
    "#在 临时文件/赋能答题纸 目录中保留 赋能template.tex.\n",
    "#在 临时文件/赋能答题纸 目录中保留output目录.\n",
    "\"\"\"---设置文件名---\"\"\"\n",
    "filename = \"赋能12\"\n",
    "\n",
    "\"\"\"---设置题目列表---\"\"\"\n",
    "problems = r\"\"\"\n",
    "446:448,30500,450,451,30498,435,454,30499\n",
    "\"\"\"\n",
    "#完成后将含有 filename 的文件移至其它目录\n",
    "\n",
    "import os,re,json,shutil\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",
    "with open(\"../../题库0.3/Problems.json\",\"r\",encoding = \"utf8\") as f:\n",
    "    jsondata = f.read()\n",
    "pro_dict = json.loads(jsondata)\n",
    "\n",
    "#读取pdf文件并转化为png\n",
    "pdffile = \"临时文件/赋能答题纸/\"+[f for f in os.listdir(\"临时文件/赋能答题纸\") if \".pdf\" in f][0]\n",
    "try:\n",
    "    shutil.copy(pdffile,\"tempanswersheet.pdf\")\n",
    "except:\n",
    "    os.remove(\"tempanswersheet.pdf\")\n",
    "    shutil.copy(pdffile,\"tempanswersheet.pdf\")\n",
    "os.system(\"pdftocairo -png tempanswersheet.pdf\")\n",
    "try:\n",
    "    os.rename(\"tempanswersheet-1.png\",\"临时文件/赋能答题纸/output/\"+filename+\"答题纸.png\")\n",
    "except:\n",
    "    os.remove(\"临时文件/赋能答题纸/output/\"+filename+\"答题纸.png\")\n",
    "    os.rename(\"tempanswersheet-1.png\",\"临时文件/赋能答题纸/output/\"+filename+\"答题纸.png\")\n",
    "os.rename(pdffile,\"临时文件/赋能答题纸/\"+filename+\"答题纸_raw.pdf\")\n",
    "os.remove(\"tempanswersheet.pdf\")\n",
    "\n",
    "#替换tex模板中的内容\n",
    "problem_list = [id for id in generate_number_set(problems.strip(),pro_dict) if id in pro_dict]\n",
    "\n",
    "with open(\"临时文件/赋能答题纸/赋能template.tex\",\"r\",encoding=\"utf8\") as f:\n",
    "    texdata = f.read()\n",
    "texdata = texdata.replace(\"背景待替换\",filename+\"答题纸.png\")\n",
    "data_problems = \"\\n\\n\"\n",
    "for id in problem_list:\n",
    "    problemset = pro_dict[id]\n",
    "    problem = problemset[\"content\"]\n",
    "    data_problems += \"\\\\item \" + \"{\\\\tiny (\"+id+\")}\"+problem + \"\\n\\n\"\n",
    "texdata = texdata.replace(\"题目待替换\",data_problems)\n",
    "\n",
    "with open(\"临时文件/赋能答题纸/output/\"+filename+\".tex\",\"w\",encoding=\"utf8\") as f:\n",
    "    f.write(texdata)\n",
    "\n",
    "#编译文件\n",
    "os.chdir(\"临时文件/赋能答题纸/output\")\n",
    "os.system(\"xelatex -interaction=batchmode \" + filename +\".tex\")\n",
    "print(os.system(\"xelatex -interaction=batchmode \" +  filename +\".tex\"))\n",
    "os.chdir(\"../../..\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.15 ('mathdept')",
   "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.15"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "42dd566da87765ddbe9b5c5b483063747fec4aacc5469ad554706e4b742e67b2"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
