IPython / Jupyter 노트북을 PDF로 저장할 때 발생하는 문제
그래서, 나는 jupyter 노트북을 PDF로 저장하려고 노력했지만 이것을 수행하는 방법을 알 수 없습니다. 가장 먼저 시도하는 것은 파일 메뉴에서 PDF로 다운로드하는 것입니다.
nbconvert failed: PDF creating failed
다음으로 시도 할 것은 명령 프롬프트에서 이와 같이 변환을 시도하는 것입니다.
$ ipython nbconvert --to latex --post PDF MyNotebook.ipynb
그러나 다시 이것은 오류 메시지가 발생합니다.
ImportError: No module named 'PDF'
그리고 내가 시도한다면
$ ipython nbconvert --to latex MyNotebook.ipynb
이 결과
IPython.nbconvert.utils.pandoc.PandocMissing: Pandoc wasn't found:
Please check that pandoc is installed
pandoc ( pip install pandoc
) 을 설치하려고하면
ImportError: No module named 'ConfigParser'
그리고 이것은 내가 다른 무엇을 해야할지 모르기 때문에 내가 막히는 곳입니다. 누구나 잘못된 것을 고치는 방법을 알고 있습니까?
Mac을 사용 중이고 Homebrew가 설치되어있는 경우 터미널 셸을 열고 다음 명령을 입력하여 pandoc을 설치합니다 .
brew install pandoc
인내심을 가지십시오. 느린 인터넷 연결이나 오래된 시스템에서는 설치 및 컴파일에 시간이 걸릴 수 있습니다.
이를 작동시키기 위해 라텍스, 일반 라텍스 추가 및 pandoc을 설치했습니다.
우분투 사용 :
sudo apt-get install texlive texlive-latex-extra pandoc
몇 번의 시간이 걸립니다 : 다운로드하는 데 몇 100 Mb. 나는 --no-install-recommends
texlive에 사용할 수 있고 dl로 줄이기 위해 추가로 사용할 수있는 곳을 읽었습니다 .
2015-4-22 : IPython 업데이트 --to pdf
는 --to latex --post PDF
. 관련된 Github 문제가 있습니다.
노트북을 PDF로 변환하려면 먼저 nbconvert를 설치해야합니다.
pip install nbconvert
# OR
conda install nbconvert
다음으로 Anaconda를 사용하지 않거나 아직 사용하지 않은 경우 웹 사이트 의 지침에 따라 또는 Linux에서 다음과 같이 pandoc을 설치해야 합니다.
sudo apt-get install pandoc
그런 다음 컴퓨터에 XeTex를 설치해야합니다.
이제 IPython 노트북이있는 폴더로 이동하여 다음 명령을 실행할 수 있습니다.
jupyter nbconvert --to pdf MyNotebook.ipynb
질문에 대한 의견에 따르면 pandoc 및 라텍스 (예 : TeXShop)가 필요합니다. Homebrew와 함께 pandoc을 설치했는데 1 초 밖에 걸리지 않았습니다. pandoc과 TeXShop을 사용하면 라텍스를 생성 할 수 있지만 pdf는 생성 할 수 없습니다 (명령 줄에서).
ipython nbconvert --to latex mynotebook.ipynb
TeXShop으로 라텍스 (.tex) 파일을 탐색 할 때 실패는 스타일 시트와 정의가 누락되어 발생했습니다. 이들 (adjustbox.sty, adjcalc.sty, trimclip.sty, collectbox.sty, tc-pgf.def, ucs.sty, uni-global.def, utf8x.def, ucsencs.def)을 모두 설치 한 후, 마침내 완료되었습니다. 작업.
그러나 결과는 내 취향에 비해 너무 펑키 해 보입니다. Safari에서 html을 인쇄하면 구문 색상이 손실되는 것은 너무 나쁩니다. 그렇지 않으면 그렇게 나쁘게 보이지 않습니다. (이것은 모두 OS X에 있습니다).
이 Python 스크립트에는 pdf로 변환하려는 Ipython Notebook을 탐색기로 선택할 수있는 GUI가 있습니다. wkhtmltopdf를 사용한 접근 방식은 내가 찾은 유일한 접근 방식이며 잘 작동하고 고품질 pdf를 제공합니다. 여기에 설명 된 다른 접근 방식은 문제가 있으며 구문 강조가 작동하지 않거나 그래프가 엉망입니다.
wkhtmltopdf를 설치해야합니다 : http://wkhtmltopdf.org/downloads.html
및 Nbconvert
pip install nbconvert
# OR
conda install nbconvert
Python 스크립트
# Script adapted from CloudCray
# Original Source: https://gist.github.com/CloudCray/994dd361dece0463f64a
# 2016--06-29
# This will create both an HTML and a PDF file
import subprocess
import os
from Tkinter import Tk
from tkFileDialog import askopenfilename
WKHTMLTOPDF_PATH = "C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf" # or wherever you keep it
def export_to_html(filename):
cmd = 'ipython nbconvert --to html "{0}"'
subprocess.call(cmd.format(filename), shell=True)
return filename.replace(".ipynb", ".html")
def convert_to_pdf(filename):
cmd = '"{0}" "{1}" "{2}"'.format(WKHTMLTOPDF_PATH, filename, filename.replace(".html", ".pdf"))
subprocess.call(cmd, shell=True)
return filename.replace(".html", ".pdf")
def export_to_pdf(filename):
fn = export_to_html(filename)
return convert_to_pdf(fn)
def main():
print("Export IPython notebook to PDF")
print(" Please select a notebook:")
Tk().withdraw() # Starts in folder from which it is started, keep the root window from appearing
x = askopenfilename() # show an "Open" dialog box and return the path to the selected file
x = str(x.split("/")[-1])
print(x)
if not x:
print("No notebook selected.")
return 0
else:
fn = export_to_pdf(x)
print("File exported as:\n\t{0}".format(fn))
return 1
main()
OS에서 Anaconda-Jupyter Notebook을 사용하고 있습니다 : Python 프로그래밍을 위해 Ubuntu 16.0.
Nbconvert, Pandoc 및 Tex를 설치합니다.
터미널을 열고 다음 명령을 구현하십시오.
Nbconvert 설치 : Jupyter 생태계의 일부이지만 여전히 다시 설치합니다.
$conda install nbconvert
또는
$pip install nbconvert
하지만 아나콘다를 사용하는 경우 pip 대신 conda를 사용하는 것이 좋습니다.
Pandoc 설치 : Nbconvert는 마크 다운을 HTML 이외의 형식으로 변환하기 위해 Pandoc을 사용하기 때문입니다. 다음 명령을 입력하십시오.
$sudo apt-get install pandoc
TeX 설치 : PDF로 변환하기 위해 nbconvert는 TeX를 사용합니다. 다음 명령을 입력하십시오.
$sudo apt-get install texlive-xetex
이 명령을 실행 한 후 열린 노트북을 닫고 홈 페이지를 새로 고치거나 열린 노트북의 커널을 다시 시작하십시오. 이제 노트북을 pdf로 다운로드하십시오 :)
참고 : 자세한 내용은 공식 문서를 참조하십시오 :
https://nbconvert.readthedocs.io/en/latest/install.html
Jupyter 노트북을 PDF로 변환하려면 아래 지침을 따르십시오.
( Jupyter 노트북 안에 있어야합니다 ) :
에 맥 OS :
command + P-> 인쇄 대화 상자가 나타납니다-> 대상을 PDF로 변경-> 인쇄를 클릭하십시오
에 윈도우 :
Ctrl + P-> 인쇄 대화 상자가 나타납니다-> 대상을 PDF로 변경-> 인쇄를 클릭하십시오
위의 단계가 Jupyter 노트북의 전체 PDF를 생성하지 않는 경우 (아마 Chrome이 경우에 따라 Jupyter가 큰 출력을 스크롤하기 때문에 모든 출력을 인쇄하지 않기 때문에),
메뉴에서 자동 스크롤을 제거하려면 아래 단계를 수행하십시오 .
크레딧 : @ ÂngeloPolotto
This problem was experienced with both Ubuntu and Mac OSX. After a frantic set of searches and trials, both of them were solved. This requires both tex
and pandoc
; both jumbo external programs cannot installed by Python's pip
.
Mac OSX: using MacPorts installation of pandoc
port install pandoc
This should take nearly an hour to complete (in the usual case). If the problem persists, you might have to install MacTeX distro. of TeXLive.
For Ubuntu: install vanilla TeXLive from the network installer -- not through apt-get. Then install pandoc using apt-get.
sudo apt-get install pandoc
A complete installation of TeXLive would require a upto to 4.4 GB on disk.
To save all this trouble, the recommeded way to use IPython/Jupyter Notebook would be to install Anaconda Python distribution.
If you are using sagemath cloud version, you can simply go to the left corner,
select File --> Download as --> Pdf via LaTeX (.pdf)
Check the screenshot if you want.
Screenshot Convert ipynb to pdf
If it dosn't work for any reason, you can try another way.
select File --> Print Preview and then on the preview
right click --> Print and then select save as pdf.
As a brand new member, I was unable to simply add a comment on the post but I want to second that the solution offered by Phillip Schwartz worked for me. Hopefully people in a similar situation will try that path sooner with the emphasis. Not having page breaks was a frustrating problem for quite a while so I am grateful for the discussion above.
As Phillip Schwartz said: "You'll need to install wkhtmltopdf: [http://wkhtmltopdf.org/downloads.html][1]
and Nbconvert "
You then add a cell of the type "rawNBConvert" and include:
<p style="page-break-after:always;"></p>
That seemed to do the trick for me, and the generated PDF had the page break at the corresponding locations. You don't need to run the custom code though, as it seems the "normal" path of downloading the notebook as HTML, opening in browser, and printing to PDF works once those utilities are installed.
I had all kinds of problems figuring this out as well. I don't know if it will provide exactly what you need, but I downloaded my notebook as an HTML file, then pulled it up in my Chrome browser, and then printed it as a PDF file, which I saved. It captured all my code, text and graphs. It was good enough for what I needed.
What I found was that the nbconvert/utils/pandoc.py had a code bug that resulted in the error for my machine. The code checks if pandoc is in your environmental variables path. For my machine the answer is no. However pandoc.exe is!
Solution was to add '.exe' to the code on line 69
if __version is None:
if not which('pandoc.exe'):
raise PandocMissing()
The same goes for 'xelatex' is not installed. Add to the file nbconvert/exporters/pdf.py on line 94
cmd = which(command_list[0]+'.exe')
To convert .ipynb into pdf, your system should contain 2 components,
nbconvert: Is part of jupyter allows to convert ipynb to pdf
pip install nbconvert OR conda install nbconvert
XeTeX: Convert ipynb to .tex format and then converting to pdf.
sudo apt-get install texlive-xetex
Then you can use below command to convert to pdf,
ipython nbconvert --to pdf YOURNOTEBOOK.ipynb
In case, it doesn't work, install pandoc and try again.
sudo apt-get install pandoc
For Ubuntu users, an answer can be found here. I also quote it:
The most probable cause, is that you have not installed the appropriate dependencies. Your Ubuntu system has to have some packages installed regarding conversion of LaTeX and XeTeX files, in order to save your notebook as PDF. You can install them by:
sudo apt-get install texlive texlive-xetex texlive-generic-extra texlive-generic-recommended pandoc
Also,
nbconvert
is another dependency that is usually automatically installed with jupyter. But you can install it just to be sure, while having your virtual environment activated:pip install -U nbconvert
참고URL : https://stackoverflow.com/questions/29156653/ipython-jupyter-problems-saving-notebook-as-pdf
'Program Tip' 카테고리의 다른 글
Collections.max () 서명에서 T가 Object에 묶여있는 이유는 무엇입니까? (0) | 2020.11.05 |
---|---|
ApplicationContextAware는 Spring에서 어떻게 작동합니까? (0) | 2020.11.05 |
사용하지 않는 문자열의 컴파일러 최적화에 대한 일관성없는 동작 (0) | 2020.11.05 |
D3.js가 데이터를 노드에 바인딩하는 방법 이해 (0) | 2020.11.05 |
곱셈이 부동 나누기보다 빠릅니까? (0) | 2020.11.05 |