首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用colab_pdf将Python转换为Pdf

使用colab_pdf将Python转换为Pdf
EN

Stack Overflow用户
提问于 2022-08-19 12:57:16
回答 1查看 412关注 0票数 1

我正在尝试将我的ipython笔记本(.ipynb)文件转换为pdf。我经常使用这种转换方法:

  1. 将.ipynb文件保存在驱动器/mydrive/Colab笔记本中
  2. 安装驱动器 从google.colab导入驱动器drive.mount('/content/gdrive')
  3. 下载pdf !wget -nc pdf.py from colab_pdf import colab_pdf colab_pdf('mynotebookname.ipynb')

但是这个方法已经行不通了。当我试图转换时,我会遇到这样的问题:

错误:

-2022-08-19 12:47:01185.199.108.133,185.199.109.133,185.199.110.133,连接到(raw.githubusercontent.com)|185.199.108.133|:443... raw.githubusercontent.com已连接。HTTP请求已发送,正在等待响应...200确定长度:1864年(1.8K)文本/普通保存到:‘colab_pdf.py’

colab_pdf.py 100%===================> 1.82K --.-KB/s

2022-08-19 12:47:01 (30.7MB/s)-“colab_pdf.py”保存了1864年/1864年

警告: apt没有稳定的CLI接口。在脚本中谨慎使用。

警告: apt没有稳定的CLI接口。在脚本中谨慎使用。

这个应用程序用于将笔记本文件( NbConvertApp *.ipynb)转换成各种其他格式。

代码语言:javascript
运行
复制
    WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.

选项

代码语言:javascript
运行
复制
The options below are convenience aliases to configurable class-options,
as listed in the "Equivalent to" description-line of the aliases.
To see all configurable class-options for some <cmd>, use:
    <cmd> --help-all

--debug
    set log level to logging.DEBUG (maximize logging output)
    Equivalent to: [--Application.log_level=10]
--show-config
   Show the application's configuration (human-readable format)
   Equivalent to: [--Application.show_config=True]
--show-config-json
   Show the application's configuration (json format)
   Equivalent to: [--Application.show_config_json=True]
--generate-config
   generate default config file
   Equivalent to: [--JupyterApp.generate_config=True]
-y
   Answer yes to any questions instead of prompting.
   Equivalent to: [--JupyterApp.answer_yes=True]
--execute
   Execute the notebook prior to export.
   Equivalent to: [--ExecutePreprocessor.enabled=True]
--allow-errors
   Continue notebook execution even if one of the cells throws an error and include the 
   error message in the cell output (the default behaviour is to abort conversion). This 
   flag is only relevant if '--execute' was specified, too.
   Equivalent to: [--ExecutePreprocessor.allow_errors=True]
--stdin
   read a single notebook file from stdin. Write the resulting notebook with default 
   basename 'notebook.*'
   Equivalent to: [--NbConvertApp.from_stdin=True]
--stdout
   Write notebook output to stdout instead of files.
   Equivalent to: [--NbConvertApp.writer_class=StdoutWriter]
--inplace
   Run nbconvert in place, overwriting the existing notebook (only 
        relevant when converting to notebook format)
   Equivalent to: [--NbConvertApp.use_output_suffix=False -- 
   NbConvertApp.export_format=notebook --FilesWriter.build_directory=]
--clear-output
   Clear output of current file and save in place, 
        overwriting the existing notebook.
   Equivalent to: [--NbConvertApp.use_output_suffix=False -- 
  NbConvertApp.export_format=notebook --FilesWriter.build_directory= -- 
  ClearOutputPreprocessor.enabled=True]
--no-prompt
  Exclude input and output prompts from converted document.
  Equivalent to: [--TemplateExporter.exclude_input_prompt=True -- 
  TemplateExporter.exclude_output_prompt=True]
--no-input
  Exclude input cells and output prompts from converted document. 
        This mode is ideal for generating code-free reports.
  Equivalent to: [--TemplateExporter.exclude_output_prompt=True -- 
  TemplateExporter.exclude_input=True]
--log-level=<Enum>
   Set the log level by value or name.
   Choices: any of [0, 10, 20, 30, 40, 50, 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL']
   Default: 30
   Equivalent to: [--Application.log_level]
--config=<Unicode>
Full path of a config file.
Default: ''
Equivalent to: [--JupyterApp.config_file]
--to=<Unicode>
The export format to be used, either one of the built-in formats
        ['asciidoc', 'custom', 'html', 'latex', 'markdown', 'notebook', 'pdf', 'python', 
'rst', 'script', 'slides']
        or a dotted object name that represents the import path for an
        `Exporter` class
Default: 'html'
Equivalent to: [--NbConvertApp.export_format]
--template=<Unicode>
Name of the template file to use
Default: ''
Equivalent to: [--TemplateExporter.template_file]
--writer=<DottedObjectName>
Writer class used to write the 
                                    results of the conversion
Default: 'FilesWriter'
Equivalent to: [--NbConvertApp.writer_class]
--post=<DottedOrNone>
PostProcessor class used to write the
                                    results of the conversion
Default: ''
Equivalent to: [--NbConvertApp.postprocessor_class]
--output=<Unicode>
overwrite base name use for output files.
            can only be used when converting one notebook at a time.
Default: ''
Equivalent to: [--NbConvertApp.output_base]
--output-dir=<Unicode>
Directory to write output(s) to. Defaults
                              to output to the directory of each notebook. To recover
                              previous default behaviour (outputting to the current 
                              working directory) use . as the flag value.
Default: ''
Equivalent to: [--FilesWriter.build_directory]
--reveal-prefix=<Unicode>
The URL prefix for reveal.js (version 3.x).
        This defaults to the reveal CDN, but can be any url pointing to a copy 
        of reveal.js. 
        For speaker notes to work, this must be a relative path to a local 
        copy of reveal.js: e.g., "reveal.js".
        If a relative path is given, it must be a subdirectory of the
        current directory (from which the server is run).
        See the usage documentation
        (https://nbconvert.readthedocs.io/en/latest/usage.html#reveal-js-html-slideshow)
        for more details.
Default: ''
Equivalent to: [--SlidesExporter.reveal_url_prefix]
--nbformat=<Enum>
The nbformat version to write.
        Use this to downgrade notebooks.
Choices: any of [1, 2, 3, 4]
Default: 4
Equivalent to: [--NotebookExporter.nbformat_version]

示例

代码语言:javascript
运行
复制
The simplest way to use nbconvert is

        > jupyter nbconvert mynotebook.ipynb

        which will convert mynotebook.ipynb to the default format (probably HTML).

        You can specify the export format with `--to`.
        Options include ['asciidoc', 'custom', 'html', 'latex', 'markdown', 'notebook', 'pdf', 'python', 'rst', 'script', 'slides'].

        > jupyter nbconvert --to latex mynotebook.ipynb

        Both HTML and LaTeX support multiple output templates. LaTeX includes
        'base', 'article' and 'report'.  HTML includes 'basic' and 'full'. You
        can specify the flavor of the format used.

        > jupyter nbconvert --to html --template basic mynotebook.ipynb

        You can also pipe the output to stdout, rather than a file

        > jupyter nbconvert mynotebook.ipynb --stdout

        PDF is generated via latex

        > jupyter nbconvert mynotebook.ipynb --to pdf

        You can get (and serve) a Reveal.js-powered slideshow

        > jupyter nbconvert myslides.ipynb --to slides --post serve

        Multiple notebooks can be given at the command line in a couple of 
        different ways:

        > jupyter nbconvert notebook*.ipynb
        > jupyter nbconvert notebook1.ipynb notebook2.ipynb

        or you can specify the notebooks list in a config file, containing::

            c.NbConvertApp.notebooks = ["my_notebook.ipynb"]

        > jupyter nbconvert --config mycfg.py

要查看所有可用的可配置性,请使用--help-all

文件下载失败。保存在Google中

在这件事上你能帮我一下吗?提前谢谢。任何帮助都将不胜感激。:)

EN

Stack Overflow用户

回答已采纳

发布于 2022-08-20 07:01:29

我不知道问题出在哪里。不过,您可以直接用nbconvert生成pdf。

代码语言:javascript
运行
复制
# Mount the drive
from google.colab import drive
drive.mount('/content/gdrive')

# Go to the directory where your file is
%cd /content/drive/MyDrive/Colab\ Notebooks/

# Verify that now you see your notebooks
!ls

# Convert
!jupyter nbconvert --to pdf my_notebook.ipynb

\ in /Colab\ Notebooks需要向路径内的空间发出信号。与使用%cd不同,您还可以使用以下方法,避免插入\

代码语言:javascript
运行
复制
import os
os.chdir("/content/gdrive/MyDrive/Colab Notebooks/")

更新:根据要下载的笔记本,可能需要安装其他软件包。例如,您可能需要安装:

代码语言:javascript
运行
复制
!apt-get install texlive-xetex texlive-fonts-recommended texlive-plain-generic

您可以找到完整的列表这里

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73417137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档