How to convert IPython notebooks to PDF and HTML?

前端 未结 18 1304
有刺的猬
有刺的猬 2020-12-12 10:49

I want to convert my ipython-notebooks to print them, or simply send them in html format. I have noticed that there exists a tool to do that already, nbconvert. Although I h

相关标签:
18条回答
  • 2020-12-12 11:02

    notebook-as-pdfInstall

    python -m pip install notebook-as-pdf pyppeteer-install

    Use it

    You can also use it with nbconvert:

    jupyter-nbconvert --to PDFviaHTML filename.ipynb

    which will create a file called filename.pdf.

    or pip install notebook-as-pdf

    create pdf from notebook jupyter-nbconvert-toPDFviaHTML

    0 讨论(0)
  • 2020-12-12 11:08

    If you have LaTeX installed you can download as PDF directly from Jupyter notebook with File -> Download as -> PDF via LaTeX (.pdf). Otherwise follow these two steps.

    1. For HTML output, you should now use Jupyter in place of IPython and select File -> Download as -> HTML (.html) or run the following command:

      jupyter nbconvert --to html notebook.ipynb  
      

      This will convert the Jupyter document file notebook.ipynb into the html output format.

      Google Colaboratory is Google's free Jupyter notebook environment that requires no setup and runs entirely in the cloud. If you are using Google Colab the commands are the same, but Google Colab only lets you download .ipynb or .py formats.

    2. Convert the html file notebook.html into a pdf file called notebook.pdf. In Windows, Mac or Linux, install wkhtmltopdf. wkhtmltopdf is a command line utility to convert html to pdf using WebKit. You can download wkhtmltopdf from the linked webpage, or in many Linux distros it can be found in their repositories.

      wkhtmltopdf notebook.html notebook.pdf   
      

    Original (now almost obsolete) revision: Convert the IPython notebook file to html.

    ipython nbconvert --to html notebook.ipynb

    0 讨论(0)
  • 2020-12-12 11:09

    The simplest way I think is 'Ctrl+P' > save as 'pdf'. That's it.

    0 讨论(0)
  • 2020-12-12 11:10

    Also pass the --execute flag to get the output

    jupyter nbconvert --execute --to html notebook.ipynb
    jupyter nbconvert --execute --to pdf notebook.ipynb
    

    The best practice is to keep the output out of the notebook for version control, see: Using IPython notebooks under version control

    But then, if you don't pass --execute, the output won't be present in the HTML, see also: How to run an .ipynb Jupyter Notebook from terminal?

    For an HTML fragment without header: How to export an IPython notebook to HTML for a blog post?

    Tested in Jupyter 4.4.0.

    0 讨论(0)
  • 2020-12-12 11:10

    For those who can't install wkhtmltopdf in their systems, one more method other than many already mentioned in the answers to this question is to simply download the file as an html file from the jupyter notebook, upload that to HTML to PDF, and download the converted pdf files from there.

    Here you have your IPython notebook(.ipynb) converted to both PDF(.pdf) & HTML(.html) formats.

    0 讨论(0)
  • I find that the easiest method to convert a notebook which is on the web to pdf is to first view it on the web service nbviewer. You can then print it to a pdf file. If the notebook is on your local drive, then upload it to a github repository first and use its url for nbviewer.

    0 讨论(0)
提交回复
热议问题