Skip to content

Instantly share code, notes, and snippets.

@PedroBern
Last active May 5, 2020 23:04
Show Gist options
  • Save PedroBern/2378b33bc7a66c1379d8865e7b1ab6c0 to your computer and use it in GitHub Desktop.
Save PedroBern/2378b33bc7a66c1379d8865e7b1ab6c0 to your computer and use it in GitHub Desktop.
Jupyter Notebook html2pdf
import sys
import re
import pdfkit
infile_name = sys.argv[1]
if len(sys.argv) > 2:
outfile_name = sys.argv[2]
else:
outfile_name = infile_name + "_2"
re_remove = [
"#notebook\-container\s+{.+(-webkit-box-shadow:[^;]+;)",
"#notebook\-container\s+{.+(box-shadow:[^;]+;)"
]
re_sub = [
("\/\* This needs to be wide enough for 3 digit prompt numbers: In\[100\]: \*\/",
"visibility: hidden;")
]
with open(outfile_name + ".html", 'w') as outfile:
with open(infile_name + ".html") as infile:
f = infile.read()
for r in re_remove:
f = re.sub(r, '', f, flags=re.DOTALL)
for r in re_sub:
f = re.sub(r[0], r[1], f, flags=re.DOTALL)
outfile.write(f)
pdfkit.from_file(outfile_name + ".html", outfile_name + ".pdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment