-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakepdf.py
executable file
·43 lines (38 loc) · 966 Bytes
/
makepdf.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
import argparse
import os
import subprocess
try:
from PyPDF2 import PdfFileMerger
MERGE = True
except ImportError:
print("Could not find PyPDF2. Leaving pdf files unmerged.")
MERGE = False
def main(files):
os_args = [
'jupyter',
'nbconvert',
'--log-level',
'CRITICAL',
'--to',
'pdf',
]
for f in files:
os_args.append(f)
subprocess.run(os_args)
print("Created PDF {}.".format(f))
if MERGE:
pdfs = [f.split('.')[0] + ".pdf" for f in files]
merger = PdfFileMerger()
for pdf in pdfs:
merger.append(pdf)
merger.write("assignment.pdf")
merger.close()
for pdf in pdfs:
os.remove(pdf)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# we pass in explicit notebook arg so that we can provide
# an ordered list and produce an ordered pdf
parser.add_argument("--notebooks", type=str, nargs='+', required=True)
args = parser.parse_args()
main(args.notebooks)