📜  在您想要将笔记本转换为 PDF 或 HTML 文件以共享它时,在单元格中运行此代码,这意味着此代码应在笔记本的最后一个单元格中运行. - 朱莉娅(1)

📅  最后修改于: 2023-12-03 15:37:42.310000             🧑  作者: Mango

将笔记本转换为 PDF 或 HTML 文件的Python脚本

在您想要将笔记本转换为 PDF 或 HTML 文件以共享它时,在单元格中运行此代码,这意味着此代码应在笔记本的最后一个单元格中运行.

使用 Python 的 nbconvert 库可以将 Jupyter 笔记本转换为 PDF 或 HTML 文件。下面是一个示例脚本,可以将当前笔记本转换为 PDF 或 HTML 文件。

import os
import sys
import subprocess
from nbconvert import HTMLExporter, PDFExporter
from nbconvert.exporters.export import ExporterNotFoundException

# Set the output format (html or pdf)
output_format = 'html'

# Set the output file name
output_file = 'output'

# Set the path to the LaTeX template (only needed for PDF output)
latex_template_path = ''

# Set the path to the Pandoc executable (only needed for PDF output on Windows)
pandoc_path = ''

# Create the exporter
if output_format == 'html':
    exporter = HTMLExporter()
elif output_format == 'pdf':
    try:
        exporter = PDFExporter(template_file=latex_template_path)
    except ExporterNotFoundException:
        sys.stderr.write('Error: Could not find the LaTeX template file.\n')
        sys.exit(1)
else:
    sys.stderr.write('Error: Invalid output format specified.\n')
    sys.exit(1)

# Export the notebook
try:
    if output_format == 'html':
        output = exporter.from_notebook_node(notebook_node=get_ipython().nbformat, resources={},)

        with open(output_file + '.html', 'w') as f:
            f.write(output[0])

    elif output_format == 'pdf':
        output, resources = exporter.from_notebook_node(notebook_node=get_ipython().nbformat, resources={},)
        resources['metadata']['pdfurl'] = 'http://localhost:8888/notebooks/Untitled.ipynb'

        if sys.platform == 'win32':
            converter = os.path.join(os.path.dirname(pandoc_path), 'pandoc.exe')
            subprocess.call([converter, '-f', 'html', '-t', 'latex', '-o', output_file + '.pdf', '--pdf-engine=xelatex'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        else:
            subprocess.call(['pandoc', '-f', 'html', '-t', 'latex', '-o', output_file + '.pdf', '--pdf-engine=xelatex'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        subprocess.call(['rm', output_file + '.tex'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    print('Exported successfully!')

except Exception as e:
    sys.stderr.write('Error: An unexpected error occurred while exporting the notebook.\n' + str(e) + '\n')
    sys.exit(1)
参数说明
  • output_format:导出文件的格式。目前支持的只有 HTML 和 PDF 两种。
  • output_file:导出文件的文件名。
  • latex_template_path:LaTeX模板文件的路径,在导出 PDF 文件时需要。
  • pandoc_path:Pandoc 可执行文件的路径,在导出 PDF 文件时需要(仅在 Windows 上)。
如何使用
  1. 将上面的代码拷贝到 Jupyter 笔记本的最后一个单元格中。
  2. 根据需要修改参数。
  3. 运行单元格。
返回的代码片段
import os
import sys
import subprocess
from nbconvert import HTMLExporter, PDFExporter
from nbconvert.exporters.export import ExporterNotFoundException

# Set the output format (html or pdf)
output_format = 'html'

# Set the output file name
output_file = 'output'

# Set the path to the LaTeX template (only needed for PDF output)
latex_template_path = ''

# Set the path to the Pandoc executable (only needed for PDF output on Windows)
pandoc_path = ''

# Create the exporter
if output_format == 'html':
    exporter = HTMLExporter()
elif output_format == 'pdf':
    try:
        exporter = PDFExporter(template_file=latex_template_path)
    except ExporterNotFoundException:
        sys.stderr.write('Error: Could not find the LaTeX template file.\n')
        sys.exit(1)
else:
    sys.stderr.write('Error: Invalid output format specified.\n')
    sys.exit(1)

# Export the notebook
try:
    if output_format == 'html':
        output = exporter.from_notebook_node(notebook_node=get_ipython().nbformat, resources={},)

        with open(output_file + '.html', 'w') as f:
            f.write(output[0])

    elif output_format == 'pdf':
        output, resources = exporter.from_notebook_node(notebook_node=get_ipython().nbformat, resources={},)
        resources['metadata']['pdfurl'] = 'http://localhost:8888/notebooks/Untitled.ipynb'

        if sys.platform == 'win32':
            converter = os.path.join(os.path.dirname(pandoc_path), 'pandoc.exe')
            subprocess.call([converter, '-f', 'html', '-t', 'latex', '-o', output_file + '.pdf', '--pdf-engine=xelatex'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        else:
            subprocess.call(['pandoc', '-f', 'html', '-t', 'latex', '-o', output_file + '.pdf', '--pdf-engine=xelatex'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        subprocess.call(['rm', output_file + '.tex'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    print('Exported successfully!')

except Exception as e:
    sys.stderr.write('Error: An unexpected error occurred while exporting the notebook.\n' + str(e) + '\n')
    sys.exit(1)

注:此处返回的是 Markdown 代码片段,因此需要在 Jupyter 笔记本中单独运行。