Skip to main content

将Jupyter Notebook转换为优化的PDF格式,确保代码完整显示且不被截断

Project description

wshuyi-nb2pdf

将Jupyter Notebook转换为优化的PDF格式,特别关注:

  • 确保代码不会在右侧被截断
  • 使用合适的字体大小
  • 优化分页和布局
  • 支持中文和其他非拉丁字符

安装

# 从PyPI安装
pip install wshuyi-nb2pdf

# 安装Playwright浏览器依赖
playwright install chromium

使用方法

命令行使用

# 基本用法
wshuyi-nb2pdf notebook.ipynb

# 指定输出文件
wshuyi-nb2pdf notebook.ipynb output.pdf

# 使用纵向布局
wshuyi-nb2pdf --portrait notebook.ipynb

# 调整缩放比例
wshuyi-nb2pdf --scale 0.9 notebook.ipynb

# 使用不同纸张大小
wshuyi-nb2pdf --paper-size A4 notebook.ipynb

# 查看所有选项
wshuyi-nb2pdf --help

Python中使用

from wshuyi_nb2pdf import convert_notebook

# 基本用法
convert_notebook('notebook.ipynb')

# 指定输出文件
convert_notebook('notebook.ipynb', 'output.pdf')

# 自定义选项
options = {
    'landscape': True,
    'scale': 0.95,
    'format': 'Letter',
    'margin': {"top": "0.5in", "right": "0.5in", "bottom": "0.5in", "left": "0.5in"},
    'wait_time': 2000,
}
convert_notebook('notebook.ipynb', 'output.pdf', options=options)

主要特性

  1. 智能代码换行:自动对长代码行进行换行,而不是截断
  2. 优化的字体大小:使用合适的字体大小,确保代码可读性
  3. 防止代码块分割:确保代码块不会在页面之间被分割
  4. 横向布局:默认使用横向布局以提供更多水平空间
  5. 语法高亮保留:在PDF中保留代码的语法高亮
  6. 适合中文环境:完全支持中文和其他非拉丁字符

要求

  • Python 3.7+
  • Jupyter Notebook
  • nbconvert 6.0+
  • Playwright

常见问题

安装问题

  1. 安装Playwright失败

    # 尝试单独安装
    pip install playwright
    playwright install chromium
    
  2. nbconvert依赖问题

    确保您的环境中已安装了完整的Jupyter套件:

    pip install jupyter nbconvert
    

使用问题

  1. PDF生成为空白或代码缺失

    尝试增加等待时间:

    options = {'wait_time': 5000}  # 5秒
    convert_notebook('notebook.ipynb', options=options)
    
  2. 中文字符显示为方块

    确保您的环境中已安装了中文字体,或者尝试使用不同的字体。

许可证

MIT

作者

WSY

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wshuyi_nb2pdf-0.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wshuyi_nb2pdf-0.1.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file wshuyi_nb2pdf-0.1.0.tar.gz.

File metadata

  • Download URL: wshuyi_nb2pdf-0.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for wshuyi_nb2pdf-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bd8d27e446dbe4db80eb89afad509885cba212447f2731c391fde274028ea836
MD5 e6c1277b112f4a5c54636a0612dbe0fc
BLAKE2b-256 339d06d891c8a178419e2739a020e1ee43800a8abaca25c04b179d7a82f564e0

See more details on using hashes here.

File details

Details for the file wshuyi_nb2pdf-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: wshuyi_nb2pdf-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for wshuyi_nb2pdf-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b4bbb115b79a3cea837603670ba0afb60fe1400733eb655ab9814351d12d2413
MD5 384a758fae2a7ae461c592af75e1457a
BLAKE2b-256 60a63c6ac9117a9daa60dd41fe3776601e7dfb85437ca46e8d16b1c8fecec801

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page