예제 #1
0
    def parse_cell_xfs(self):
        """Read styles from the shared style table"""
        cell_xfs = self.root.find('{%s}cellXfs' % SHEET_MAIN_NS)

        if cell_xfs is None:  # can happen on bad OOXML writers (e.g. Gnumeric)
            return

        builtin_formats = NumberFormat._BUILTIN_FORMATS
        cell_xfs_nodes = safe_iterator(cell_xfs, '{%s}xf' % SHEET_MAIN_NS)
        for index, cell_xfs_node in enumerate(cell_xfs_nodes):
            new_style = Style(static=True)
            number_format_id = int(cell_xfs_node.get('numFmtId'))
            if number_format_id < 164:
                new_style.number_format.format_code = \
                        builtin_formats.get(number_format_id, 'General')
            else:
                fmt_code = self.custom_num_formats.get(number_format_id)
                if fmt_code is not None:
                    new_style.number_format.format_code = fmt_code
                else:
                    raise MissingNumberFormat('%s' % number_format_id)

            if bool(cell_xfs_node.get('applyAlignment')):
                alignment = cell_xfs_node.find('{%s}alignment' % SHEET_MAIN_NS)
                if alignment is not None:
                    for key in ('horizontal', 'vertical', 'indent'):
                        _value = alignment.get(key)
                        if _value is not None:
                            setattr(new_style.alignment, key, _value)
                    new_style.alignment.wrap_text = bool(alignment.get('wrapText'))
                    new_style.alignment.shrink_to_fit = bool(alignment.get('shrinkToFit'))
                    text_rotation = alignment.get('textRotation')
                    if text_rotation is not None:
                        new_style.alignment.text_rotation = int(text_rotation)
                    # ignore justifyLastLine option when horizontal = distributed

            if bool(cell_xfs_node.get('applyFont')):
                new_style.font = deepcopy(self.font_list[int(cell_xfs_node.get('fontId'))])

            if bool(cell_xfs_node.get('applyFill')):
                new_style.fill = deepcopy(self.fill_list[int(cell_xfs_node.get('fillId'))])

            if bool(cell_xfs_node.get('applyBorder')):
                new_style.borders = deepcopy(self.border_list[int(cell_xfs_node.get('borderId'))])

            if bool(cell_xfs_node.get('applyProtection')):
                protection = cell_xfs_node.find('{%s}protection' % SHEET_MAIN_NS)
                # Ignore if there are no protection sub-nodes
                if protection is not None:
                    new_style.protection.locked = bool(protection.get('locked'))
                    new_style.protection.hidden = bool(protection.get('hidden'))

            self.style_prop['table'][index] = new_style
예제 #2
0
    def parse_cell_xfs(self):
        """Read styles from the shared style table"""
        cell_xfs = self.root.find('{%s}cellXfs' % SHEET_MAIN_NS)

        if cell_xfs is None:  # can happen on bad OOXML writers (e.g. Gnumeric)
            return

        builtin_formats = NumberFormat._BUILTIN_FORMATS
        cell_xfs_nodes = safe_iterator(cell_xfs, '{%s}xf' % SHEET_MAIN_NS)
        for index, cell_xfs_node in enumerate(cell_xfs_nodes):
            new_style = Style(static=True)
            number_format_id = int(cell_xfs_node.get('numFmtId'))
            if number_format_id < 164:
                new_style.number_format.format_code = \
                        builtin_formats.get(number_format_id, 'General')
            else:
                fmt_code = self.custom_num_formats.get(number_format_id)
                if fmt_code is not None:
                    new_style.number_format.format_code = fmt_code
                else:
                    raise MissingNumberFormat('%s' % number_format_id)

            if bool(cell_xfs_node.get('applyAlignment')):
                alignment = cell_xfs_node.find('{%s}alignment' % SHEET_MAIN_NS)
                if alignment is not None:
                    for key in ('horizontal', 'vertical', 'indent'):
                        _value = alignment.get(key)
                        if _value is not None:
                            setattr(new_style.alignment, key, _value)
                    new_style.alignment.wrap_text = bool(
                        alignment.get('wrapText'))
                    new_style.alignment.shrink_to_fit = bool(
                        alignment.get('shrinkToFit'))
                    text_rotation = alignment.get('textRotation')
                    if text_rotation is not None:
                        new_style.alignment.text_rotation = int(text_rotation)
                    # ignore justifyLastLine option when horizontal = distributed

            if bool(cell_xfs_node.get('applyFont')):
                new_style.font = deepcopy(self.font_list[int(
                    cell_xfs_node.get('fontId'))])

            if bool(cell_xfs_node.get('applyFill')):
                new_style.fill = deepcopy(self.fill_list[int(
                    cell_xfs_node.get('fillId'))])

            if bool(cell_xfs_node.get('applyBorder')):
                new_style.borders = deepcopy(self.border_list[int(
                    cell_xfs_node.get('borderId'))])

            if bool(cell_xfs_node.get('applyProtection')):
                protection = cell_xfs_node.find('{%s}protection' %
                                                SHEET_MAIN_NS)
                # Ignore if there are no protection sub-nodes
                if protection is not None:
                    new_style.protection.locked = bool(
                        protection.get('locked'))
                    new_style.protection.hidden = bool(
                        protection.get('hidden'))

            self.style_prop['table'][index] = new_style
예제 #3
0
def read_style_table(xml_source):
    """Read styles from the shared style table"""
    style_prop = {'table': {}}
    root = fromstring(xml_source)
    custom_num_formats = parse_custom_num_formats(root)
    style_prop['color_index'] = parse_color_index(root)
    font_list = parse_fonts(root, style_prop['color_index'])
    fill_list = parse_fills(root, style_prop['color_index'])
    border_list = parse_borders(root, style_prop['color_index'])
    style_prop['dxf_list'] = parse_dxfs(root, style_prop['color_index'])
    builtin_formats = NumberFormat._BUILTIN_FORMATS
    cell_xfs = root.find('{%s}cellXfs' % SHEET_MAIN_NS)
    if cell_xfs is not None:  # can happen on bad OOXML writers (e.g. Gnumeric)
        cell_xfs_nodes = cell_xfs.findall('{%s}xf' % SHEET_MAIN_NS)
        for index, cell_xfs_node in enumerate(cell_xfs_nodes):
            new_style = Style(static=True)
            number_format_id = int(cell_xfs_node.get('numFmtId'))
            if number_format_id < 164:
                new_style.number_format.format_code = \
                        builtin_formats.get(number_format_id, 'General')
            else:

                if number_format_id in custom_num_formats:
                    new_style.number_format.format_code = \
                            custom_num_formats[number_format_id]
                else:
                    raise MissingNumberFormat('%s' % number_format_id)

            if cell_xfs_node.get('applyAlignment') == '1':
                alignment = cell_xfs_node.find('{%s}alignment' % SHEET_MAIN_NS)
                if alignment is not None:
                    if alignment.get('horizontal') is not None:
                        new_style.alignment.horizontal = alignment.get('horizontal')
                    if alignment.get('vertical') is not None:
                        new_style.alignment.vertical = alignment.get('vertical')
                    if alignment.get('wrapText'):
                        new_style.alignment.wrap_text = True
                    if alignment.get('shrinkToFit'):
                        new_style.alignment.shrink_to_fit = True
                    if alignment.get('indent') is not None:
                        new_style.alignment.ident = int(alignment.get('indent'))
                    if alignment.get('textRotation') is not None:
                        new_style.alignment.text_rotation = int(alignment.get('textRotation'))
                    # ignore justifyLastLine option when horizontal = distributed

            if cell_xfs_node.get('applyFont') == '1':
                new_style.font = deepcopy(font_list[int(cell_xfs_node.get('fontId'))])
                new_style.font.color = deepcopy(font_list[int(cell_xfs_node.get('fontId'))].color)

            if cell_xfs_node.get('applyFill') == '1':
                new_style.fill = deepcopy(fill_list[int(cell_xfs_node.get('fillId'))])
                new_style.fill.start_color = deepcopy(fill_list[int(cell_xfs_node.get('fillId'))].start_color)
                new_style.fill.end_color = deepcopy(fill_list[int(cell_xfs_node.get('fillId'))].end_color)

            if cell_xfs_node.get('applyBorder') == '1':
                new_style.borders = deepcopy(border_list[int(cell_xfs_node.get('borderId'))])
                new_style.borders.left = deepcopy(border_list[int(cell_xfs_node.get('borderId'))].left)
                new_style.borders.left.color = deepcopy(border_list[int(cell_xfs_node.get('borderId'))].left.color)
                new_style.borders.right = deepcopy(border_list[int(cell_xfs_node.get('borderId'))].right)
                new_style.borders.right.color = deepcopy(border_list[int(cell_xfs_node.get('borderId'))].right.color)
                new_style.borders.top = deepcopy(border_list[int(cell_xfs_node.get('borderId'))].top)
                new_style.borders.top.color = deepcopy(border_list[int(cell_xfs_node.get('borderId'))].top.color)
                new_style.borders.bottom = deepcopy(border_list[int(cell_xfs_node.get('borderId'))].bottom)
                new_style.borders.bottom.color = deepcopy(border_list[int(cell_xfs_node.get('borderId'))].bottom.color)
                new_style.borders.diagonal = deepcopy(border_list[int(cell_xfs_node.get('borderId'))].diagonal)
                new_style.borders.diagonal.color = deepcopy(border_list[int(cell_xfs_node.get('borderId'))].diagonal.color)

            if cell_xfs_node.get('applyProtection') == '1':
                protection = cell_xfs_node.find('{%s}protection' % SHEET_MAIN_NS)
                # Ignore if there are no protection sub-nodes
                if protection is not None:
                    if protection.get('locked') is not None:
                        if protection.get('locked') == '1':
                            new_style.protection.locked = Protection.PROTECTION_PROTECTED
                        else:
                            new_style.protection.locked = Protection.PROTECTION_UNPROTECTED
                    if protection.get('hidden') is not None:
                        if protection.get('hidden') == '1':
                            new_style.protection.hidden = Protection.PROTECTION_PROTECTED
                        else:
                            new_style.protection.hidden = Protection.PROTECTION_UNPROTECTED

            style_prop['table'][index] = new_style
    return style_prop