Last active
April 29, 2020 07:42
-
-
Save dyedgreen/df4b5325adb51db204be904d3cc54573 to your computer and use it in GitHub Desktop.
Generate exam submissions, following the naming convention for Physics at Imperial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate exam submissions | |
import os, shutil | |
from subprocess import run | |
cid = "0123456789" | |
def tex_template(**kwargs): | |
return """ | |
\\documentclass[a4paper]{{article}} | |
\\newcommand{{\\module}}{{{module}}} | |
\\newcommand{{\\question}}{{{question}}} | |
\\newcommand{{\\pdfname}}{{{pdf_file}}} | |
\\author{{CID: {cid}}} | |
\\title{{Module --- \\module{{}} \\\\ Question --- \\question}} | |
\\date{{\\today}} | |
\\usepackage{{pdfpages}} | |
\\usepackage{{tikz}} | |
\\usepackage[a4paper, total={{7in, 10.5in}}]{{geometry}} | |
\\begin{{document}} | |
\\maketitle | |
\\includepdf[pages=-,scale=1.0,pagecommand=\\subsection*{{CID: {cid}, Module: \\module, Question: \\question}}]{{\\pdfname}} | |
\\end{{document}} | |
""".format(cid=cid, **kwargs) | |
os.makedirs("input", exist_ok=True) | |
os.makedirs("output", exist_ok=True) | |
os.makedirs("tmp", exist_ok=True) | |
for exam in os.listdir("input"): | |
for question in os.listdir("input/{}".format(exam)): | |
with open("tmp/tmp.tex", "w") as tex: | |
tex.write( | |
tex_template( | |
module=exam, | |
question=question.split(".")[0], | |
pdf_file="../input/{}/{}".format(exam, question) | |
) | |
) | |
os.chdir("tmp") | |
run(["pdflatex", "tmp.tex"]) | |
os.chdir("..") | |
os.rename("tmp/tmp.pdf", "output/{}_{}_{}.pdf".format(cid, exam, question)) | |
shutil.rmtree("tmp") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use: Stick your answers into the
input
folder, in a folder for the exam. Runpython3 exams.py
.E.g.: