def parent(bag): """for cell, get its top-left cell""" output_bag = xypath.Bag(table=bag.table) for cell in bag.unordered: row, _, col, _ = cell.properties.raw_span(always=True) output_bag.add(cell.table.get_at(col, row)._cell) return output_bag
def children(bag): """for top-left cell, get all cells it spans""" outputbag = xypath.Bag(table=bag.table) for parent in bag: top, bottom, left, right = parent.properties.raw_span(always=True) for row in xrange(top, bottom + 1): for col in xrange(left, right + 1): outputbag = outputbag | bag.table.get_at(col, row) return outputbag
def by_index(bag, items): """filter: return numbered items from a bag. Note that this is 1-indexed! Items can be a list or a single number""" if isinstance(items, int): return bag.by_index([items]) new = xypath.Bag(table=bag.table) for i, cell in enumerate(bag): if i + 1 in items: new.add(cell._cell) if i + 1 == max(items): return new raise xypath.XYPathError( "get_nth needed {} items, but bag only contained {}.\n{!r}".format( max(items), len(bag), bag))
def excel_ref(table, reference): if ':' not in reference: (col, row) = xypath.contrib.excel.excel_address_coordinate(reference, partial=True) return table.get_at(col, row) else: ((left, top), (right, bottom)) = xypath.contrib.excel.excel_range(reference) bag = xypath.Bag(table=table) if top is None and bottom is None: for col in xrange(left, right + 1): bag = bag | table.get_at(col, None) elif left is None and right is None: for row in xrange(top, bottom + 1): bag = bag | table.get_at(None, row) else: for row in xrange(top, bottom + 1): for col in xrange(left, right + 1): bag = bag | table.get_at(col, row) return bag