-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonepy.py
More file actions
executable file
·101 lines (68 loc) · 2.74 KB
/
onepy.py
File metadata and controls
executable file
·101 lines (68 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import json
import win32com.client
from xml.etree import ElementTree
onapp = win32com.client.gencache.EnsureDispatch('OneNote.Application')
NS = "{http://schemas.microsoft.com/office/onenote/2010/onenote}"
#Returns the Notebook Hierarchy as JSON
def getHierarchyJson():
return(json.dumps(getHierarchy(), indent=4))
# Returns the Notebook Hierarchy as a Dictionary Array
def getHierarchy():
oneTree = ElementTree.fromstring(onapp.GetHierarchy("",win32com.client.constants.hsPages))
notebooks = []
for notebook in oneTree:
nbk = parseAttributes(notebook)
if (notebook.getchildren()):
s, sg = getSections(notebook)
if (s != []):
nbk['sections'] = s
# Removes RecycleBin from SectionGroups and adds as a first class object
for i in range(len(sg)):
if ('isRecycleBin' in sg[i]):
nbk['recycleBin'] = sg[i]
sg.pop(i)
if (sg != []):
nbk['sectionGroups'] = sg
notebooks.append(nbk)
return notebooks
# Takes in a Notebook or SectionGroup and returns a Dict Array of its Sections & Section Groups
def getSections(notebook):
sections = []
sectionGroups = []
for section in notebook:
if (section.tag == NS + "SectionGroup"):
newSectionGroup = parseAttributes(section)
if (section.getchildren()):
s, sg = getSections(section)
if (sg != []):
newSectionGroup['sectionGroups'] = sg
if (s != []):
newSectionGroup['sections'] = s
sectionGroups.append(newSectionGroup)
if (section.tag == NS + "Section"):
newSection = parseAttributes(section)
if (section.getchildren()):
newSection['pages'] = getPages(section)
sections.append(newSection)
return sections, sectionGroups
# Takes in a Section and returns a Dict Array of its Pages
def getPages(section):
pages =[]
for page in section:
newPage = parseAttributes(page)
if (page.getchildren()):
newPage['meta'] = getMeta(page)
pages.append(newPage)
return pages
# Takes in a Page and returns a Dict Array of its Meta properties
def getMeta (page):
metas = []
for meta in page:
metas.append(parseAttributes(meta))
return metas
# Takes in an object and returns a dictionary of its values
def parseAttributes(obj):
tempDict = {}
for key,value in obj.items():
tempDict[key] = value
return tempDict