How to convert IPython notebooks to PDF and HTML?

前端 未结 18 1306
有刺的猬
有刺的猬 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:21

    I can't get pdf to work yet. The docs imply I should be able to get it to work with latex, so maybe my latex is not working. http://ipython.org/ipython-doc/rel-1.0.0/interactive/nbconvert.html $ ipython --version 1.1.0 $ ipython nbconvert --to latex --post PDF myfile.ipynb [NbConvertApp] ... raise child_exception OSError: [Errno 2] No such file or directory $ ipython nbconvert --to pdf myfile.ipynb [NbConvertApp] CRITICAL | Bad config encountered during initialization: [NbConvertApp] CRITICAL | The 'export_format' trait of a NbConvertApp instance must be any of ['custom', 'html', 'latex', 'markdown', 'python', 'rst', 'slides'] or None, but a value of u'pdf' was specified.

    However, HTML works great using 'slides', and it is beautiful! $ ipython nbconvert --to slides myfile.ipynb ... [NbConvertApp] Writing 215220 bytes to myfile.slides.html

    //Update 2014-11-07Fri.: The IPython v3 syntax differs, it is simpler; $ ipython nbconvert --to PDF myfile.ipynb In all cases, it appears that I was missing the library 'pdflatex'. I'm investigating that.

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

    You can do it by 1st converting the notebook into HTML and then into PDF format:

    Following steps I have implemented on: OS: Ubuntu, Anaconda-Jupyter notebook, Python 3

    1 Save Notebook in HTML format:

    1. Start the jupyter notebook that you want to save in HTML format. First save the notebook properly so that HTML file will have a latest saved version of your code/notebook.

    2. Run the following command from the notebook itself:

      !jupyter nbconvert --to html your_notebook_name.ipynb

    After execution will create HTML version of your notebook and will save it in the current working directory. You will see one html file will be added into the current directory with your_notebook_name.html name

    (your_notebook_name.ipynb --> your_notebook_name.html).

    2 Save html as PDF:

    1. Now open that your_notebook_name.html file (click on it). It will be opened in a new tab of your browser.
    2. Now go to print option. From here you can save this file in pdf file format.

    Note that from print option we also have the flexibility of selecting a portion of a notebook to save in pdf format.

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

    I've been searching for a way to save notebooks as html, since whenever I try to download as html with my new Jupyter installation, I always get a 500 : Internal Server Error The error was: nbconvert failed: validate() got an unexpected keyword argument 'relax_add_props' error. Oddly enough, I've found that downloading as html is as simple as:

    1. Left click in the notebook
    2. Click 'Save As...' in the dropdown menu
    3. Save accordingly

    No print preview, no print, no nbconvert. Using Jupyter Version: 1.0.0. Just a suggestion to try (obviously not all setups are the same).

    0 讨论(0)
  • 2020-12-12 11:22
    1. Save as HTML ;
    2. Ctrl + P ;
    3. Save as PDF.
    0 讨论(0)
  • 2020-12-12 11:24

    The plain python version of partizanos's answer.

    • open Terminal (Linux, MacOS) or get to point where you can execute python files in Windows
    • Type the following code in a .py file (say tejas.py)
    import os
    
    [os.system("jupyter nbconvert --to pdf " + f) for f in os.listdir(".") if f.endswith("ipynb")]
    
    • Navigate to the folder containing the jupyter notebooks
    • Ensure that tejas.py is in the current folder. Copy it to the current folder if necessary.
    • type "python tejas.py"
    • Job done
    0 讨论(0)
  • 2020-12-12 11:25

    I combined some answers above in inline python that u can add to ~/.bashrc or ~/.zshrc to compile and convert many notebooks to a single pdf file

    function convert_notebooks(){
      # read anything on this folder that ends on ipynb and run pdf formatting for it  
      python -c 'import os; [os.system("jupyter nbconvert --to pdf " + f) for f in os.listdir (".") if f.endswith("ipynb")]'
      # to convert to pdf u must have installed latex and that means u have pdfjam installed
      pdfjam * 
    }
    
    0 讨论(0)
提交回复
热议问题