将IPython笔记本转换为PDF

时间:2014-10-09 15:24:54

标签: python-3.x pdf-generation ipython ipython-notebook

我写了一个很长的IPython笔记本,需要将其转换为PDF才能分发。我已阅读this thread,但解决方案对我不起作用。

我尝试了以下内容:

  1. 如果我在浏览器中使用“打印”按钮,则会显示所有IPython菜单 等等,我不能接受。

  2. 如果我使用IPython菜单中的“打印预览”,则不会渲染任何LaTeX方程式。它们仍然只是LaTeX代码。

  3. 如果我使用ipython nbconvert --to latex --post pdf myfile.ipynb,我会获得FileNotFoundError。跟踪如下:

  4. [NbConvertApp] Using existing profile dir: 'C:\\Users\\blokeley\\.ipython\\profile_default'
    [NbConvertApp] Converting notebook myfile.ipynb to latex
    [NbConvertApp] Support files will be in myfile_files\
    [NbConvertApp] Loaded template article.tplx
    [NbConvertApp] Writing 72246 bytes to myfile.tex
    [NbConvertApp] Building PDF
    [NbConvertApp] Running pdflatex 3 times: ['pdflatex', 'myfile.tex']
    Traceback (most recent call last):
      File "C:\Anaconda3\Scripts\ipython-script.py", line 5, in <module>
        sys.exit(start_ipython())
      File "C:\Anaconda3\lib\site-packages\IPython\__init__.py", line 120, in start_ipython
        return launch_new_instance(argv=argv, **kwargs)
      File "C:\Anaconda3\lib\site-packages\IPython\config\application.py", line 564,
      launch_instance
        app.start()
      File "C:\Anaconda3\lib\site-packages\IPython\terminal\ipapp.py", line 367, in start
        return self.subapp.start()
      File "C:\Anaconda3\lib\site-packages\IPython\nbconvert\nbconvertapp.py",
      line 268, in start
        self.convert_notebooks()
      File "C:\Anaconda3\lib\site-packages\IPython\nbconvert\nbconvertapp.py",
      line 311, in convert_notebooks
        self.postprocessor(write_resultes)
      File "C:\Anaconda3\lib\site-packages\IPython\nbconvert\postprocessors\base.py",
      line 28, in __call__
        self.postprocess(input)
      File "C:\Anaconda3\lib\site-packages\IPython\nbconvert\postprocessors\pdf.py",
      line 140, in postprocess
        cont = self.run_latex(filename)
      File "C:\Anaconda3\lib\site-packages\IPython\nbconvert\postprocessors\pdf.py",
      line 104, in run_latex
        self.latex_count, log_error)
      File "C:\Anaconda3\lib\site-packages\IPython\nbconvert\postprocessors\pdf.py",
      line 84, in run_command
        p = subprocess.Popen(command, stdout=stdout, stdin=null)
      File "C:\Anaconda3\lib\subprocess.py", line 858, in __init__
        restore_signals, start_new_session)
      File "C:\Anaconda3\lib\subprocess.py", line 1111, in _execute_child
        startupinfo)
      FileNotFoundError: [WinError 2] The system cannot find the file specified
    
    If you suspect this is an IPython bug, please report it at:
        https://github.com/ipython/ipython/issues
    or send an email to the mailing list at ipython-dev@scipy.org
    
    You can print a more detailed traceback right now with "%tb", or use "%debug"
    to interactively debug it.
    
    Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
        c.Application.verbose_crash=True
    

    我在Windows 7 Professional上使用64位的Python 3.4中的Anaconda的最新更新。

1 个答案:

答案 0 :(得分:3)

您的问题是您的计算机上未安装LaTeX。当subprocess尝试呼叫pdflatex myfile.tex时,它会失败,因为它无法找到pdflatex

虽然您可以在不需要LaTeX的情况下从IPython笔记本创建.tex文件,但这些只是包含LaTeX代码的文件。要编译它们以创建PDF,您需要一个版本的LaTeX。

在Windows上安装LaTeX并不困难,MiKTeX是一种可能的安装。