Python 无需代码即可将笔记本导出为 pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34818723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
export notebook to pdf without code
提问by aloha
I have a large notebook with a lot of figures and text. I want to convert it to a html file. However, I don't want to export the code. I am using the following command
我有一个很大的笔记本,里面有很多数字和文字。我想将其转换为 html 文件。但是,我不想导出代码。我正在使用以下命令
ipython nbconvert --to html notebook.ipynb
But this option also exports the code. Is there a way to convert the notebook to html without the code?
但是这个选项也会导出代码。有没有办法在没有代码的情况下将笔记本转换为 html?
采纳答案by Esptheitroad Murhabazi
I found this article interesting it explain how to remove the input columns :
我发现这篇文章很有趣,它解释了如何删除输入列:
you have just to create a template file named "hidecode.tplx" in same directory as the notebook you want to convert and add those line in it :
您只需在要转换的笔记本所在的目录中创建一个名为“hidecode.tplx”的模板文件,然后在其中添加这些行:
((*- extends 'article.tplx' -*))
((* block input_group *))
((*- if cell.metadata.get('nbconvert', {}).get('show_code', False) -*))
((( super() )))
((*- endif -*))
((* endblock input_group *))
And after run this command it will use pdfLatex to convert the notebook in pdf via latex:
运行此命令后,它将使用 pdfLatex 通过乳胶将笔记本转换为 pdf:
jupyter nbconvert --to pdf --template hidecode Example.ipynb
or if you want to edit you can convert it to a .tex document and use pdfLatex to put it in pdf :
或者,如果您想编辑,您可以将其转换为 .tex 文档并使用 pdfLatex 将其放入 pdf :
jupyter nbconvert --to latex --template hidecode Example.ipynb
EDIT Sept 2018:
ipython nbconvert
is deprecated. It will be replaced by jupyter nbconvert
: So we replace the command ipython
with jupyter
编辑 2018 年 9 月:
ipython nbconvert
已弃用。它将被替换为jupyter nbconvert
:所以我们更换命令ipython
用jupyter
回答by teddy browns
You can add this css to your page and then print into PDF from the browser. Note that the code is only hidden, not removed.
您可以将此 css 添加到您的页面,然后从浏览器打印为 PDF。请注意,代码只是隐藏,而不是删除。
div.output_prompt { visibility: hidden; }
*{ font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif !important }
.input,#header { display: none; }
To make sure you remove the code you should use something like
为了确保删除代码,您应该使用类似的东西
$(".input").remove()
As far as I know there is no way to generate via latex a PDF that does not include the code. It would be great to have a jupyter notebook extension that does it though...
据我所知,无法通过乳胶生成不包含代码的 PDF。不过,如果有一个 jupyter notebook 扩展,那就太好了……
回答by Neal Fultz
nbconvert
uses templates to convert the notebook's JSON into a document; for example, in the markdown template that comes with nbconvert, markdown.tpl
:
nbconvert
使用模板将笔记本的 JSON 转换为文档;比如在nbconvert自带的markdown模板中,markdown.tpl
:
{% block input %}
{% if nb.metadata.language_info %}{{ nb.metadata.language_info.name }}{% endif %}
{{ cell.source}}
{% endblock input %}
which prints each cell's source in the output. If you delete {{ cell.source}}
then the cell source will not print out. There are corresponding blocks in the HTML and Latex templates as well.
它在输出中打印每个单元格的源。如果删除,{{ cell.source}}
则单元格源将不会打印出来。HTML 和 Latex 模板中也有相应的块。
Better yet, make your own template that does exactly what you want :)
更好的是,制作您自己的模板,完全符合您的要求:)
回答by Herpes Free Engineer
As a generalization of this answer, so that hidecodetemplate could be accessible from multiple locations:
作为一个概括这个答案,让hidecode模板可以从多个位置访问:
Go into your home directory:
cd ~/.jupyter
Create
jupyter_nbconvert_config.py
under this directory.Write the following into the
.py
file (Changeuser_name
to your user name):c = get_config() c.TemplateExporter.template_path = ['.', "~/.jupyter" ] c.LatexExporter.template_path = ['.', "~/.jupyter"]
Create a template file under this directory, named
hidecode.tplx
orhidecode.tpl
:((*- extends 'article.tplx' -*)) ((* block input_group *)) ((*- if cell.metadata.get('nbconvert', {}).get('show_code', False) -*)) ((( super() ))) ((*- endif -*)) ((* endblock input_group *))
Then, the following should generate a PDF without
.ipynb
files' codes (Changefile_name
to your file name):jupyter nbconvert --to pdf '<file_name>.ipynb' --template=hidecode.tpl
进入你的主目录:
cd ~/.jupyter
jupyter_nbconvert_config.py
在这个目录下创建。将以下内容写入
.py
文件(更改user_name
为您的用户名):c = get_config() c.TemplateExporter.template_path = ['.', "~/.jupyter" ] c.LatexExporter.template_path = ['.', "~/.jupyter"]
在这个目录下创建一个模板文件,命名为
hidecode.tplx
orhidecode.tpl
:((*- extends 'article.tplx' -*)) ((* block input_group *)) ((*- if cell.metadata.get('nbconvert', {}).get('show_code', False) -*)) ((( super() ))) ((*- endif -*)) ((* endblock input_group *))
然后,以下内容应生成没有
.ipynb
文件代码的 PDF (更改file_name
为您的文件名):jupyter nbconvert --to pdf '<file_name>.ipynb' --template=hidecode.tpl
回答by amagard
Here is how I do it: I simply download my notebook as html.
我是这样做的:我只是将我的笔记本下载为 html。
Then run this python script to convert that html file so that prompts and code cells are gone:
然后运行这个 python 脚本来转换那个 html 文件,这样提示和代码单元格就消失了:
FILE = "/somewhere/myHTMLFile.html"
with open(FILE, 'r') as html_file:
content = html_file.read()
# Get rid off prompts and source code
content = content.replace("div.input_area {","div.input_area {\n\tdisplay: none;")
content = content.replace(".prompt {",".prompt {\n\tdisplay: none;")
f = open(FILE, 'w')
f.write(content)
f.close()
That script bascially adds the CSS 'display: none' attribute for all divs of class 'prompt' or 'input_area'.
该脚本基本上为“prompt”或“input_area”类的所有div添加了CSS“display:none”属性。
回答by Boying
I was seeking the same question in SO and finally turned out to a very straightforward way:
我在 SO 中寻求同样的问题,最后结果是一种非常简单的方式:
Assuming using Firefox(57) + Win7
假设使用 Firefox(57) + Win7
- Run Jupyter notebook and download the notebook in the browser: File->Download as->HTML and you will get a html page with code and output.
- Open the exported HTML with browser and activate the browser console with key
F12
Run following command in the console:
document.querySelectorAll("div.input").forEach(function(a){a.remove()})
The code removes all input div DOM. Then
right mouse button
and chose "Save Page As" and Save the "Complete page" (not single page).You will get a page with an associated folder in windows. Use a trick by zip the html page and then extract to unbind the associated. The folder is useless.
Now it is a single html page without code. You can re-distribute it or print it as PDF.
- 运行 Jupyter notebook 并在浏览器中下载 notebook:文件->下载为->HTML,您将获得一个带有代码和输出的 html 页面。
- 用浏览器打开导出的 HTML 并用键激活浏览器控制台
F12
在控制台中运行以下命令:
document.querySelectorAll("div.input").forEach(function(a){a.remove()})
该代码删除所有输入 div DOM。然后
right mouse button
选择“将页面另存为”并保存“完整页面”(不是单页)。您将在 Windows 中获得一个带有关联文件夹的页面。通过压缩 html 页面使用一个技巧,然后解压缩以解除关联。文件夹没用。
现在它是一个没有代码的单个 html 页面。您可以重新分发它或将其打印为 PDF。
If you are not using Firefox or Windows, please adjust the above 3-6 steps.
如果您不是使用 Firefox 或 Windows,请调整以上 3-6 步。