自动将ipython输出转换为pdf [英] Automating ipython output to pdf

查看:176
本文介绍了自动将ipython输出转换为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序,基本上可以进行各种统计计算,并打印出结果和图表.

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

目前,一种获得程序良好pdf输出的便捷方法是使用魔术命令%matplotlib内联在Jupyter IPython Notebook中运行我的代码,并通过执行通过LaTex(.pdf)PDF"将其另存为pdf

At present, a convenient way to get a nice pdf output of my program is to run my code in Jupyter IPython Notebook using magic command %matplotlib inline and save as pdf by doing "PDF via LaTex(.pdf) "

但是,问题是我每次运行程序时都必须手动进行操作.此外,我无法以二进制可执行文件的形式将程序交付给其他人,这是我的最终目标.

But, the problem is I have to do it manually every time I run the program. In addition, I cannot deliver the program as binary executable to other people, which is my final goal.

是否可以通过编程方式执行此操作?明确地说,我想要的只是我的程序以pdf格式输出,以便在运行可执行文件时,输出为pdf. 我不希望最终用户创建ipython笔记本.最终用户将无权访问源代码.

Is there a way to do this programmatically? Just to be clear, all I want is output of my program in pdf format so that when the executable is run, the output is pdf. I don't want the end user to create ipython notebook. The end user will not have access to the source code.

推荐答案

截至目前,尚无用于将笔记本转换为PDF的编程语言API.但是,存在一个名为 nbconvert 的命令行实用程序.它具有与Web界面的格式转换器相同的功能.要转换为PDF,可以执行jupyter nbconvert notebook.ipynb --to pdf.

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.

幸运的是,Python有一个方便的子流程模块,该模块可让您产生新的流程,检查其输入和输出,并获取其返回码.因此,您可以执行以下操作:

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")

有点骇人听闻的方法,但是它可以工作.如果您使用的是Windows,则可能需要将其他kwarg shell=True添加到subprocess.call()

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()

这篇关于自动将ipython输出转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆