簡體   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