Automating ipython output to pdf

前端 未结 2 788
没有蜡笔的小新
没有蜡笔的小新 2021-02-15 04:30

I have a little program that basically does all kinds of statistical calculation and prints out results and charts.

At present, a convenient way to get a nice pdf output

相关标签:
2条回答
  • 2021-02-15 05:18

    As of now there's not a programming language API to convert notebooks to PDF. However, a command line utility exists called nbconvert. It has the same functionality as the web interface's format converters. To convert to PDF, you can do jupyter nbconvert notebook.ipynb --to pdf.

    Luckily, Python has a handy subprocess module which allows you to spawn new processes, check their inputs and outputs, and get their return codes. So you could do something like:

    import subprocess
    subprocess.call("jupyter nbconvert notebook.ipynb --to pdf")
    

    Kind of a hacky way, but it will work. If you're using Windows, you may need to add the additional kwarg shell=True to subprocess.call()

    0 讨论(0)
  • 2021-02-15 05:20

    If you have an IPython notebook, you can make a small python or bash script that first executes and then produces the PDF. For me, this bash script works:

    jupyter nbconvert --ExecutePreprocessor.timeout=500 --to notebook --execute my_file.ipynb
    jupyter nbconvert --to pdf my_file.nbconvert.ipynb
    rm my_file.nbconvert.ipynb
    

    If you want to customise the output using a template, I have found that converting the file first to markdown and then using pandoc to create a PDF avoids some issues:

    jupyter nbconvert --ExecutePreprocessor.timeout=500 --to notebook --execute my_file.ipynb
    jupyter nbconvert  --to markdown my_file.nbconvert.ipynb  --template="mytemplate.tpl"
    pandoc --toc --template=mytemplate.tex markdown my_file.nbconvert.ipynb --latex-engine=pdflatex -o final file.pdf
    
    0 讨论(0)
提交回复
热议问题