Skip to content

Instantly share code, notes, and snippets.

@dyedgreen
Last active April 29, 2020 07:42
Show Gist options
  • Save dyedgreen/df4b5325adb51db204be904d3cc54573 to your computer and use it in GitHub Desktop.
Save dyedgreen/df4b5325adb51db204be904d3cc54573 to your computer and use it in GitHub Desktop.
Generate exam submissions, following the naming convention for Physics at Imperial
# 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")
@dyedgreen
Copy link
Author

How to use: Stick your answers into the input folder, in a folder for the exam. Run python3 exams.py.

E.g.:

input
 | PHYSY3
 | | Q1.pdf
 | | Q2.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment