IPython / Jupyter将笔记本保存为PDF的问题

问题描述 投票:62回答:13

所以,我一直试图将一个jupyter笔记本保存为PDF,但我无法弄清楚如何做到这一点。我尝试的第一件事是从文件菜单下载PDF格式,但这样做会导致:

nbconvert failed: PDF creating failed

我尝试的下一件事是尝试从这样的命令提示符进行转换

$ ipython nbconvert --to latex --post PDF MyNotebook.ipynb 

但同样,这会导致错误消息

ImportError: No module named 'PDF'

如果我试试

$ ipython nbconvert --to latex MyNotebook.ipynb 

这导致了

IPython.nbconvert.utils.pandoc.PandocMissing: Pandoc wasn't found:
Please check that pandoc is installed

如果我尝试安装pandoc(pip install pandoc),这给了我

ImportError: No module named 'ConfigParser'

这就是我被卡住的地方,因为我只是不知道还能做什么。任何人都知道如何解决任何错误?

pdf ipython jupyter
13个回答
26
投票

如果您在Mac上并安装了Homebrew,请打开终端shell并安装pandoc输入命令: brew安装pandoc

请耐心等待,安装和编译的时间可能会在慢速互联网连接或旧系统上花费一些时间。


1
投票

要将任何Jupyter笔记本转换为PDF,请按照以下说明操作:

(在Jupyter笔记本内):

在Mac OS上:

命令+ P - >您将获得一个打印对话框 - >将目标更改为PDF - >单击打印

在Windows上:

按Ctrl + P - >您将获得一个打印对话框 - >将目标更改为PDF - >单击打印


0
投票

我也有各种各样的问题。我不知道它是否能提供您所需要的,但我将笔记本下载为HTML文件,然后在我的Chrome浏览器中将其拉出,然后将其打印为PDF文件,我保存了该文件。它捕获了我的所有代码,文本和图形。它足以满足我的需求。


0
投票

我发现nbconvert / utils / pandoc.py有一个代码错误,导致我的机器出错。代码检查pandoc是否在您的环境变量路径中。对于我的机器,答案是否定的。但是pandoc.exe是!

解决方案是在第69行的代码中添加“.exe”

if __version is None:
    if not which('pandoc.exe'):
        raise PandocMissing()

同样适用于'xelatex'未安装。在第94行添加到文件nbconvert / exporters / pdf.py

    cmd = which(command_list[0]+'.exe')

0
投票

作为一个全新的成员,我无法简单地在帖子上添加评论,但我想说明Phillip Schwartz提供的解决方案对我有用。希望处于类似情况的人们能够更快地尝试这条道路。没有分页符是一个令人沮丧的问题很长一段时间,所以我很感激上面的讨论。

正如Phillip Schwartz所说:“你需要安装wkhtmltopdf:[http://wkhtmltopdf.org/downloads.html][1]

和Nbconvert“

然后添加“rawNBConvert”类型的单元格,并包括:

<p style="page-break-after:always;"></p>

这似乎对我有用,并且生成的PDF在相应的位置有分页符。您不需要运行自定义代码,因为它似乎是以HTML格式下载笔记本,在浏览器中打开以及在安装这些实用程序后打印到PDF的“正常”路径。


0
投票

要将.ipynb转换为pdf,您的系统应包含2个组件,

  1. nbconvert:jupyter的一部分允许将ipynb转换为pdf pip install nbconvert OR conda install nbconvert
  2. XeTeX:将ipynb转换为.tex格式,然后转换为pdf。 sudo apt-get install texlive-xetex

然后你可以使用下面的命令转换为pdf,

ipython nbconvert --to latex YOURNOTEBOOK.ipynb

如果它不起作用,请安装pandoc并重试。

sudo apt-get install pandoc

22
投票

为了使它工作,我安装了乳胶,典型的乳胶和pandoc。

使用ubuntu:

sudo apt-get install texlive texlive-latex-extra pandoc

它需要一些时间:下载几百Mb。我在某处读到你可以使用--no-install-recommends进行texlive和extra以减少到dl。


11
投票

2015-4-22:看起来像IPython更新意味着应该使用--to pdf而不是--to latex --post PDF。有一个相关的Github issue


7
投票

要将笔记本电脑转换为PDF,首先需要安装nbconvert。

pip install nbconvert
# OR
conda install nbconvert

接下来,如果您不使用Anaconda或尚未使用,则必须按照their website或Linux上的说明安装pandoc,如下所示:

sudo apt-get install pandoc

之后,您需要在您的机器上安装XeTex:

您现在可以导航到包含IPython Notebook的文件夹并运行以下命令:

jupyter nbconvert --to pdf MyNotebook.ipynb

如需进一步参考,请查看this link


4
投票

正如对问题的评论所说,你需要pandoc和latex(例如TeXShop)。我用Homebrew安装了pandoc,花了一秒钟。有了pandoc和TeXShop,我可以生成乳胶而不是pdf(在命令行上)。

ipython nbconvert --to latex mynotebook.ipynb

使用TeXShop探索latex(.tex)文件,失败的原因是缺少样式表和defs。安装完所有这些(adjustbox.sty,adjcalc.sty,trimclip.sty,collectbox.sty,tc-pgf.def,ucs.sty,uni-global.def,utf8x.def,ucsencs.def)后,它终于做到了工作。

但是,根据我的口味,结果看起来有点太时髦了。从Safari打印html失去语法着色太糟糕了。否则,它看起来不那么糟糕。 (这是OS X上的全部内容)。


3
投票

我在操作系统上使用Anaconda-Jupyter Notebook:用于Python编程的Ubuntu 16.0。

Install Nbconvert, Pandoc, and Tex:

打开终端并在其中执行以下命令。

Install Nbconvert: though it's part of the Jupyter ecosystem still install it once again

$conda install nbconvert

要么

$pip install nbconvert

但如果您使用的是anaconda,我会建议使用conda而不是pip

Install Pandoc: since Nbconvert uses Pandoc for converting markdown to formats other than HTML. Type following command

$sudo apt-get install pandoc

Install TeX: For converting to PDF, nbconvert uses the TeX. Type following command

$sudo apt-get install texlive-xetex

执行这些命令后,关闭打开的笔记本刷新主页或重新启动打开的笔记本的内核。现在尝试下载笔记本作为PDF格式:)

注意:有关详细信息,请参阅官方文档: https://nbconvert.readthedocs.io/en/latest/install.html


2
投票

此Python脚本具有GUI,可以使用资源管理器选择要转换为pdf的Ipython Notebook。使用wkhtmltopdf的方法是我发现的唯一方法,并提供高质量的pdf。此处描述的其他方法存在问题,语法突出显示不起作用或图形混乱。

你需要安装wkhtmltopdf:http://wkhtmltopdf.org/downloads.html

和Nbconvert

pip install nbconvert
# OR
conda install nbconvert

Python脚本

# Script adapted from CloudCray
# Original Source: https://gist.github.com/CloudCray/994dd361dece0463f64a
# 2016--06-29
# This will create both an HTML and a PDF file

import subprocess
import os
from Tkinter import Tk
from tkFileDialog import askopenfilename

WKHTMLTOPDF_PATH = "C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf"  # or wherever you keep it

def export_to_html(filename):
    cmd = 'ipython nbconvert --to html "{0}"'
    subprocess.call(cmd.format(filename), shell=True)
    return filename.replace(".ipynb", ".html")


def convert_to_pdf(filename):
    cmd = '"{0}" "{1}" "{2}"'.format(WKHTMLTOPDF_PATH, filename, filename.replace(".html", ".pdf"))
    subprocess.call(cmd, shell=True)
    return filename.replace(".html", ".pdf")


def export_to_pdf(filename):
    fn = export_to_html(filename)
    return convert_to_pdf(fn)

def main():
    print("Export IPython notebook to PDF")
    print("    Please select a notebook:")

    Tk().withdraw() # Starts in folder from which it is started, keep the root window from appearing 
    x = askopenfilename() # show an "Open" dialog box and return the path to the selected file
    x = str(x.split("/")[-1])

    print(x)

    if not x:
        print("No notebook selected.")
        return 0
    else:
        fn = export_to_pdf(x)
        print("File exported as:\n\t{0}".format(fn))
        return 1

main()

1
投票

Ubuntu和Mac OSX都遇到过这个问题。经过一系列疯狂的搜索和试验后,两者都得到了解决。这需要texpandoc;两个巨型外部程序都无法通过Python的pip安装。

Mac OSX:使用MacPorts安装pandoc

port install pandoc

这应该需要将近一个小时才能完成(通常情况下)。如果问题仍然存在,则可能必须安装MacTeX发行版。 TeXLive。

对于Ubuntu:从network installer安装vanilla TeXLive - 而不是通过apt-get。然后使用apt-get安装pandoc。

sudo apt-get install pandoc

完整安装TeXLive需要磁盘上最多4.4 GB。

为了省去所有这些麻烦,推荐使用IPython / Jupyter Notebook的方法是安装Anaconda Python发行版。


1
投票

如果您使用的是sagemath云版,您可以直接转到左下角, 选择文件 - >下载为 - > Pdf via LaTeX(.pdf) 如果需要,请检查屏幕截图。

Screenshot Convert ipynb to pdf

如果它因任何原因不起作用,您可以尝试其他方式。 选择文件 - >打印预览,然后选择预览 右键单击 - >打印,然后选择另存为pdf。

© www.soinside.com 2019 - 2024. All rights reserved.