forked from ngs-training/notebooks2pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_course_pdf.py
executable file
·52 lines (36 loc) · 1.58 KB
/
make_course_pdf.py
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
#!/usr/bin/env python3
import subprocess
import os
import sys
import argparse
import file_utils
parser = argparse.ArgumentParser(
description = 'Cats tex files. Uses header from first file listed.',
usage = '%(prog)s [options] <outprefix> <infile1, infile2, ...>')
parser.add_argument('--no_exec', action='store_true', help='Do not execute all commands in notebook')
parser.add_argument('outprefix', help='Prefix of output files')
parser.add_argument('infiles', nargs='+', help='Name of input ipynb files')
options = parser.parse_args()
tex_files = []
for filename in options.infiles:
outfile = options.outprefix + '.' + str(len(tex_files)) + '.tex'
tex_files.append(outfile)
print('Converting', filename, 'to temporary tex file', outfile)
converter = file_utils.Ipynb_to_tex_converter(filename, outfile, execute_notebook=(not options.no_exec))
converter.run()
assert len(tex_files) == len(options.infiles)
final_tex_file = options.outprefix + '.tex'
print(len(tex_files), 'files converted. Combining into to one tex file', final_tex_file)
catter = file_utils.Tex_catter(tex_files, final_tex_file)
catter.run()
final_pdf = options.outprefix + '.pdf'
print('Making final pdf', final_pdf)
try:
subprocess.check_output('lualatex --halt-on-error ' + final_tex_file, shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as error:
sys.exit('Error running latex')
to_delete = [options.outprefix + '.' + x for x in ['aux', 'log', 'out']]
to_delete.extend(tex_files)
print('Cleaning files:', *to_delete)
for filename in to_delete:
os.unlink(filename)