def parse_named_styles(self): """ Extract named styles """ ns = [] styles_node = self.root.find("{%s}cellStyleXfs" % SHEET_MAIN_NS) self._parse_xfs(styles_node) _ids = self.cell_styles for _name, idx in self._parse_style_names(): _id = _ids[idx] style = NamedStyle(name=_name) style.border = self.border_list[_id.border] style.fill = self.fill_list[_id.fill] style.font = self.font_list[_id.font] if _id.alignment: style.alignment = self.alignments[_id.alignment] if _id.protection: style.protection = self.protections[_id.protection] ns.append(style) self.named_styles = IndexedList(ns)
def start(self, tag, attrib): if tag == 'sheet': if not self._current_ws: self._current_ws = self.wb.active if 'title' in attrib: self._current_ws.title = attrib['title'] else: index = int(attrib.get('index')) if 'index' in attrib else None self._current_ws = self.wb.create_sheet(title=attrib.get( 'title', None), index=index) self._row = 0 elif tag == 'columns': start = column_index_from_string(attrib['start']) end = column_index_from_string(attrib.get('end', attrib['start'])) for i in range(start, end + 1): self._current_ws.column_dimensions[get_column_letter( i)].width = int(attrib.get('width')) / 7.0 elif tag == 'row': self._row_buf = [] self._col = 0 elif tag == 'cell': self._cell = WriteOnlyCell(self._current_ws) for attr, value in iteritems(attrib): if attr == 'font': self._cell.font = self._get_font(value) elif attr == 'fill': self._cell.fill = self._get_fill(value) elif attr == 'alignment': self._cell.alignment = self._get_alignment(value) elif attr == 'ref-id': self._refs[value] = CellRef(self, self._row, self._col) elif attr == 'ref-append': self._refs[value] = self._refs.get(value, []) self._refs[value].append( CellRef(self, self._row, self._col)) elif attr == 'fmt': self._cell.number_format = value elif attr == 'rowspan': self._current_ws.merge_cells(start_row=self._row + 1, start_column=self._col + 1, end_row=self._row + int(value), end_column=self._col + 1) elif attr == 'colspan': self._current_ws.merge_cells(start_row=self._row + 1, start_column=self._col + 1, end_row=self._row + 1, end_column=self._col + int(value)) ctype = attrib.get('type', 'unicode') if ctype not in ['unicode', 'number', 'date']: raise ValueError(u'Unknown cell type {ctype}.'.format( ctype=ctype, )) self._cell_type = ctype try: self._cell_date_format = attrib.get('date-fmt') except KeyError: raise ValueError(u"Specify 'date-fmt' attribute for 'date'" u" type") elif tag == 'style': style = NamedStyle(name=attrib['name']) if 'font' in attrib: style.font = self._get_font(attrib['font']) if 'fill' in attrib: style.fill = self._get_fill(attrib['fill']) self.wb.add_named_style(style)