Skip to content

Instantly share code, notes, and snippets.

@wcchin
Created October 7, 2016 06:00
Show Gist options
  • Save wcchin/d25dea4c7f68acedb6d923f67bb0549c to your computer and use it in GitHub Desktop.
Save wcchin/d25dea4c7f68acedb6d923f67bb0549c to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
## EXPORTING TO PDF FROM revealjs OR jupyter notebook slides
## using nbconvert and decktape (https://github.com/astefanutti/decktape)
## to export pdf and/or html(revealjs)
## from jupyter notebook / revealjs html
## phantomjs must be included in path, and decktape directory must be place beside this export_reveal.py file
## for more detail, please check:
## nbconvert - https://github.com/jupyter/nbconvert
## decktape - https://github.com/astefanutti/decktape
import io
import sys
import subprocess
import argparse
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument("--notebook", type=str, help="take .ipynb as input")
parser.add_argument("--html", type=str, help="take .html as input")
parser.add_argument("-o","--output", help="output_directories/filename of pdf", type=str)
parser.add_argument("-c","--custom", help="customizing", action='store_true')
parser.add_argument("--trans", help="customizing transition", type=str)
parser.add_argument("--theme", help="customizing theme", type=str)
args = parser.parse_args()
pdffile = args.output
if not(pdffile is None):
if not('.pdf' in pdffile):
pdffile = pdffile + '.pdf'
cusz = args.custom
trans = None
theme = None
if not(args.trans is None):
cusz = True
trans = args.trans
if not(args.theme is None):
cusz = True
theme = args.theme
if not(args.notebook is None):
notefile = args.notebook
export_tohtml(notefile)
htmlfile = notefile[:-6]+'.slides.html'
if cusz:
custom_reveal(htmlfile, transition=trans, theme=theme)
if pdffile is None:
pdffile = notefile[:-6]+'.slides.pdf'
export_topdf(htmlfile, pdffile)
elif not(args.html is None):
htmlfile = args.html
if cusz:
custom_reveal(htmlfile, transition=trans, theme=theme)
if pdffile is None:
pdffile = htmlfile[:-4]+'pdf'
export_topdf(htmlfile, pdffile)
else:
print "You must provide either notebook file (--notebook xx.ipynb) "
print "or revealjs slides' html file (--html xx.html)"
print "For help: python export_reveal.py -h"
def export_tohtml(notefile):
bashCommand = "jupyter nbconvert --to slides "+notefile
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
def export_topdf(htmlfile, pdffile):
bashCommand = "phantomjs decktape/decktape.js reveal "+htmlfile+" "+pdffile
print 'writing slides to ', pdffile
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
def custom_reveal(htmlfile, transition=None, theme=None):
if theme is None:
theme_txt = '/'+'sky'+'.css"'
else:
theme_txt = '/'+theme+'.css"'
if transition is None:
trans_txt = ".transition || '"+'concave'+"'"
else:
trans_txt = ".transition || '"+transition+"'"
with io.open(htmlfile, 'r') as in_file:
data = in_file.readlines()
for i, line in enumerate(data):
if 'href="//' in data[i]:
data[i] = data[i].replace('href="//', 'href="http://')
if '/simple.css"' in data[i]:
data[i] = data[i].replace('/simple.css"', theme_txt)
if ".transition || 'linear'" in data[i]:
data[i] = data[i].replace(".transition || 'linear'", trans_txt)
with io.open(htmlfile, 'w') as out_file:
out_file.writelines(data)
print "slides is customized"
if __name__ == '__main__':
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment