How to Export a OneNote File to PDF in Python
Aspose.Note FOSS for Python enables programmatic PDF export of Microsoft OneNote .one section files without requiring Microsoft Office or any operating-system-level document converter. Export is handled by the Document.Save() method backed by the optional ReportLab PDF renderer.
Benefits
- Server-friendly: runs on any OS, including headless Linux servers and CI/CD containers
- Stream-capable: save directly to an
io.BytesIObuffer, no temporary file needed - Free and open-source: MIT license
Prerequisites
PDF export requires the optional ReportLab dependency. Install it via the [pdf] extra:
pip install "aspose-note[pdf]"If you already have aspose-note installed without the extra:
pip install --upgrade "aspose-note[pdf]"Verify that ReportLab is available:
python -c "import reportlab; print(reportlab.Version)"Step-by-Step Guide
Step 1: Install aspose-note with PDF Support
pip install "aspose-note[pdf]"Confirm the install (PDF is an export-only format — the library produces PDF output from OneNote source files; the reverse direction is not supported):
from aspose.note import Document, SaveFormat
print("aspose-note is ready.")Step 2: Load the OneNote File
from aspose.note import Document
doc = Document("MyNotes.one")Step 3: Export the Entire Document to PDF
The simplest export, covering all pages with default settings:
from aspose.note import Document, SaveFormat
doc = Document("MyNotes.one")
doc.Save("output.pdf", SaveFormat.Pdf)
print("PDF saved to output.pdf")Step 4: Use PdfSaveOptions
PdfSaveOptions lets you configure PDF export settings:
from aspose.note import Document
from aspose.note.saving import PdfSaveOptions
doc = Document("MyNotes.one")
opts = PdfSaveOptions()
doc.Save("output.pdf", opts)Available PdfSaveOptions
| Option | Type | Default | Description |
|---|---|---|---|
PageIndex | int | 0 | Zero-based index of the first page to export (0 = first page) |
PageCount | int | None | None | Number of pages to export from PageIndex; None exports all remaining pages |
Step 5: Export to an In-Memory Stream
Document.Save() accepts a binary stream directly: no temporary file needed:
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")Step 6: Batch Export Multiple Files
Process multiple .one files in a directory:
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}")Common Issues and Fixes
1. ImportError: No module named ‘reportlab’
Cause: The [pdf] extra was not installed.
Fix:
pip install "aspose-note[pdf]"2. UnsupportedSaveFormatException
Cause: A format other than SaveFormat.Pdf was used. Only SaveFormat.Pdf is implemented.
Fix: Always use SaveFormat.Pdf for export. Other formats are declared for API compatibility but raise UnsupportedSaveFormatException.
3. IncorrectPasswordException
Cause: The .one file is encrypted. Encrypted documents are not supported.
Fix: Use an unencrypted .one file. The commercial Aspose.Note product supports encryption.
4. FileNotFoundError
Cause: The input .one file path is incorrect.
Fix: Use pathlib.Path.exists() to validate before loading:
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. Output PDF is blank or empty
Cause: The .one file contains pages but no text content (only images or tables with no text). The PDF renderer produces pages based on what ReportLab can render from the DOM.
Fix: Verify page content before exporting:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
text_count = len(doc.GetChildNodes(RichText))
print(f"RichText nodes found: {text_count}")Frequently Asked Questions
Which save formats are supported?
Only SaveFormat.Pdf is currently implemented. The SaveFormat enum has exactly one member: SaveFormat.Pdf.
Can I export to a stream instead of a file?
Yes. Document.Save() accepts any writable binary stream as its first argument:
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()Does export preserve page order?
Yes. Pages are exported in the same order they appear in the DOM (the order returned by iterating the Document).
Is PDF export available on Linux?
Yes. ReportLab and Aspose.Note FOSS for Python are both OS-independent.
Can I export a subset of pages?
Yes. Use PdfSaveOptions with PageIndex (zero-based start page) and PageCount (number of pages to export; None = all remaining) to export a subset of pages. Both fields are forwarded to the PDF exporter in v26.3.2.
Related Resources: