Mercurial > hg > ooxml
annotate excel.py @ 70:0003fe7b6b67
beginning work on class structure for excel annotation
author | Henry S. Thompson <ht@markup.co.uk> |
---|---|
date | Mon, 26 Jun 2017 18:08:25 +0100 |
parents | |
children | 54bb53434887 |
rev | line source |
---|---|
70
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
1 #!/usr/bin/python3 |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
2 '''Class model for analysis of Excel spreadsheets''' |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
3 from jsonweb.encode import to_object, dumper |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
4 try: |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
5 string_types=basestring |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
6 except NameError: |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
7 string_types=str |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
8 |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
9 @to_object(exclude_nulls=True) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
10 class Book(object): |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
11 def __init__(self,source,sheets=[],formats=[]): |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
12 assert(isinstance(source,string_types)) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
13 self.source=source |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
14 sheets=list(sheets) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
15 assert(all(isinstance(s,Sheet) for s in sheets)) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
16 self.sheets=sheets |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
17 formats=list(formats) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
18 self.formats=formats |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
19 |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
20 def addSheet(self,sheet): |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
21 assert(isinstance(sheet,Sheet)) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
22 self.sheets.append(sheet) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
23 |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
24 @to_object(exclude_nulls=True,suppress=["book"]) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
25 class Sheet(object): |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
26 def __init__(self,name,book,tables=[],docs=[],misc=[]): |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
27 assert(isinstance(name,string_types)) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
28 self.name=name |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
29 assert(isinstance(book,Book)) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
30 self.book=book |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
31 tables=list(tables) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
32 assert(all(isinstance(s,Region) for s in tables)) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
33 self.tables=tables |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
34 docs=list(docs) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
35 assert(all(isinstance(s,Region) for s in docs)) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
36 self.docs=docs |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
37 misc=list(misc) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
38 assert(all(isinstance(s,Region) for s in misc)) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
39 self.misc=misc |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
40 book.addSheet(self) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
41 |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
42 def addTable(self,table): |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
43 assert(isinstance(table,Region)) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
44 self.tables.append(table) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
45 |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
46 @to_object(exclude_nulls=True,suppress=["sheet"]) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
47 class Region(object): |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
48 def __init__(self,name,sheet=None,ranges=[]): |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
49 if sheet is not None: |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
50 assert(isinstance(sheet,Sheet)) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
51 self.sheet=sheet |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
52 ranges=list(ranges) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
53 assert(all(isinstance(s,Range) for s in ranges)) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
54 self.ranges=ranges |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
55 assert(isinstance(name,string_types)) |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
56 self.name=name |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
57 if sheet is not None: |
0003fe7b6b67
beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
58 sheet.addTable(self) |