<s> """<STR_LIT>""" <EOL> from io import StringIO <EOL> from . . cell import coordinate_from_string , column_index_from_string <EOL> from . . shared . xmltools import Element , SubElement , XMLGenerator , get_document_content , start_tag , end_tag , tag <EOL> def row_sort ( cell ) : <EOL> """<STR_LIT>""" <EOL> return column_index_from_string ( cell . column ) <EOL> def write_worksheet ( worksheet , string_table , style_table ) : <EOL> """<STR_LIT>""" <EOL> xml_file = StringIO ( ) <EOL> doc = XMLGenerator ( xml_file , '<STR_LIT:utf-8>' ) <EOL> start_tag ( doc , '<STR_LIT>' , <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> start_tag ( doc , '<STR_LIT>' ) <EOL> tag ( doc , '<STR_LIT>' , <EOL> { '<STR_LIT>' : '<STR_LIT>' % ( worksheet . show_summary_below ) , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( worksheet . show_summary_right ) } ) <EOL> end_tag ( doc , '<STR_LIT>' ) <EOL> tag ( doc , '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT:%s>' % worksheet . calculate_dimension ( ) } ) <EOL> write_worksheet_sheetviews ( doc , worksheet ) <EOL> tag ( doc , '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> write_worksheet_cols ( doc , worksheet ) <EOL> write_worksheet_data ( doc , worksheet , string_table , style_table ) <EOL> if worksheet . auto_filter : <EOL> tag ( doc , '<STR_LIT>' , { '<STR_LIT>' : worksheet . auto_filter } ) <EOL> write_worksheet_hyperlinks ( doc , worksheet ) <EOL> if worksheet . _charts : <EOL> tag ( doc , '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> end_tag ( doc , '<STR_LIT>' ) <EOL> doc . endDocument ( ) <EOL> xml_string = xml_file . getvalue ( ) <EOL> xml_file . close ( ) <EOL> return xml_string <EOL> def write_worksheet_sheetviews ( doc , worksheet ) : <EOL> start_tag ( doc , '<STR_LIT>' ) <EOL> start_tag ( doc , '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT:0>' } ) <EOL> selectionAttrs = { } <EOL> topLeftCell = worksheet . freeze_panes <EOL> if topLeftCell : <EOL> colName , row = coordinate_from_string ( topLeftCell ) <EOL> column = column_index_from_string ( colName ) <EOL> pane = '<STR_LIT>' <EOL> paneAttrs = { } <EOL> if column > <NUM_LIT:1> : <EOL> paneAttrs [ '<STR_LIT>' ] = str ( column - <NUM_LIT:1> ) <EOL> if row > <NUM_LIT:1> : <EOL> paneAttrs [ '<STR_LIT>' ] = str ( row - <NUM_LIT:1> ) <EOL> pane = '<STR_LIT>' <EOL> if column > <NUM_LIT:1> : <EOL> pane = '<STR_LIT>' <EOL> paneAttrs . update ( dict ( topLeftCell = topLeftCell , <EOL> activePane = pane , <EOL> state = '<STR_LIT>' ) ) <EOL> tag ( doc , '<STR_LIT>' , paneAttrs ) <EOL> selectionAttrs [ '<STR_LIT>' ] = pane <EOL> if row > <NUM_LIT:1> and column > <NUM_LIT:1> : <EOL> tag ( doc , '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> tag ( doc , '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> selectionAttrs . update ( { '<STR_LIT>' : worksheet . active_cell , <EOL> '<STR_LIT>' : worksheet . selected_cell } ) <EOL> tag ( doc , '<STR_LIT>' , selectionAttrs ) <EOL> end_tag ( doc , '<STR_LIT>' ) <EOL> end_tag ( doc , '<STR_LIT>' ) <EOL> def write_worksheet_cols ( doc , worksheet ) : <EOL> """<STR_LIT>""" <EOL> if worksheet . column_dimensions : <EOL> start_tag ( doc , '<STR_LIT>' ) <EOL> for column_string , columndimension in worksheet . column_dimensions . items ( ) : <EOL> col_index = column_index_from_string ( column_string ) <EOL> col_def = { } <EOL> col_def [ '<STR_LIT>' ] = str ( columndimension . style_index ) <EOL> col_def [ '<STR_LIT>' ] = str ( col_index ) <EOL> col_def [ '<STR_LIT>' ] = str ( col_index ) <EOL> if columndimension . width != worksheet . default_column_dimension . width : <EOL> col_def [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> if not columndimension . visible : <EOL> col_def [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> if columndimension . outline_level > <NUM_LIT:0> : <EOL> col_def [ '<STR_LIT>' ] = str ( columndimension . outline_level ) <EOL> if columndimension . collapsed : <EOL> col_def [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> if columndimension . auto_size : <EOL> col_def [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> if columndimension . width > <NUM_LIT:0> : <EOL> col_def [ '<STR_LIT:width>' ] = str ( columndimension . width ) <EOL> else : <EOL> col_def [ '<STR_LIT:width>' ] = '<STR_LIT>' <EOL> tag ( doc , '<STR_LIT>' , col_def ) <EOL> end_tag ( doc , '<STR_LIT>' ) <EOL> def write_worksheet_data ( doc , worksheet , string_table , style_table ) : <EOL> """<STR_LIT>""" <EOL> start_tag ( doc , '<STR_LIT>' ) <EOL> max_column = worksheet . get_highest_column ( ) <EOL> style_id_by_hash = style_table <EOL> cells_by_row = { } <EOL> for cell in worksheet . get_cell_collection ( ) : <EOL> cells_by_row . setdefault ( cell . row , [ ] ) . append ( cell ) <EOL> for row_idx in sorted ( cells_by_row ) : <EOL> row_dimension = worksheet . row_dimensions [ row_idx ] <EOL> attrs = { '<STR_LIT:r>' : '<STR_LIT>' % row_idx , <EOL> '<STR_LIT>' : '<STR_LIT>' % max_column } <EOL> if row_dimension . height > <NUM_LIT:0> : <EOL> attrs [ '<STR_LIT>' ] = str ( row_dimension . height ) <EOL> attrs [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> start_tag ( doc , '<STR_LIT>' , attrs ) <EOL> row_cells = cells_by_row [ row_idx ] <EOL> sorted_cells = sorted ( row_cells , key = row_sort ) <EOL> for cell in sorted_cells : <EOL> value = cell . _value <EOL> coordinate = cell . get_coordinate ( ) <EOL> attributes = { '<STR_LIT:r>' : coordinate } <EOL> attributes [ '<STR_LIT:t>' ] = cell . data_type <EOL> if coordinate in worksheet . _styles : <EOL> attributes [ '<STR_LIT:s>' ] = '<STR_LIT>' % style_id_by_hash [ <EOL> hash ( worksheet . _styles [ coordinate ] ) ] <EOL> start_tag ( doc , '<STR_LIT:c>' , attributes ) <EOL> if value is None : <EOL> tag ( doc , '<STR_LIT:v>' , body = '<STR_LIT>' ) <EOL> elif cell . data_type == cell . TYPE_STRING : <EOL> tag ( doc , '<STR_LIT:v>' , body = '<STR_LIT:%s>' % string_table [ value ] ) <EOL> elif cell . data_type == cell . TYPE_FORMULA : <EOL> tag ( doc , '<STR_LIT:f>' , body = '<STR_LIT:%s>' % value [ <NUM_LIT:1> : ] ) <EOL> tag ( doc , '<STR_LIT:v>' ) <EOL> elif cell . data_type == cell . TYPE_NUMERIC : <EOL> tag ( doc , '<STR_LIT:v>' , body = '<STR_LIT:%s>' % value ) <EOL> else : <EOL> tag ( doc , '<STR_LIT:v>' , body = '<STR_LIT:%s>' % value ) <EOL> end_tag ( doc , '<STR_LIT:c>' ) <EOL> end_tag ( doc , '<STR_LIT>' ) <EOL> end_tag ( doc , '<STR_LIT>' ) <EOL> def write_worksheet_hyperlinks ( doc , worksheet ) : <EOL> """<STR_LIT>""" <EOL> write_hyperlinks = False <EOL> for cell in worksheet . get_cell_collection ( ) : <EOL> if cell . hyperlink_rel_id is not None : <EOL> write_hyperlinks = True <EOL> break <EOL> if write_hyperlinks : <EOL> start_tag ( doc , '<STR_LIT>' ) <EOL> for cell in worksheet . get_cell_collection ( ) : <EOL> if cell . hyperlink_rel_id is not None : <EOL> attrs = { '<STR_LIT>' : cell . hyperlink , <EOL> '<STR_LIT>' : cell . get_coordinate ( ) , <EOL> '<STR_LIT>' : cell . hyperlink_rel_id } <EOL> tag ( doc , '<STR_LIT>' , attrs ) <EOL> end_tag ( doc , '<STR_LIT>' ) <EOL> def write_worksheet_rels ( worksheet , idx ) : <EOL> """<STR_LIT>""" <EOL> root = Element ( '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> for rel in worksheet . relationships : <EOL> attrs = { '<STR_LIT>' : rel . id , '<STR_LIT>' : rel . type , '<STR_LIT>' : rel . target } <EOL> if rel . target_mode : <EOL> attrs [ '<STR_LIT>' ] = rel . target_mode <EOL> SubElement ( root , '<STR_LIT>' , attrs ) <EOL> if worksheet . _charts : <EOL> attrs = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' % idx } <EOL> SubElement ( root , '<STR_LIT>' , attrs ) <EOL> return get_document_content ( root ) </s>
