Notebooks¶
A notebook is the root of the OneNote tree and the only resource created
without a parent. Its name must be unique for the signed-in user. Requires
Notes.ReadWrite (Notes.Create is enough to create), Notes.Read to only
list.
Create one¶
The name must be unique; pass a timestamped --name to keep reruns from
colliding. Graph has no delete-notebook operation, so create these
deliberately.
notebook = client.me.onenote.notebooks.add("Project X").execute_query()
print(notebook.links.oneNoteWebUrl.href)
List your recent ones¶
getRecentNotebooks returns the notebooks you opened most recently — the fastest
way to build a "jump back in" list without walking the tree.
recent = client.me.onenote.notebooks.get_recent_notebooks(True).execute_query()
for item in recent.value:
print(item.displayName, item.lastAccessedTime)
Provision one from a template¶
Lay down the same sections and starter pages every time. The script is idempotent — an existing section is reused by name, and its starter page is only added when the section is still empty — so running it again changes nothing.
OneNote rejects a duplicate name (20117), so ensure can create first and
fall back to a lookup — a rerun is a no-op: