Beispiel #1
0
    def _write_dxfs(self):
        if self._style_properties and 'dxf_list' in self._style_properties:
            dxfs = SubElement(
                self._root, 'dxfs',
                {'count': str(len(self._style_properties['dxf_list']))})
            for d in self._style_properties['dxf_list']:
                dxf = SubElement(dxfs, 'dxf')
                if 'font' in d and d['font'] is not None:
                    font_node = SubElement(dxf, 'font')
                    if d['font'].color is not None:
                        self._write_color(font_node, d['font'].color)
                    ConditionalElement(font_node, 'b', d['font'].bold, 'val')
                    ConditionalElement(font_node, 'i', d['font'].italic, 'val')
                    ConditionalElement(font_node, 'u',
                                       d['font'].underline != 'none',
                                       {'val': d['font'].underline})
                    ConditionalElement(font_node, 'strike',
                                       d['font'].strikethrough)

                if 'fill' in d:
                    f = d['fill']
                    fill = SubElement(dxf, 'fill')
                    if f.fill_type:
                        node = SubElement(fill, 'patternFill',
                                          {'patternType': f.fill_type})
                    else:
                        node = SubElement(fill, 'patternFill')
                    if f.start_color != DEFAULTS.fill.start_color:
                        self._write_color(node, f.start_color, 'fgColor')

                    if f.end_color != DEFAULTS.fill.end_color:
                        self._write_color(node, f.end_color, 'bgColor')

                if 'border' in d:
                    borders = d['border']
                    border = SubElement(dxf, 'border')
                    # caution: respect this order
                    for side in ('left', 'right', 'top', 'bottom'):
                        obj = getattr(borders, side)
                        if obj.border_style is None or obj.border_style == 'none':
                            node = SubElement(border, side)
                        else:
                            node = SubElement(border, side,
                                              {'style': obj.border_style})
                            self._write_color(node, obj.color)
        else:
            dxfs = SubElement(self._root, 'dxfs', {'count': '0'})
        return dxfs
Beispiel #2
0
    def _write_font(self, node, font):
        node = SubElement(node, "font")
        # if present vertAlign has to be at the start otherwise Excel has a problem
        ConditionalElement(node, "vertAlign", font.vertAlign, {'val':font.vertAlign})
        SubElement(node, 'sz', {'val':str(font.size)})
        self._write_color(node, font.color)
        SubElement(node, 'name', {'val':font.name})
        SubElement(node, 'family', {'val': '%d' % font.family})

        # boolean attrs
        for attr in ("b", "i", "outline", "shadow", "condense"):
            ConditionalElement(node, attr, getattr(font, attr))

        # Don't write the 'scheme' element because it appears to prevent
        # the font name from being applied in Excel.
        #SubElement(font_node, 'scheme', {'val':'minor'})

        ConditionalElement(node, "u", font.underline=='single')
        ConditionalElement(node, "charset", font.charset, {'val':str(font.charset)})
Beispiel #3
0
    def _write_fonts(self):
        """ add fonts part to root
            return {font.crc => index}
        """

        fonts = SubElement(self._root, 'fonts')

        # default
        font_node = SubElement(fonts, 'font')
        SubElement(font_node, 'sz', {'val':'11'})
        SubElement(font_node, 'color', {'theme':'1'})
        SubElement(font_node, 'name', {'val':'Calibri'})
        SubElement(font_node, 'family', {'val':'2'})
        SubElement(font_node, 'scheme', {'val':'minor'})

        # others
        table = {}
        index = 1
        for st in self._style_list:
            if st.font != DEFAULTS.font and st.font not in table:
                table[st.font] = index
                font_node = SubElement(fonts, 'font')
                SubElement(font_node, 'sz', {'val':str(st.font.size)})
                self._unpack_color(font_node, st.font.color.index)
                SubElement(font_node, 'name', {'val':st.font.name})
                SubElement(font_node, 'family', {'val':'2'})
                # Don't write the 'scheme' element because it appears to prevent
                # the font name from being applied in Excel.
                #SubElement(font_node, 'scheme', {'val':'minor'})
                ConditionalElement(font_node, "b", st.font.bold)
                ConditionalElement(font_node, "i", st.font.italic)
                ConditionalElement(font_node, "u",  st.font.underline == 'single')

                index += 1

        fonts.attrib["count"] = str(index)
        return table
def test_no_tag(root, condition):
    ConditionalElement(root, "start", condition)
    assert root.find("start") is None
def test_dict_attrib(root):
    ConditionalElement(root, "start", True, {'val': 'single'})
    tag = root.find("start")
    assert tag.attrib == {'val': 'single'}
def test_simple_attrib(root):
    ConditionalElement(root, "start", True, 'val')
    tag = root.find("start")
    assert tag.attrib == {'val': '1'}
def test_simple(root, condition):
    ConditionalElement(root, "start", condition)
    assert root.find("start").tag == "start"