Mercurial > hg > ooxml
view excel.py @ 71:54bb53434887
begin work on decoder that allows identifiers as keys and values
author | Henry S. Thompson <ht@markup.co.uk> |
---|---|
date | Wed, 28 Jun 2017 19:08:30 +0100 |
parents | 0003fe7b6b67 |
children | 4bd5de7ac247 |
line wrap: on
line source
#!/usr/bin/python3 '''Class model for analysis of Excel spreadsheets''' from jsonweb.encode import to_object, dumper from jsonweb.decode import from_object, loader import json import eDecoder try: string_types=basestring except NameError: string_types=str @from_object() @to_object(exclude_nulls=True) class Book(object): def __init__(self,source,sheets=[],formats=[]): assert(isinstance(source,string_types)) self.source=source sheets=list(sheets) assert(all(isinstance(s,Sheet) for s in sheets)) self.sheets=sheets formats=list(formats) self.formats=formats def addSheet(self,sheet): assert(isinstance(sheet,Sheet)) self.sheets.append(sheet) @from_object() @to_object(exclude_nulls=True,suppress=["book"]) class Sheet(object): def __init__(self,name,book=None,tables=[],docs=[],misc=[]): assert(isinstance(name,string_types)) self.name=name tables=list(tables) assert(all(isinstance(s,Table) for s in tables)) self.tables=tables docs=list(docs) assert(all(isinstance(s,Region) for s in docs)) self.docs=docs misc=list(misc) assert(all(isinstance(s,Region) for s in misc)) self.misc=misc if book is not None: assert(isinstance(book,Book)) book.addSheet(self) self.book=book def addTable(self,table): assert(isinstance(table,Region)) self.tables.append(table) @from_object() @to_object(exclude_nulls=True,suppress=["parent"]) class Region(object): def __init__(self,name,parent=None,ranges=[],content=[]): if parent is not None: assert(isinstance(parent,(Region,Sheet))) self.parent=parent ranges=list(ranges) assert(all(isinstance(s,Range) for s in ranges)) self.ranges=ranges content=list(content) assert(all(isinstance(s,(Region,string_types)) for s in content)) self.content=content assert(isinstance(name,string_types)) self.name=name if parent is not None: parent.addRegion(self) def addRegion(self,content): assert(isinstance(content,(Region,string_types))) self.content.append(content) @from_object() @to_object(exclude_nulls=True,suppress=["sheet"]) class Table(Region): def __init__(self,name,sheet=None,ranges=[],labels=[],data=[]): super(Table,self).__init__(name,sheet,ranges) labels=list(labels) assert(all(isinstance(s,Label) for s in labels)) self.labels=labels data=list(data) assert(all(isinstance(s,Region) for s in data)) self.data=data if sheet is not None: assert(isinstance(sheet,Sheet)) sheet.addTable(self) self.sheet=sheet def addLabel(self,label): assert(isinstance(label,Label)) self.labels.append(label) def addData(self,data): assert(isinstance(data,Data)) self.data.append(data) def addRegion(self,region): assert(isinstance(region,(Label,Data))) if isinstance(region,Label): self.addLabel(region) else: self.addData(region) def lt(filename): with open(filename,'r') as js: return json.loads(js.read(),cls=eDecoder.eDecoder)