<s> from __future__ import absolute_import <EOL> """<STR_LIT>""" <EOL> from openpyxl . compat import safe_string <EOL> from openpyxl . utils . indexed_list import IndexedList <EOL> from openpyxl . xml . functions import ( <EOL> Element , <EOL> SubElement , <EOL> ConditionalElement , <EOL> tostring , <EOL> ) <EOL> from openpyxl . xml . constants import SHEET_MAIN_NS <EOL> from openpyxl . styles . colors import COLOR_INDEX <EOL> from openpyxl . styles import DEFAULTS <EOL> from openpyxl . styles . numbers import BUILTIN_FORMATS_REVERSE <EOL> from openpyxl . styles . fills import GradientFill , PatternFill <EOL> class StyleWriter ( object ) : <EOL> def __init__ ( self , workbook ) : <EOL> self . wb = workbook <EOL> self . _root = Element ( '<STR_LIT>' , { '<STR_LIT>' : SHEET_MAIN_NS } ) <EOL> def write_table ( self ) : <EOL> self . _write_number_formats ( ) <EOL> self . _write_fonts ( ) <EOL> self . _write_fills ( ) <EOL> self . _write_borders ( ) <EOL> self . _write_named_styles ( ) <EOL> self . _write_cell_styles ( ) <EOL> self . _write_style_names ( ) <EOL> self . _write_differential_styles ( ) <EOL> self . _write_table_styles ( ) <EOL> self . _write_colors ( ) <EOL> return tostring ( self . _root ) <EOL> def _write_number_formats ( self ) : <EOL> node = SubElement ( self . _root , '<STR_LIT>' , count = "<STR_LIT>" % len ( self . wb . _number_formats ) ) <EOL> for idx , nf in enumerate ( self . wb . _number_formats , <NUM_LIT> ) : <EOL> SubElement ( node , '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' % idx , <EOL> '<STR_LIT>' : '<STR_LIT:%s>' % nf } <EOL> ) <EOL> def _write_fonts ( self ) : <EOL> fonts_node = SubElement ( self . _root , '<STR_LIT>' , count = "<STR_LIT>" % len ( self . wb . _fonts ) ) <EOL> for font in self . wb . _fonts : <EOL> fonts_node . append ( font . to_tree ( ) ) <EOL> def _write_fills ( self ) : <EOL> fills_node = SubElement ( self . _root , '<STR_LIT>' , count = "<STR_LIT>" % len ( self . wb . _fills ) ) <EOL> for fill in self . wb . _fills : <EOL> fills_node . append ( fill . to_tree ( ) ) <EOL> def _write_borders ( self ) : <EOL> """<STR_LIT>""" <EOL> borders_node = SubElement ( self . _root , '<STR_LIT>' , count = "<STR_LIT>" % len ( self . wb . _borders ) ) <EOL> for border in self . wb . _borders : <EOL> borders_node . append ( border . to_tree ( ) ) <EOL> def _write_named_styles ( self ) : <EOL> styles = self . wb . _named_styles <EOL> cell_style_xfs = SubElement ( self . _root , '<STR_LIT>' , count = "<STR_LIT>" % len ( styles ) ) <EOL> for style in self . wb . _named_styles . values ( ) : <EOL> attrs = { } <EOL> attrs [ '<STR_LIT>' ] = str ( self . wb . _fonts . add ( style . font ) ) <EOL> attrs [ '<STR_LIT>' ] = str ( self . wb . _borders . add ( style . border ) ) <EOL> attrs [ '<STR_LIT>' ] = str ( self . wb . _fills . add ( style . fill ) ) <EOL> fmt = style . number_format <EOL> if fmt in BUILTIN_FORMATS_REVERSE : <EOL> fmt = BUILTIN_FORMATS_REVERSE [ fmt ] <EOL> else : <EOL> fmt = self . wb . _number_formats . add ( style . number_format ) + <NUM_LIT> <EOL> attrs [ '<STR_LIT>' ] = str ( fmt ) <EOL> SubElement ( cell_style_xfs , '<STR_LIT>' , attrs ) <EOL> def _write_cell_styles ( self ) : <EOL> """<STR_LIT>""" <EOL> cell_xfs = SubElement ( self . _root , '<STR_LIT>' , <EOL> count = '<STR_LIT>' % len ( self . wb . _cell_styles ) ) <EOL> for style in self . wb . _cell_styles : <EOL> node = style . to_tree ( ) <EOL> cell_xfs . append ( node ) <EOL> if style . applyAlignment : <EOL> al = self . wb . _alignments [ style . alignmentId ] <EOL> el = al . to_tree ( ) <EOL> node . append ( el ) <EOL> if style . applyProtection : <EOL> prot = self . wb . _protections [ style . protectionId ] <EOL> el = prot . to_tree ( ) <EOL> node . append ( el ) <EOL> def _write_style_names ( self ) : <EOL> styles = self . wb . _named_styles <EOL> cell_styles = SubElement ( self . _root , '<STR_LIT>' , count = str ( len ( styles ) ) ) <EOL> for idx , style in enumerate ( styles . values ( ) ) : <EOL> attrs = dict ( style ) <EOL> attrs [ '<STR_LIT>' ] = str ( idx ) <EOL> SubElement ( cell_styles , '<STR_LIT>' , attrs ) <EOL> def _write_differential_styles ( self ) : <EOL> dxfs = SubElement ( self . _root , "<STR_LIT>" , count = str ( len ( self . wb . _differential_styles ) ) ) <EOL> for fmt in self . wb . _differential_styles : <EOL> dxfs . append ( fmt . to_tree ( ) ) <EOL> return dxfs <EOL> def _write_table_styles ( self ) : <EOL> SubElement ( self . _root , '<STR_LIT>' , <EOL> { '<STR_LIT:count>' : '<STR_LIT:0>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def _write_colors ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . wb . _colors == COLOR_INDEX : <EOL> return <EOL> cols = SubElement ( self . _root , "<STR_LIT>" ) <EOL> rgb = SubElement ( cols , "<STR_LIT>" ) <EOL> for color in self . wb . _colors : <EOL> SubElement ( rgb , "<STR_LIT>" , rgb = color ) </s>
