def read_sheets(archive): """Read worksheet titles and ids for a workbook""" xml_source = archive.read(ARC_WORKBOOK) tree = fromstring(xml_source) for element in safe_iterator(tree, '{%s}sheet' % SHEET_MAIN_NS): attrib = element.attrib attrib['id'] = attrib["{%s}id" % REL_NS] del attrib["{%s}id" % REL_NS] if attrib['id']: yield attrib
def read_rels(archive): """Read relationships for a workbook""" xml_source = archive.read(ARC_WORKBOOK_RELS) tree = fromstring(xml_source) for element in safe_iterator(tree, '{%s}Relationship' % PKG_REL_NS): rId = element.get('Id') pth = element.get("Target") typ = element.get('Type') # normalise path if pth.startswith("/xl"): pth = pth.replace("/xl", "xl") elif not pth.startswith("xl") and not pth.startswith(".."): pth = "xl/" + pth yield rId, {'path': pth, 'type': typ}
def read_named_ranges(archive): root = fromstring(archive.read(ARC_WORKBOOK)) dict = {} for name_node in safe_iterator(root, '{%s}definedName' % SHEET_MAIN_NS): name = name_node.get('name') # if name in dict: # raise Exception('Named_range %s is defined in multiple sheets' % name) if not name_node.get('hidden'): if name_node.get('name') == 'tR': dict[name_node.get('name')] = 'Depreciation!A1:A1000' elif '!#REF' in name_node.text: dict[name_node.get('name')] = '#REF!' else: dict[name_node.get('name')] = name_node.text.replace('$','').replace(" ","") return dict