From d81d4a6db8b158d98c2807f3b9da89d402544524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20S=2E=20Martins?= Date: Tue, 17 May 2016 23:44:22 +0100 Subject: [PATCH] Add the capability to actually compile the generated tex file. Now it's a matter of improving the is_valid function and the looks of the written table. --- crossword_generator.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/crossword_generator.py b/crossword_generator.py index 2029080..2a0049e 100755 --- a/crossword_generator.py +++ b/crossword_generator.py @@ -9,6 +9,10 @@ if you're one of those people. # STL imports import random import pprint +import subprocess +import tempfile +import os +import shutil # Auxiliary Functions @@ -222,8 +226,28 @@ def write_grid(grid, screen=False, out_file="table.tex"): texfile.write("\end{tabular}\n") texfile.write("\end{document}\n") - # Compile - # TODO + # Compile in a temp folder + # (inspired by + # https://stackoverflow.com/questions/19683123/compile-latex-from-python) + if not screen: + with tempfile.TemporaryDirectory() as tmpdir: + # Save current directory + original_dir = os.getcwd() + + # Copy the latex source to the temporary directory + shutil.copy(out_file, tmpdir) + + # Move to the temp directory + os.chdir(tmpdir) + + # Rename .tex file to generic name "out" + os.rename(out_file, "out.tex") + + # Compile + proc = subprocess.call(['pdflatex', "out.tex"]) + + # Copy PDF back to the original directory + shutil.copy("out.pdf", original_dir) if __name__ == "__main__":