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)
主要特性
- 智能代码换行:自动对长代码行进行换行,而不是截断
- 优化的字体大小:使用合适的字体大小,确保代码可读性
- 防止代码块分割:确保代码块不会在页面之间被分割
- 横向布局:默认使用横向布局以提供更多水平空间
- 语法高亮保留:在PDF中保留代码的语法高亮
- 适合中文环境:完全支持中文和其他非拉丁字符
要求
- Python 3.7+
- Jupyter Notebook
- nbconvert 6.0+
- Playwright
常见问题
安装问题
-
安装Playwright失败
# 尝试单独安装 pip install playwright playwright install chromium
-
nbconvert依赖问题
确保您的环境中已安装了完整的Jupyter套件:
pip install jupyter nbconvert
使用问题
-
PDF生成为空白或代码缺失
尝试增加等待时间:
options = {'wait_time': 5000} # 5秒 convert_notebook('notebook.ipynb', options=options)
-
中文字符显示为方块
确保您的环境中已安装了中文字体,或者尝试使用不同的字体。
版本历史
0.1.1 (2025-03-10)
- 修复:解决代码单元格在转换过程中丢失的问题
- 改进:确保输入单元格(代码段)在HTML和PDF中正确显示
0.1.0
- 初始版本
- 实现基本的Jupyter Notebook转PDF功能
- 支持自定义CSS样式和页面设置
许可证
MIT
作者
WSY
Metadata
Release files for wshuyi-nb2pdf 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| wshuyi_nb2pdf-0.1.1.tar.gz | 6.4 kB | Without provenance | Details for wshuyi_nb2pdf-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| wshuyi_nb2pdf-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for wshuyi_nb2pdf-0.1.1-py3-none-any.whl |
Total release size: 14.2 kB
Release files / wshuyi_nb2pdf-0.1.1.tar.gz
| Download URL | wshuyi_nb2pdf-0.1.1.tar.gz |
|---|---|
| Size | 6.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ef709497718eabe3f96eec0fc948a4adbc51530ce1d87a4d088c209dcc1d19cb
|
|
BLAKE2b-256 checksum How to use checksums |
40b89e26dae2e8fb87ba781aaa4edd3a4e2ad9fe741714fc0ae4fdc53932212a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|
Release files / wshuyi_nb2pdf-0.1.1-py3-none-any.whl
| Download URL | wshuyi_nb2pdf-0.1.1-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dcadc2ceefb4964b1f27c10b1c97af30cbdf620f9748ac4cac37a54e282b064a
|
|
BLAKE2b-256 checksum How to use checksums |
70374c395df3b4cd9217debb6c10a9062be3e2977eef1805fce158159cf56c4d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|
Security for wshuyi-nb2pdf 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.