-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_pdf.py
More file actions
90 lines (70 loc) · 2.64 KB
/
generate_pdf.py
File metadata and controls
90 lines (70 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
""""Generates the latex files, the pdfs and cleans the directories"""
import os
import traceback
import sys
import shutil
TEMPLATE_DIR = 'templates'
RM_EXTENSIONS = ['.aux', '.latex', '.log', '.pdf']
TOPLEVELDIR = ''
def clean_templates():
"""Clean the templates directory"""
for subdir, dirs, files in os.walk(TEMPLATE_DIR):
for file in files:
ext = os.path.splitext(file)[-1].lower()
if ext in RM_EXTENSIONS:
os.remove(os.path.join(subdir, file))
def generate_latex_files():
"""Generates the latex files from the latex_template templates"""
for subdir, dirs, files in os.walk(TEMPLATE_DIR):
for file in files:
if file == "compile_template.py":
return_value = os.system(
"python3 \"" + os.path.join(subdir, file) + "\"")
if return_value != 0:
sys.exit(1)
def generate_pdf_files():
"""Generates the pdf files from the latex files"""
last_subdir = ""
for subdir, dirs, files in os.walk(TEMPLATE_DIR):
for file in files:
ext = os.path.splitext(file)[-1].lower()
if ext == '.latex':
if last_subdir != subdir:
last_subdir = subdir
os.chdir(os.path.abspath(subdir))
for i in range(0, 2):
return_value = os.system("xelatex -interaction=batchmode --halt-on-error \"" +
str(file) + "\"")
if return_value != 0:
sys.exit(1)
os.chdir(TOPLEVELDIR)
def move_pdf_files():
"""Moves the generated pdf files to output"""
for subdir, dirs, files in os.walk(TEMPLATE_DIR):
for file in files:
ext = os.path.splitext(file)[-1].lower()
if ext == '.pdf':
src = os.path.abspath(os.path.join(subdir, file))
dest = str(os.path.abspath(subdir)).replace(
"templates", "output")
print("src: " + src)
print("dest: " + dest)
shutil.copy(src, dest)
if __name__ == "__main__":
TOPLEVELDIR = os.getcwd()
try:
print("\nCleaning templates folder")
clean_templates()
print("\nGenerating latex files")
generate_latex_files()
os.chdir(TOPLEVELDIR)
print("\nGenerating pdf files")
generate_pdf_files()
os.chdir(TOPLEVELDIR)
print("\nMoving pdf files")
move_pdf_files()
print("\nCleaning templates folder")
clean_templates()
except:
traceback.print_exc()
sys.exit(1)