Reputation: 1508
I am using Jupyter Notebook. Is there a possibility to save all the output in a PDF-file?
For example, I have this code:
print('Hello World')
Which outputs:
Hello World
I just want to save the output as PDF but not the code. Can I do this somehow automated?
Background: I wrote an analytics report using a loop which outputs some graphs, equations and other printed statements. I am now looking for a solution to save this outputed results somehow. I saw that fpdf
seems to be a possibility but I could not install it. So, I would like to got for another solution.
Thanks in advance!
Upvotes: 1
Views: 1351
Reputation: 3005
You can try this
Step 1
Save the notebook, using the below snippet or from the Jupyter notebook
from IPython.display import Javascript
from nbconvert import HTMLExporter
import codecs
import nbformat
def save_notebook():
display(
Javascript("IPython.notebook.save_notebook()"),
include=['application/javascript']
)
Step 2
Convert the notebook to HTML excluding the Code cells
def output_HTML(read_file, output_file):
exporter = HTMLExporter()
# read_file is '.ipynb', output_file is '.html'
output_notebook = nbformat.read(read_file, as_version=4)
output, resources = exporter.from_notebook_node(output_notebook)
codecs.open(output_file, 'w', encoding='utf-8').write(output)
with open(output_file, 'r') as html_file:
content = html_file.read()
# Get rid off prompts and source code
content = content.replace("div.input_area {","div.input_area {\n\tdisplay: none;")
content = content.replace(".prompt {",".prompt {\n\tdisplay: none;")
f = open(output_file, 'w')
f.write(content)
f.close()
Usage
Save notebook
import time
save_notebook()
time.sleep(10)
current_file = 'Your Python Notebook.ipynb'
output_file = 'Output file Name.html'
Call the output_HTML
function
output_HTML(current_file, output_file)
Next, you can convert the HTML output to pdf either from the browser(if you are using Chrome)
It may not be an efficient solution, but can be done in this way also.
Upvotes: 1