(self)
Remove style attributes and values of the style attribute and put in <style> element as CSS.
Remove style attributes and values of the style attribute and put in <style> element as CSS.
Remove style attributes and values of the style attribute and put in <style > element as CSS .
def extractStyles(self):
        """ Remove style attributes and values of the style attribute and put in <style> element as CSS. """
        
        for element in self.tree.iter():
            style_list = []

            if "style" in element.keys():
                styles = element.attrib["style"].split(';')
                style_list.extend([tuple(style.split(':')) for style in styles])
                del element.attrib["style"]

            for attribute in STYLES & set(element.attrib.keys()):
                style_list.append((attribute, element.attrib[attribute]))
                del element.attrib[attribute]

            if len(style_list) > 0:
                # Ensure styling is in the form: (key, value)
                style_list = [style for style in style_list if len(style)==2]
            
                # Remove pointless styles, e.g. opacity = 1
                for default_style in default_styles & set(style_list):
                    style_list.remove(default_style)
                    
                # Clean decimals:
                for i, (style_name, style_value) in enumerate(style_list):
                    number = re_length.search(style_value)
                    if number:
                        clean_number = self._formatNumber(number.group(1))
                        style_list[i] = (style_name, clean_number + number.group(2))
                
                style_tuple = tuple(style_list)
                if style_tuple not in self.styles:
                    style_class = "style%d" % self.style_counter
                    self.styles[style_tuple] = style_class
                    self.style_counter += 1
                else:
                    style_class = self.styles[style_tuple]
                    
                # Should test to see whether there is already a class
                element.set("class", style_class)
def extractStyles ( self ) : for element in self . tree . iter ( ) : style_list = [ ] if "style" in element . keys ( ) : styles = element . attrib [ "style" ] . split ( ';' ) style_list . extend ( [ tuple ( style . split ( ':' ) ) for style in styles ] ) del element . attrib [ "style" ] for attribute in STYLES & set ( element . attrib . keys ( ) ) : style_list . append ( ( attribute , element . attrib [ attribute ] ) ) del element . attrib [ attribute ] if len ( style_list ) > 0 : # Ensure styling is in the form: (key, value) style_list = [ style for style in style_list if len ( style ) == 2 ] # Remove pointless styles, e.g. opacity = 1 for default_style in default_styles & set ( style_list ) : style_list . remove ( default_style ) # Clean decimals: for i , ( style_name , style_value ) in enumerate ( style_list ) : number = re_length . search ( style_value ) if number : clean_number = self . _formatNumber ( number . group ( 1 ) ) style_list [ i ] = ( style_name , clean_number + number . group ( 2 ) ) style_tuple = tuple ( style_list ) if style_tuple not in self . styles : style_class = "style%d" % self . style_counter self . styles [ style_tuple ] = style_class self . style_counter += 1 else : style_class = self . styles [ style_tuple ] # Should test to see whether there is already a class element . set ( "class" , style_class )