繁体   English   中英

将多个jupyter notebook转换为pdf

[英]Convert multiple jupyter notebooks to pdf

我在一个文件夹中有多个 jupyter 笔记本。 由于我对它们的编辑,它们会随着时间而改变。 我希望将它们反复保存为 PDF。 是否有 python 脚本可以干净地执行此操作? (最好使用 nbconvert 东西。)

from os import system, listdir
from shutil import move
from os import mkdir, getcwd


def jup2pdf():
    print("Please input a run_no ")
    print("Only integers allowed")
    run_no = int(input())
    run_dir_name = 'pdf' + str(run_no)

    mkdir(run_dir_name)

    other_stuff = []
    for f in listdir("."):
        if f.endswith("ipynb"):
            system("jupyter nbconvert --to pdf " + f)
            # convert to pdf
            f_pdf = '.'.join([f[: -6], 'pdf'])
            # make the corresponding pdf file name
            move(f_pdf, run_dir_name, )
            # move the corresponding pdf file
        else:
            print('You have something other than a Jupyter notebook.')
            # print(f)
            other_stuff.append(f)

    print('The following pdf files have been created.')
    print('in', run_dir_name)
    print()
    for f in listdir(run_dir_name):
        if f.endswith("pdf"):
            print(f)
        else:
            print('PDFs not found in ', getcwd())

    print('We also found the following')
    for other_item in other_stuff:
        print(other_item)

    return None


'''

'''
check_pres_of = [f.endswith("ipynb") for f in listdir(".")]

if (True in check_pres_of):
    print('Found Jupyter notebooks in', getcwd())
    jup2pdf()
    # print([f.endswith("ipynb") for f in listdir(".")])
else:
    print('Jupyter notebooks not found in ', getcwd())

这个脚本应该可以完成你的工作。 首先,它会检查您的文件夹中是否有任何 jupyter 笔记本。 如果不是,它会显示“在”所述文件夹中找不到 Jupyter notebooks。 如果是,则继续进行转换。 然后它会告诉您哪些 jupyter 笔记本已转换为 pdf。

它还检查所述文件夹中是否还有其他文件、子文件夹。

检查以下链接是否有用。 关联

文章中提供了以下步骤:将所有文件保存在一个文件夹中,并在命令提示符下执行以下步骤:

  1. 安装必要的 package:

    pip 安装-U notebook-as-pdf

  2. 为铬安装额外的设置

    pyppeteer 安装

  3. 如果所有文件都保存在一个文件夹中,运行命令将所有笔记本文件从 *.ipynb 转换为 *.pdf:

    jupyter-nbconvert --to PDFviaHTML *.ipynb

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM