-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpdf_convert.py
53 lines (46 loc) · 1.78 KB
/
pdf_convert.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
53
"""
Script to convert notebooks to PDFs.
Ensure that you have the correct LaTeX packages installed on your machine
for nbconvert to use else you will recieve an OSError.
"""
import re
import os
import platform
import shutil
import subprocess
if not os.path.exists('pdfs'):
os.mkdir('pdfs')
for nb in os.listdir('notebooks'):
# Skip non-notebooks
if not re.search(r'\.ipynb$', nb):
continue
# Convert to PDF
system = platform.system()
if system == 'Windows':
process = subprocess.Popen([
'jupyter', 'nbconvert',
f'notebooks/{nb}',
'--to', 'pdf',
'--TagRemovePreprocessor.enabled=True',
"--TagRemovePreprocessor.remove_cell_tags=['remove_cell']",
"--TagRemovePreprocessor.remove_input_tags=['remove_input']",
"--TagRemovePreprocessor.remove_single_output_tags=['remove_single_output']",
"--TagRemovePreprocessor.remove_all_outputs_tags=['remove_all_output']",
], shell=True)
elif system in ('Linux', 'Darwin'):
process = subprocess.Popen(' '.join([
'jupyter nbconvert',
f"notebooks/{nb}",
'--to pdf',
'--TagRemovePreprocessor.enabled=True',
"--TagRemovePreprocessor.remove_cell_tags=\"['remove_cell']\"",
"--TagRemovePreprocessor.remove_input_tags=\"['remove_input']\"",
"--TagRemovePreprocessor.remove_single_output_tags=\"['remove_single_output']\"",
"--TagRemovePreprocessor.remove_all_outputs_tags=\"['remove_all_output']\"",
]), shell=True)
else:
raise NotImplementedError("build does currently not support system")
process.wait()
# Move file
pdf = nb.replace('ipynb', 'pdf')
shutil.move(f'notebooks/{pdf}', f'pdfs/{pdf}')