How to Extract Text from OneNote Files in Python
Microsoft OneNote .one files are binary documents that cannot be read as plain text or parsed with generic XML tools. Aspose.Note FOSS for Python provides a pure-Python parser that loads .one files into a full document object model (DOM), making it straightforward to extract text, formatting metadata, and hyperlinks programmatically.
Benefits of Using Aspose.Note FOSS for Python
- No Microsoft Office required: read
.onefiles on any platform, including Linux CI/CD servers - Full text and formatting access: plain text, bold/italic/underline runs, font properties, and hyperlink URLs
- Free and open-source: MIT license, no usage fees or API keys
Step-by-Step Guide
Step 1: Install Aspose.Note FOSS for Python
Install the library from PyPI. The core package has no mandatory dependencies:
pip install aspose-noteVerify the installation:
from aspose.note import Document
print("Installation OK")Step 2: Load the .one File
Create a Document instance by passing the file path:
from aspose.note import Document
doc = Document("MyNotes.one")
print(f"Section: {doc.DisplayName}")
# Use the enumerator to iterate over child nodes (e.g., pages)
enumerator = doc.GetEnumerator()
print(f"Pages: {len(list(enumerator))}")To load from a binary stream (e.g. from cloud storage or an HTTP response):
from aspose.note import Document
with open("MyNotes.one", "rb") as f:
doc = Document(f)Step 3: Extract All Plain Text
Use GetChildNodes(RichText) to collect every RichText node in the document tree. This performs a recursive depth-first search across all pages, outlines, and outline elements:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
texts = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]
for text in texts:
print(text)To save the extracted text to a file:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
texts = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]
with open("extracted_text.txt", "w", encoding="utf-8") as out:
out.write("\n".join(texts))
print(f"Wrote {len(texts)} text blocks to extracted_text.txt")Step 4: Inspect Formatted Runs
Each RichText node contains a TextRuns list of TextRun segments. Each run carries an independent TextStyle with per-character formatting:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
for run in rt.TextRuns:
style = run.Style
attrs = []
if style.IsBold: attrs.append("bold")
if style.IsItalic: attrs.append("italic")
if style.IsUnderline: attrs.append("underline")
if style.IsStrikethrough: attrs.append("strikethrough")
```python
from aspose.note import Document, TextRun
doc = Document("MyNotes.one")
# Iterate over TextRun nodes and access their Text property
for run in doc.GetChildNodes(TextRun, True):
print(f"{run.Text!r}")for rt in doc.GetChildNodes(RichText): for run in rt.TextRuns: if run.Style.IsHyperlink and run.Style.HyperlinkAddress: print(f"Link text: {run.Text!r}") print(f"URL: {run.Style.HyperlinkAddress}")
---
### Step 6: Extract Text Per Page
To extract text organized by page title:
```python
from aspose.note import Document, Page, RichText
doc = Document("MyNotes.one")
for page in doc.GetChildNodes(Page):
title = (
page.Title.TitleText.Text
if page.Title and page.Title.TitleText
else "(untitled)"
)
print(f"\n=== {title} ===")
for rt in page.GetChildNodes(RichText):
if rt.Text:
print(rt.Text)<button class=“hextra-code-copy-btn hx-group/copybtn hx-transition-all active:hx-opacity-50 hx-bg-primary-700/5 hx-border hx-border-black/5 hx-text-gray-600 hover:hx-text-gray-900 hx-rounded-md hx-p-1.5 dark:hx-bg-primary-300/10 dark:hx-border-white/10 dark:hx-text-gray-400 dark:hover:hx-text-gray-50” title=“Copy code”
<div class="copy-icon group-[.copied]/copybtn:hx-hidden hx-pointer-events-none hx-h-4 hx-w-4"></div>
<div class="success-icon hx-hidden group-[.copied]/copybtn:hx-block hx-pointer-events-none hx-h-4 hx-w-4"></div>
Common Issues and Fixes
1. ImportError: No module named ‘aspose’
Cause: The package is not installed in the active Python environment. Fix: Install the package (see Installation), then confirm the active environment:
pip show aspose-note2. FileNotFoundError when loading .one file
Cause: The file path is incorrect or the file does not exist.
Fix: Use an absolute path or verify the file exists before loading:
from pathlib import Path
from aspose.note import Document
path = Path("MyNotes.one")
if not path.exists():
raise FileNotFoundError(f"File not found: {path.resolve()}")
The `Document` API surface has changed in the current source model; review its reference page before relying on older member details.
doc = Document(str(path))3. UnicodeEncodeError on Windows when printing
Cause: Windows terminals may use a legacy encoding that cannot render Unicode characters.
Fix: Reconfigure stdout at the start of your script:
import sys
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8", errors="replace")4. Empty text results
Cause: The .one file may be empty, contain only images or tables (no RichText nodes), or be a notebook file (.onetoc2) rather than a section file (.one).
Fix: Check the page count and inspect node types:
from aspose.note import Document
doc = Document("MyNotes.one")
print(f"Pages: {len(list(doc))}")
for page in doc:
print(f" Children: {sum(1 for _ in page)}")5. IncorrectPasswordException
Cause: The .one file is encrypted. Encrypted documents are not supported.
Fix: Aspose.Note FOSS for Python does not support encrypted .one files. The full-featured commercial Aspose.Note product supports decryption.
Frequently Asked Questions
Can I extract text from all pages at once?
Yes. doc.GetChildNodes(RichText) searches the entire document tree recursively, including all pages, outlines, and outline elements.
Does the library support .onetoc2 notebook files?
No. The library handles .one section files only. Notebook table-of-contents files (.onetoc2) are a different format and are not supported.
Can I extract text from tables?
Yes. TableCell nodes contain RichText children that can be read the same way:
from aspose.note import Document, Table, TableRow, TableCell, RichText
doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
for row in table.GetChildNodes(TableRow):
for cell in row.GetChildNodes(TableCell):
cell_text = " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
print(cell_text, end="\t")
print()What Python versions are supported?
Python 3.10, 3.11, and 3.12.
Is the library thread-safe?
Each Document instance should be used from a single thread. For parallel extraction, create a separate Document per thread.
Related Resources: