python - 将多个 jupyter notebook 转换为 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 笔记本”。如果是,它会继续转换它们。然后它会告诉您哪些 jupyter notebook 已转换为 pdf。
它还会检查所述文件夹中是否还有其他文件、子文件夹。
推荐阅读
- javascript - 使用此特定代码使用 JavaScript 在实际网页上打印一些消息的最简单方法
- bayesian - 错误消息切片器卡在具有无限密度的值
- java - 使用 Spring 集成的 Java 中的 Python。错误:javax.script.ScriptEngineManager 无法为语言“python”创建脚本引擎
- java - 数组中的递增或递减序列
- c# - 如何在他们的示例 Pet Store 的 swagger.io csharp 客户端中配置 nunit 测试?
- javascript - 从 Chrome 扩展发送 POST 请求
- linux - Ansible - winrm 或 requests 未安装
- javascript - 如何从 onChange() 事件中获取道具?
- flutter - 如何使用 StreamBuilder 检查我们是否在 ListView 的末尾
- android - Unity Google Play Console Warning optimize APK