forked from rambasnet/CPP-Fundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatConverter.py
85 lines (72 loc) · 3.05 KB
/
formatConverter.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
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
# program converts all notebook.ipynb files into given format
import os
import subprocess
import argparse
import shutil
def convert(inputFile, outputFile, format='pdf'):
print(inputFile, outputFile, format)
out = subprocess.Popen(['jupyter', 'nbconvert', '--to', format, inputFile, '--output', outputFile])
stdout, stderr = out.communicate()
return stderr
def convertToLatex(files, inputDir, outPutDir):
errors = []
if os.path.exists(outPutDir):
shutil.rmtree(outPutDir)
shutil.copytree('resources', os.path.join(outPutDir, 'resources'))
for inf in files:
outputFile = os.path.join(outPutDir, inf.split('.')[0])
error = convert(os.path.join(inputDir, inf), outputFile, 'latex')
if error:
errors.append(errors)
print(errors)
def convertToPdfs(outPutDir):
import subprocess
#outPutDir = '../pdfs/'
if os.path.exists(outPutDir):
shutil.rmtree(outPutDir)
os.mkdir('pdfs')
files = [f for f in os.listdir('latex') if f.endswith('.tex')]
fileExt = ['*.aux', '*.log', '*.out']
for f in files:
path = os.path.join('latex', f)
with open(path, 'r') as fin:
latex = fin.read()
latex = latex.replace('.ipynb', '.pdf')
with open(path, 'w') as fout:
fout.write(latex)
cmd = f'pdflatex -output-directory={outPutDir} {path}'
print(cmd)
process = subprocess.run(cmd.split())
#output, error = process.communicate()
#print(output)
subprocess.run(['clear'])
for ext in fileExt:
cmd = 'rm ' + os.path.join(outPutDir, ext)
print(cmd)
process = subprocess.run(cmd.split())
#process.communicate()
print('all done! see pdfs folder...')
def main():
parser = argparse.ArgumentParser(description="Notebook converter")
parser.add_argument('-id', '--inputDir', default=".", help="Input Folder; default is current folder the script is running from")
parser.add_argument('-f', '--format', default="pdf", help="conversion type; default is pdf other options: pdf, latex, html, webpdf, slides")
parser.add_argument('-od', '--outputDir', default="pdfs", help="Output folder to save the converted files; default is pdfs inside input folder.")
#parser.add_argument('-if', '--inputFile', help="Input notebook file to convert")
args = vars(parser.parse_args()) # convert Namespace to dict()
#print(args)
#print(os.listdir(args['inputFolder']))
files = [f for f in os.listdir(args['inputDir']) if f.endswith('.ipynb')]
errors = []
formatType = args['format']
outPutDir = os.path.join(args['inputDir'], args['outputDir'])
inputDir = args['inputDir']
if formatType == 'latex':
convertToLatex(files, inputDir, outPutDir)
elif formatType == 'pdf':
convertToLatex(files, inputDir, "latex")
convertToPdfs(outPutDir)
print(f'Total {len(files)} notebooks found in {args["inputDir"]} folder.')
print(f'Total {len(errors)} errors:')
print('\n'.join(errors))
if __name__ == "__main__":
main()