def __init__(self): self.shared_strings = IndexedList() self.shared_styles = IndexedList() self.shared_styles.add(Style()) self._local_data = DummyLocalData() self.encoding = "UTF-8" self.excel_base_date = CALENDAR_WINDOWS_1900
def __init__(self, optimized_write=False, encoding='utf-8', worksheet_class=Worksheet, guess_types=False, data_only=False, read_only=False, write_only=False): self.worksheets = [] self._active_sheet_index = 0 self._named_ranges = [] self._external_links = [] self.properties = DocumentProperties() self.style = Style() self.security = DocumentSecurity() self.__write_only = write_only or optimized_write self.__read_only = read_only self.__thread_local_data = threading.local() self.shared_strings = IndexedList() self.shared_styles = IndexedList() self.shared_styles.add(Style()) self.loaded_theme = None self._worksheet_class = worksheet_class self.vba_archive = None self.style_properties = None self._guess_types = guess_types self.data_only = data_only self.relationships = [] self.drawings = [] self.code_name = u'ThisWorkbook' self.encoding = encoding if not self.write_only: self.worksheets.append(self._worksheet_class(parent_workbook=self))
def test_read_standalone_worksheet(datadir): class DummyWb(object): encoding = 'utf-8' excel_base_date = CALENDAR_WINDOWS_1900 _guess_types = True data_only = False def get_sheet_by_name(self, value): return None def get_sheet_names(self): return [] datadir.join("reader").chdir() ws = None shared_strings = IndexedList(['hello']) with open('sheet2.xml') as src: ws = read_worksheet(src.read(), DummyWb(), 'Sheet 2', shared_strings, {1: Style()}) assert isinstance(ws, Worksheet) assert ws.cell('G5').value == 'hello' assert ws.cell('D30').value == 30 assert ws.cell('K9').value == 0.09
def create_string_table(workbook): """Compile the string table for a workbook.""" strings = set() for sheet in workbook.worksheets: for cell in itervalues(sheet._cells): if cell.data_type == cell.TYPE_STRING and cell._value is not None: strings.add(cell.value) return IndexedList(sorted(strings))
def parse_cell_xfs(self): """Read styles from the shared style table""" cell_xfs = self.root.find('{%s}cellXfs' % SHEET_MAIN_NS) _styles = [] if cell_xfs is None: # can happen on bad OOXML writers (e.g. Gnumeric) return builtin_formats = numbers.BUILTIN_FORMATS xfs = safe_iterator(cell_xfs, '{%s}xf' % SHEET_MAIN_NS) for index, xf in enumerate(xfs): _style = {} num_fmt = xf.get('numFmtId') if num_fmt is not None: num_fmt = int(num_fmt) if num_fmt < 164: format_code = builtin_formats.get(num_fmt, 'General') else: fmt_code = self.custom_num_formats.get(num_fmt) if fmt_code is not None: format_code = fmt_code else: raise MissingNumberFormat('%s' % num_fmt) _style['number_format'] = format_code if bool_attrib(xf, 'applyAlignment'): alignment = {} al = xf.find('{%s}alignment' % SHEET_MAIN_NS) if al is not None: alignment = al.attrib _style['alignment'] = Alignment(**alignment) if bool_attrib(xf, 'applyFont'): _style['font'] = self.font_list[int(xf.get('fontId'))].copy() if bool_attrib(xf, 'applyFill'): _style['fill'] = self.fill_list[int(xf.get('fillId'))].copy() if bool_attrib(xf, 'applyBorder'): _style['border'] = self.border_list[int( xf.get('borderId'))].copy() if bool_attrib(xf, 'applyProtection'): protection = {} prot = xf.find('{%s}protection' % SHEET_MAIN_NS) if prot is not None: protection.update(prot.attrib) _style['protection'] = Protection(**protection) _styles.append(Style(**_style)) self.shared_styles = IndexedList(_styles)
def __init__(self, xml_source): self.root = fromstring(xml_source) self.style_prop = {'table': {}, 'list': IndexedList()} self.color_index = COLOR_INDEX
class WB(): style_properties = None shared_styles = IndexedList() worksheets = []
def __init__(self, xml_source): self.root = fromstring(xml_source) self.shared_styles = IndexedList() self.cond_styles = [] self.style_prop = {} self.color_index = COLOR_INDEX
def _write_cell_xfs(self, number_format_node, fonts_node, fills_node, borders_node): """ write styles combinations based on ids found in tables """ # writing the cellXfs cell_xfs = SubElement(self._root, 'cellXfs', {'count': '%d' % len(self.styles)}) # default def _get_default_vals(): return dict(numFmtId='0', fontId='0', fillId='0', xfId='0', borderId='0') _fonts = IndexedList() _fills = IndexedList() _borders = IndexedList() _custom_fmts = IndexedList() for st in self.styles: vals = _get_default_vals() font = st.font if font != DEFAULTS.font: if font not in _fonts: font_id = _fonts.add(font) self._write_font(fonts_node, st.font) else: font_id = _fonts.index(font) vals['fontId'] = "%d" % (font_id + 1 ) # There is one default font vals['applyFont'] = '1' border = st.border if st.border != DEFAULTS.border: if border not in _borders: border_id = _borders.add(border) self._write_border(borders_node, border) else: border_id = _borders.index(border) vals['borderId'] = "%d" % (border_id + 1 ) # There is one default border vals['applyBorder'] = '1' fill = st.fill if fill != DEFAULTS.fill: if fill not in _fills: fill_id = _fills.add(st.fill) fill_node = SubElement(fills_node, 'fill') if isinstance(fill, PatternFill): self._write_pattern_fill(fill_node, fill) elif isinstance(fill, GradientFill): self._write_gradient_fill(fill_node, fill) else: fill_id = _fills.index(fill) vals['fillId'] = "%d" % (fill_id + 2 ) # There are two default fills vals['applyFill'] = '1' nf = st.number_format if nf != DEFAULTS.number_format: fmt_id = numbers.builtin_format_id(nf) if fmt_id is None: if nf not in _custom_fmts: fmt_id = _custom_fmts.add(nf) + 165 self._write_number_format(number_format_node, fmt_id, nf) else: fmt_id = _custom_fmts.index(nf) vals['numFmtId'] = '%d' % fmt_id vals['applyNumberFormat'] = '1' if st.alignment != DEFAULTS.alignment: vals['applyAlignment'] = '1' if st.protection != DEFAULTS.protection: vals['applyProtection'] = '1' node = SubElement(cell_xfs, 'xf', vals) if st.alignment != DEFAULTS.alignment: self._write_alignment(node, st.alignment) if st.protection != DEFAULTS.protection: self._write_protection(node, st.protection) fonts_node.attrib["count"] = "%d" % (len(_fonts) + 1) borders_node.attrib["count"] = "%d" % (len(_borders) + 1) fills_node.attrib["count"] = "%d" % (len(_fills) + 2) number_format_node.attrib['count'] = '%d' % len(_custom_fmts)
def __init__(self): self.shared_styles = IndexedList()
class DummyWorkbook(object): shared_styles = IndexedList()
def __init__(self): self.shared_styles = IndexedList(range(28)) self.shared_styles.add(Style())
class DummyWorkbook(object): shared_styles = IndexedList() shared_styles.add(None) # Workbooks always have a default style
def __init__(self, sheet): self.sheet = sheet self.authors = IndexedList() self.comments = [] self.extract_comments()
def read_string_table(xml_source): """Read in all shared strings in the table""" root = fromstring(text=xml_source) nodes = safe_iterator(root, '{%s}si' % SHEET_MAIN_NS) strings = (get_string(node) for node in nodes) return IndexedList(strings)