{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在生成:  周末卷01答案\n",
      "正在生成:  周末卷02答案\n",
      "正在生成:  周末卷03答案\n",
      "正在生成:  周末卷03_暂未使用答案\n",
      "正在生成:  周末卷04答案\n",
      "正在生成:  周末卷05答案\n",
      "正在生成:  周末卷06答案\n",
      "正在生成:  周末卷07答案\n",
      "正在生成:  周末卷08答案\n",
      "正在生成:  周末卷09答案\n",
      "正在生成:  周末卷10答案\n",
      "正在生成:  周末卷11答案\n",
      "正在生成:  周末卷12答案\n",
      "正在生成:  周末卷13答案\n",
      "正在生成:  周末卷14答案\n",
      "正在生成:  国庆卷答案\n",
      "编译 临时文件/23届高三上学期周末卷参考答案.tex 中\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "import os,re,json,zipfile\n",
    "\n",
    "file_dir = r\"C:\\Users\\weiye\\Documents\\wwy sync\\23届\\上学期周末卷\"\n",
    "filelist = []\n",
    "output_gloss_filename = \"23届高三上学期周末卷参考答案\"\n",
    "\n",
    "if len(filelist) == 0:\n",
    "    filelist = [f for f in os.listdir(file_dir) if \".tex\" in f]\n",
    "\n",
    "\n",
    "\n",
    "with open(\"../题库0.3/Problems.json\",\"r\",encoding = \"u8\") as f:\n",
    "    pro_dict = json.loads(f.read())\n",
    "\n",
    "output_data = \"\"\n",
    "\n",
    "for filename in filelist:\n",
    "    outfilename = os.path.split(filename)[1][:-4]+\"答案\"\n",
    "    with open(os.path.join(file_dir,filename),\"r\",encoding = \"u8\") as f:\n",
    "        data = f.read()\n",
    "\n",
    "    ids = re.findall(r\"\\((\\d{6})\\)\",data)\n",
    "    if len(ids) > 0:\n",
    "        print(\"正在生成: \",outfilename)\n",
    "        output_data += (\"\\n\\n\"+r\"\\section{\"+outfilename.replace(\"_\",\"-\")+\"}\\n\\n\")\n",
    "        output_data += (\"\\n\\n\"+r\"\\begin{enumerate}\"+\"\\n\\n\")\n",
    "        for id in ids:\n",
    "            problemset = pro_dict[id]\n",
    "            content = problemset[\"content\"]\n",
    "            solution = problemset[\"solution\"]\n",
    "            answer = \"\\\\textcolor{red}{\" + (problemset[\"ans\"] if problemset[\"ans\"] != \"\" else \"\\\\textcolor{blue}{暂无答案}\") + \"}\"\n",
    "            output_data += \"\\\\item \" + \"(\"+id+\") \" + content + \"\\n\\n\" + \"答案: \" + answer + \"\\n\\n\" \n",
    "        output_data += (\"\\n\\n\"+r\"\\end{enumerate}\"+\"\\n\\\\newpage\\n\\n\")\n",
    "\n",
    "with open(\"模板文件/试卷答案模板.txt\",\"r\",encoding = \"u8\") as f:\n",
    "    outlatex = f.read()\n",
    "\n",
    "outlatex = outlatex.replace(\"内容待替换\",output_data)\n",
    "outlatex = outlatex.replace(\"标题文字待处理\",output_gloss_filename.replace(\"_\",\"-\"))\n",
    "\n",
    "outfile = os.path.join(\"临时文件\",output_gloss_filename+\".tex\")\n",
    "with open(outfile,\"w\",encoding=\"u8\") as f:\n",
    "    f.write(outlatex)\n",
    "\n",
    "outfile = outfile.replace(\"\\\\\",\"/\")\n",
    "\n",
    "print(\"编译 \"+outfile+\" 中\")\n",
    "os.system(\"xelatex -interaction=batchmode -output-directory=临时文件 \" + outfile)\n",
    "print(os.system(\"xelatex -interaction=batchmode -output-directory=临时文件 \" + outfile))\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pythontest",
   "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": "91219a98e0e9be72efb992f647fe78b593124968b75db0b865552d6787c8db93"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
