简体   繁体   English

如何通过 VSCode 以 PDF 格式导出 Jupyter Notebook? (视窗 10)

[英]How to Export Jupyter Notebook by VSCode in PDF format? (Windows 10)

When I try to export my Jupyter Notebook in pdf format in VSCode like this:当我尝试在VSCode中以 pdf 格式导出 Jupyter Notebook 时,如下所示: 在此处输入图像描述

在此处输入图像描述

then I got this error:然后我收到了这个错误:
在此处输入图像描述

and jupyter output panel says: jupyter 输出面板说: 在此处输入图像描述

so i tried to install MikTeX and update the required packages, but still I can't export Jupyter Notebooks in PDF format by VSCode!所以我尝试安装 MikTeX并更新所需的软件包,但我仍然无法通过 VSCode 以 PDF 格式导出 Jupyter Notebooks!
how can I fix this problem?我该如何解决这个问题?

Note That I know i can do it by convert it to HTML and then with ctrl+p try to save it as pdf!请注意,我知道我可以通过将其转换为 HTML 来做到这一点,然后使用ctrl+p尝试将其保存为 pdf! but I want to convert it to pdf in straight way!但我想直接将其转换为pdf!

Since I'm using conda venvs, I did these steps:由于我使用的是 conda venvs,因此我执行了以下步骤:

  1. Activate conda venv using: conda activate <NAME_OF_VENV> in Anaconda prompt.在 Anaconda 提示符下使用 conda conda activate <NAME_OF_VENV>激活 conda venv。
  2. Install nbconvert using conda install -c anaconda nbconvert使用conda install -c anaconda nbconvert

Now it's all okay, and I can export Jupyter notebooks in HTML and PDF format both.现在一切都好,我可以以 HTML 和 PDF 格式导出 Jupyter 笔记本。

Update更新

Also, you should consider that nbconvert is compatible with Python 3.7-3.9 based on the official doc .此外,您应该考虑nbconvert与基于官方文档的Python 3.7-3.9 兼容。

You can try following URL.您可以尝试以下网址。 Hope it will solve your issue希望它能解决你的问题

https://code.visualstudio.com/docs/datascience/jupyter-notebooks#_export-your-jupyter-notebook https://code.visualstudio.com/docs/datascience/jupyter-notebooks#_export-your-jupyter-notebook

I just tried in Linux(Ubuntu 20.04) and it worked for me我刚刚在 Linux(Ubuntu 20.04)中尝试过,它对我有用

You can follow this steps:您可以按照以下步骤操作:

  1. sudo apt-get install texlive-xetex texlive-fonts-recommended texlive-latex-recommended
  2. Active env where you have jupyter installed安装了 jupyter 的活动环境
  3. Execute this command: jupyter nbconvert --to pdf your_file.ipynb执行这个命令: jupyter nbconvert --to pdf your_file.ipynb

Output:输出: 在此处输入图像描述

If you are using arch linux or any child distro of arch,如果您使用的是 arch linux 或任何 arch 的子发行版,

❯ sudo pacman -S texlive-latexextra texlive-core ❯ sudo pacman -S texlive-latexextra texlive-core

That fixed it for me那为我修好了

While this question has been answered twice neither one of the answers seemed to address the question on OP's OS.虽然这个问题已经回答了两次,但似乎没有一个答案能解决 OP 操作系统上的问题。 I wanted to setup the same thing for a more streamlined workflow on Windows Jupyter development.我想为 Windows Jupyter 开发中更简化的工作流程设置相同的东西。

Unfortunately, I have been unabled to get the export as PDF button to work due to not knowing how to get VS Code to search the correct locations for the various dependencies;不幸的是,由于不知道如何让 VS Code 搜索各种依赖项的正确位置,我无法让导出为 PDF 按钮工作; however, I have found a work around that allows one to export as PDF on Windows through WSL (Windows Subsystem for Linux).但是,我找到了一种解决方法,允许通过 WSL(Linux 的 Windows 子系统)在 Windows 上导出为 PDF。

WSL is a tool that I have grown to love using during all my development both professionally and within school, so I am sure you can find great use for it outside of PDF export if you do not already have it installed. WSL 是我在专业和学校的所有发展过程中逐渐喜欢使用的工具,因此我相信如果您还没有安装它,您可以在 PDF 导出之外找到它的巨大用途。

This method primarily uses the command line method for exporting using jupyter nbconvert --to pdf <filename>此方法主要使用命令行方式使用jupyter nbconvert --to pdf <filename>导出

This can also be used in Windows powershell;这也可以在 Windows powershell 中使用; however, I have had little luck setting up the dependencies in powershell.但是,我在 powershell 中设置依赖项时运气不佳。

In WSL you can run this command and use the friendly neighbourhood Linux command line to install all the required dependencies.在 WSL 中,您可以运行此命令并使用友好的社区 Linux 命令行来安装所有必需的依赖项。

Here is the documentation on how to setup nbconvert for Linux which I also found to work on WSL ( https://nbconvert.readthedocs.io/en/latest/install.html )这是有关如何为 Linux 设置 nbconvert 的文档,我还发现它可以在 WSL 上工作( https://nbconvert.readthedocs.io/en/latest/install.html

If there are any other dependencies missing it is typically fairly easy to figure out how to install these through the command line.如果缺少任何其他依赖项,通常很容易弄清楚如何通过命令行安装这些依赖项。

Using anaconda for virtual environments on linux (Ubuntu 21.10), I needed to install these packages within the venv:在 linux (Ubuntu 21.10) 上将 anaconda 用于虚拟环境,我需要在 venv 中安装这些软件包:

conda activate <NAME_OF_VENV>
pip install notebook
conda install nbconvert
conda install pandoc
conda deactivate

I already had texlive installed, but I was missing the xetex packages:我已经安装了texlive ,但是我缺少xetex包:

sudo apt install texlive-xetex

These were all required for me , as stated by the error messages given by VSCode正如 VSCode 给出的错误消息所述,这些对我来说都是必需的

I am using a Debian distribution installed in Windows Subsystem Linux 2 with a Conda environment with a 3.7 Python version.我正在使用安装在Windows 子系统 Linux 2中的Debian发行版和带有3.7 Python版本的Conda环境。 I found the following error in the jupyter output while exporting a jupyter file:我在导出 jupyter 文件时在 jupyter 输出中发现以下错误:

! LaTeX Error: File `ucharcat.sty' not found

I solved the issue just by upgrading the Debian distribution from Debian 9 (strecht) to bullseye and re-installing latex in the upgraded distribution.我只是通过将 Debian 发行版从 Debian 9 ( strecht ) 升级到 Bullseye 并在升级后的发行版中重新安装 Latex 解决了这个问题。

I found that the Windows Store installs Debian 9 (still in 2022) by default and therefore the LaTex version installed with the apt tool package dates from 2016. Perhaps there is a compatibility issue with Python 3.7 and the nbconvert package used to export to pdf files.我发现Windows 应用商店默认安装 Debian 9(仍在 2022 年),因此使用apt工具包安装的 LaTex 版本可以追溯到 2016 年。可能存在与 Python 3.7 和用于导出到 pdf 文件的nbconvert包的兼容性问题.

The code I used to我以前的代码

  • Check my Linux, and LaTex versions检查我的 Linux 和 LaTex 版本
  • Upgrade the Debian WSL distro升级 Debian WSL 发行版
  • Uninstall and re-install LaTex卸载并重新安装 LaTex

can be found in the following link可以在以下链接中找到

I hope this answer helps.我希望这个答案有帮助。

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

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