如何在 Python 中将 OneNote 文件导出为 PDF
Aspose.Note FOSS for Python 使能够以编程方式将 Microsoft OneNote .one 部分文件导出为 PDF,无需 Microsoft Office 或任何操作系统级别的文档转换器。导出由 Document.Save() 方法处理,依赖可选的 ReportLab PDF 渲染器。
好处
- 服务器友好:在任何操作系统上运行,包括无头 Linux 服务器和 CI/CD 容器
- 流式支持:直接保存到
io.BytesIO缓冲区,无需临时文件 - 免费且开源:MIT 许可证
先决条件
PDF 导出需要可选的 ReportLab 依赖。通过 [pdf] 额外组件进行安装:
pip install "aspose-note[pdf]"如果您已经安装了aspose-note且没有额外的组件:
pip install --upgrade "aspose-note[pdf]"验证 ReportLab 是否可用:
python -c "import reportlab; print(reportlab.Version)"逐步指南
步骤 1:安装带 PDF 支持的 aspose-note
pip install "aspose-note[pdf]"确认安装:
from aspose.note import Document, SaveFormat
print("Ready for PDF export.")步骤 2:加载 OneNote 文件
from aspose.note import Document
doc = Document("MyNotes.one")步骤 3:将整个文档导出为 PDF
最简单的导出,使用默认设置覆盖所有页面:
from aspose.note import Document, SaveFormat
doc = Document("MyNotes.one")
doc.Save("output.pdf", SaveFormat.Pdf)
print("PDF saved to output.pdf")步骤 4:使用 PdfSaveOptions
PdfSaveOptions 让您配置 PDF 导出设置:
from aspose.note import Document
from aspose.note.saving import PdfSaveOptions
doc = Document("MyNotes.one")
opts = PdfSaveOptions()
doc.Save("output.pdf", opts)可用的 PdfSaveOptions
| 选项 | 类型 | 默认 | 描述 |
|---|---|---|---|
PageIndex | int | 0 | 字段存在;在 v26.3.1 中未转发至 PDF 导出器:无效 |
PageCount | int | None | None | 字段存在;在 v26.3.1 中未转发至 PDF 导出器:无效 |
步骤 5:导出到内存流
Document.Save() 直接接受二进制流:无需临时文件:
import io
from aspose.note import Document, SaveFormat
from aspose.note.saving import PdfSaveOptions
doc = Document("MyNotes.one")
buf = io.BytesIO()
doc.Save(buf, PdfSaveOptions())
pdf_bytes = buf.getvalue()
print(f"PDF size: {len(pdf_bytes)} bytes")步骤 6:批量导出多个文件
处理目录中的所有 .one 文件:
from pathlib import Path
from aspose.note import Document, SaveFormat
input_dir = Path("./onenote_files")
output_dir = Path("./pdf_output")
output_dir.mkdir(exist_ok=True)
for one_file in input_dir.glob("*.one"):
doc = Document(str(one_file))
out_path = output_dir / one_file.with_suffix(".pdf").name
doc.Save(str(out_path), SaveFormat.Pdf)
print(f"Exported: {one_file.name} -> {out_path.name}")常见问题及解决方案
1. ImportError: 没有名为 ‘reportlab’ 的模块
原因: 未安装 [pdf] 额外组件。
修复:
pip install "aspose-note[pdf]"2. UnsupportedSaveFormatException
Cause: 使用了除 SaveFormat.Pdf 之外的格式。仅实现了 SaveFormat.Pdf。
修复:始终使用 SaveFormat.Pdf 进行导出。其他格式仅为 API 兼容性声明,但会触发 UnsupportedSaveFormatException。
3. IncorrectPasswordException
原因: .one 文件已加密。不支持加密文档。
修复: 使用未加密的 .one 文件。商业版 Aspose.Note 产品支持加密。
4. FileNotFoundError
原因: 输入 .one 文件路径不正确。
修复:使用 pathlib.Path.exists() 在加载前进行验证:
from pathlib import Path
from aspose.note import Document, SaveFormat
path = Path("MyNotes.one")
assert path.exists(), f"File not found: {path.resolve()}"
doc = Document(str(path))
doc.Save("output.pdf", SaveFormat.Pdf)5. 输出 PDF 为空白或为空
原因: .one 文件包含页面但没有文本内容(仅有图像或没有文本的表格)。PDF 渲染器根据 ReportLab 能从 DOM 渲染的内容生成页面。
修复:导出前验证页面内容:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
text_count = len(doc.GetChildNodes(RichText))
print(f"RichText nodes found: {text_count}")常见问题
哪些保存格式受支持?
目前仅实现了 SaveFormat.Pdf。SaveFormat 枚举恰好只有一个成员:SaveFormat.Pdf。
我可以导出到流而不是文件吗?
是的。Document.Save() 接受任何可写的二进制流作为其第一个参数:
import io
from aspose.note import Document, PdfSaveOptions, SaveFormat
doc = Document("MyNotes.one")
buf = io.BytesIO()
doc.Save(buf, PdfSaveOptions())
pdf_bytes = buf.getvalue()导出是否保留页面顺序?
是的。页面按照它们在 DOM 中出现的顺序导出(即通过遍历 Document 返回的顺序)。
在 Linux 上是否提供 PDF 导出?
是的。ReportLab 和 Aspose.Note FOSS for Python 都是跨操作系统的。
我可以导出页面的子集吗?
PdfSaveOptions.PageIndex 和 PageCount 字段存在,但在 v26.3.1 中未转发给 PDF 导出器,且没有任何作用:整个文档始终会被导出。
相关资源: