使用nbconvert和PDFExporter()将jupyter笔记本导出为pdf的最小示例

时间:2016-09-27 19:26:25

标签: pdf jupyter-notebook nbconvert

我正在尝试从笔记本单元格中使用nbconvert导出jupyter笔记本的pdf副本。我已经阅读了documentation,但我找不到实际执行nbconvert命令并导出为pdf的基本代码。

我能够做到这一点,但我希望有人可以填补最后的空白。

from nbconvert import PDFExporter
notebook_pdf = PDFExporter()
notebook_pdf.template_file = '../print_script/pdf_nocode.tplx'

请注意如何从这里开始实际创建pdf。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:3)

我不是专家,但设法让这个工作。关键是您需要预处理笔记本,这将允许您使用PDFExporter.from_notebook_node()功能。这将为您提供字节格式的pdf_data,然后可以将其写入文件:

import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
from nbconvert import PDFExporter

notebook_filename = "notebook.ipynb"

with open(notebook_filename) as f:
    nb = nbformat.read(f, as_version=4)

ep = ExecutePreprocessor(timeout=600, kernel_name='python3')

ep.preprocess(nb, {'metadata': {'path': 'notebooks/'}})

pdf_exporter = PDFExporter()

pdf_data, resources = pdf_exporter.from_notebook_node(nb)

with open("notebook.pdf", "wb") as f:
    f.write(pdf_data)
    f.close()

值得注意的是,ExecutePreprocessor需要资源dict,但我们在此示例中不使用它。

答案 1 :(得分:0)

下面是rest api,它将.ipynb文件转换为.html POST:http://URL/export/<id> 获取:http://URL/export/<id>将返回一个id.html

import os
from flask import Flask, render_template, make_response
from flask_cors import CORS
from flask_restful import reqparse, abort, Api, Resource
from nbconvert.exporters import HTMLExporter

exporter = HTMLExporter()

app = Flask(__name__)
cors = CORS(app, resources={r"/export/*": {"origins": "*"}})
api = Api(app)
parser = reqparse.RequestParser()
parser.add_argument('path')
notebook_file_srv = '/path of your .ipynb file'

def notebook_doesnt_exist(nb):
    abort(404, message="Notebook {} doesn't exist".format(nb))


class Notebook(Resource):
    def get(self, id):
        headers = {'Content-Type': 'text/html'}
        return make_response(render_template(id + '.html'), 200, headers)


    def post(self, id):
        args = parser.parse_args()
        notebook_file = args['path']

        notebook_file = notebook_file_srv + id + '.ipynb'

        if not os.path.exists(notebook_file):
            return 'notebook \'.ipynb\' file not found', 404
        else:
            nb_name, _ = os.path.splitext(os.path.basename(notebook_file))
            # dirname = os.path.dirname(notebook_file)
            output_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'templates')

            output_path = os.path.join(output_path, '{}.html'.format(nb_name))

            output, resources = exporter.from_filename(notebook_file)

            f = open(output_path, 'wb')
            f.write(output.encode('utf8'))
            f.close()
            return 'done', 201


api.add_resource(Notebook, '/export/<id>')

if __name__ == '__main__':
    app.run(debug=True)
相关问题