How to convert IPython notebooks to PDF and HTML?

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

    You can use this simple online service. It supports both HTML and PDF.

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

    nbconvert is not yet fully replaced by nbconvert2, you can still use it if you wish, otherwise we would have removed the executable. It's just a warning that we do not bugfix nbconvert1 anymore.

    The following should work :

    ./nbconvert.py --format=pdf yourfile.ipynb 
    

    If you are on a IPython recent enough version, do not use print view, just use the the normal print dialog. Graph beeing cut in chrome is a known issue (Chrome does not respect some print css), and works much better with firefox, not all versions still.

    As for nbconvert2, it still highly dev and docs need to be written.

    Nbviewer use nbconvert2 so it's pretty decent with HTML.

    List of current available profiles:

    $ ls -l1 profile|cut -d. -f1
    
    base_html
    blogger_html
    full_html
    latex_base
    latex_sphinx_base
    latex_sphinx_howto
    latex_sphinx_manual
    markdown
    python
    reveal
    rst
    

    Give you the existing profiles. (You can create your own, cf future doc, ./nbconvert2.py --help-all should give you some option you can use in your profile.)

    then

    $ ./nbconvert2.py [profilename] --no-stdout --write=True <yourfile.ipynb>
    

    And it should write your (tex) files as long as extracted figures in cwd. Yes I know this is not obvious, and it will probably change hence no doc...

    The reason for that is that nbconvert2 will mainly be a python library where in pseudo code you can do :

     MyConverter = NBConverter(config=config)
     ipynb = read(ipynb_file)
     converted_files = MyConverter.convert(ipynb)
     for file in converted_files :
         write(file)
    

    Entry point will come later, once the API is stabilized.

    I'll just point out that @jdfreder (github profile) is working on tex/pdf/sphinx export and is the expert to generate PDF from ipynb file at the time of this writing.

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

    From the docs:

    If you want to provide others with a static HTML or PDF view of your notebook, use the Print button. This opens a static view of the document, which you can print to PDF using your operating system’s facilities, or save to a file with your web browser’s ‘Save’ option (note that typically, this will create both an html file and a directory called notebook_name_files next to it that contains all the necessary style information, so if you intend to share this, you must send the directory along with the main html file).

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

    Only this answer would be useful to you if you have math, scientific formulae in your document. Even if you don't have them it works fine.

    • Install LaTeX
    • Install nbconvert

    GUI way

    • open the jupyter notebook
    • Go to Files > Download as > HTML or PDF via LaTeX

    • Then check your Downloads folder for the file. PS: If LaTeX had any errors while compiling the PDF, it will fail. If this happens, download the HTML file and then use http://webpagetopdf.com/ or any other similar service to convert the HTML to PDF.

    Command-Line way

    • Open the terminal
    • Navigate to the folder containing the jupyter notebook
    • type "jupyter nbconvert --to pdf your_jupyter_notebook.ipynb " PS: If it fails, try Yogesh's answer
    0 讨论(0)
  • If you are using sagemath cloud version, you can simply go to the left corner,
    select File → Download as → Pdf via LaTeX (.pdf)
    Check the screenshot if you want.
    Screenshot Convert ipynb to pdf

    If it dosn't work for any reason, you can try another way.
    select File → Print Preview and then on the preview
    right click → Print and then select save as pdf.

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

    Other suggested approaches:

    1. Using the 'Print and then select save as pdf.' from your HTML file will result in loss of border edges, highlighting of syntax, trimming of plots etc.

    2. Some other libraries have shown to be broken when it comes to using obsolete versions.

    Solution: A better, hassle-free option is to use an online converter which will convert the *.html version of your *.ipynb to *.pdf.

    Steps:

    1. First, from your Jupyter notebook interface, convert your *.ipynb to *.html using:

    File > Download as > HTML(.html)

    1. Upload the newly created *.html file here and then select the option HTML to PDF.

    2. Your pdf file is now ready for download.

    3. You now have .ipynb, .html and .pdf files

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