<s> from __future__ import absolute_import <EOL> """<STR_LIT>""" <EOL> from io import BytesIO <EOL> from warnings import warn <EOL> from openpyxl . xml . functions import iterparse <EOL> from openpyxl . cell import Cell <EOL> from openpyxl . cell . read_only import _cast_number <EOL> from openpyxl . worksheet import Worksheet , ColumnDimension , RowDimension <EOL> from openpyxl . worksheet . page import PageMargins , PrintOptions , PrintPageSetup <EOL> from openpyxl . worksheet . protection import SheetProtection <EOL> from openpyxl . worksheet . views import SheetView <EOL> from openpyxl . worksheet . datavalidation import DataValidation <EOL> from openpyxl . xml . constants import ( <EOL> SHEET_MAIN_NS , <EOL> REL_NS , <EOL> EXT_TYPES <EOL> ) <EOL> from openpyxl . xml . functions import safe_iterator <EOL> from openpyxl . styles import Color <EOL> from openpyxl . formatting import ConditionalFormatting , Rule <EOL> from openpyxl . formula . translate import Translator <EOL> from openpyxl . worksheet . properties import WorksheetProperties <EOL> from openpyxl . utils import ( <EOL> coordinate_from_string , <EOL> get_column_letter , <EOL> column_index_from_string , <EOL> coordinate_to_tuple , <EOL> ) <EOL> from openpyxl . descriptors . excel import ExtensionList , Extension <EOL> def _get_xml_iter ( xml_source ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( xml_source , '<STR_LIT>' ) : <EOL> try : <EOL> xml_source = xml_source . encode ( "<STR_LIT:utf-8>" ) <EOL> except ( AttributeError , UnicodeDecodeError ) : <EOL> pass <EOL> return BytesIO ( xml_source ) <EOL> else : <EOL> try : <EOL> xml_source . seek ( <NUM_LIT:0> ) <EOL> except : <EOL> pass <EOL> return xml_source <EOL> class WorkSheetParser ( object ) : <EOL> COL_TAG = '<STR_LIT>' % SHEET_MAIN_NS <EOL> ROW_TAG = '<STR_LIT>' % SHEET_MAIN_NS <EOL> CELL_TAG = '<STR_LIT>' % SHEET_MAIN_NS <EOL> VALUE_TAG = '<STR_LIT>' % SHEET_MAIN_NS <EOL> FORMULA_TAG = '<STR_LIT>' % SHEET_MAIN_NS <EOL> MERGE_TAG = '<STR_LIT>' % SHEET_MAIN_NS <EOL> INLINE_STRING = "<STR_LIT>" % ( SHEET_MAIN_NS , SHEET_MAIN_NS ) <EOL> INLINE_RICHTEXT = "<STR_LIT>" % ( SHEET_MAIN_NS , SHEET_MAIN_NS , SHEET_MAIN_NS ) <EOL> def __init__ ( self , wb , title , xml_source , shared_strings ) : <EOL> self . ws = wb . create_sheet ( title = title ) <EOL> self . source = xml_source <EOL> self . shared_strings = shared_strings <EOL> self . guess_types = wb . _guess_types <EOL> self . data_only = wb . data_only <EOL> self . styles = self . ws . parent . _cell_styles <EOL> self . differential_styles = wb . _differential_styles <EOL> self . keep_vba = wb . vba_archive is not None <EOL> self . shared_formula_masters = { } <EOL> def parse ( self ) : <EOL> dispatcher = { <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parse_merge , <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parse_column_dimensions , <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parse_row_dimensions , <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parse_print_options , <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parse_margins , <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parse_page_setup , <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parse_header_footer , <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parser_conditional_formatting , <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parse_auto_filter , <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parse_sheet_protection , <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parse_data_validation , <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parse_properties , <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parse_legacy_drawing , <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parse_sheet_views , <EOL> '<STR_LIT>' % SHEET_MAIN_NS : self . parse_extensions , <EOL> } <EOL> tags = dispatcher . keys ( ) <EOL> stream = _get_xml_iter ( self . source ) <EOL> it = iterparse ( stream , tag = tags ) <EOL> for _ , element in it : <EOL> tag_name = element . tag <EOL> if tag_name in dispatcher : <EOL> dispatcher [ tag_name ] ( element ) <EOL> element . clear ( ) <EOL> self . ws . _current_row = self . ws . max_row <EOL> def parse_cell ( self , element ) : <EOL> value = element . find ( self . VALUE_TAG ) <EOL> if value is not None : <EOL> value = value . text <EOL> formula = element . find ( self . FORMULA_TAG ) <EOL> data_type = element . get ( '<STR_LIT:t>' , '<STR_LIT:n>' ) <EOL> coordinate = element . get ( '<STR_LIT:r>' ) <EOL> style_id = element . get ( '<STR_LIT:s>' ) <EOL> if formula is not None and not self . data_only : <EOL> data_type = '<STR_LIT:f>' <EOL> if formula . text : <EOL> value = "<STR_LIT:=>" + formula . text <EOL> else : <EOL> value = "<STR_LIT:=>" <EOL> formula_type = formula . get ( '<STR_LIT:t>' ) <EOL> if formula_type : <EOL> if formula_type != "<STR_LIT>" : <EOL> self . ws . formula_attributes [ coordinate ] = dict ( formula . attrib ) <EOL> else : <EOL> si = formula . get ( '<STR_LIT>' ) <EOL> if si in self . shared_formula_masters : <EOL> trans = self . shared_formula_masters [ si ] <EOL> value = trans . translate_formula ( coordinate ) <EOL> else : <EOL> self . shared_formula_masters [ si ] = Translator ( value , coordinate ) <EOL> style_array = None <EOL> if style_id is not None : <EOL> style_id = int ( style_id ) <EOL> style_array = self . styles [ style_id ] <EOL> row , column = coordinate_to_tuple ( coordinate ) <EOL> cell = Cell ( self . ws , row = row , col_idx = column , style_array = style_array ) <EOL> self . ws . _cells [ ( row , column ) ] = cell <EOL> if value is not None : <EOL> if data_type == '<STR_LIT:n>' : <EOL> value = _cast_number ( value ) <EOL> elif data_type == '<STR_LIT:b>' : <EOL> value = bool ( int ( value ) ) <EOL> elif data_type == '<STR_LIT:s>' : <EOL> value = self . shared_strings [ int ( value ) ] <EOL> elif data_type == '<STR_LIT:str>' : <EOL> data_type = '<STR_LIT:s>' <EOL> else : <EOL> if data_type == '<STR_LIT>' : <EOL> data_type = '<STR_LIT:s>' <EOL> child = element . find ( self . INLINE_STRING ) <EOL> if child is None : <EOL> child = element . find ( self . INLINE_RICHTEXT ) <EOL> if child is not None : <EOL> value = child . text <EOL> if self . guess_types or value is None : <EOL> cell . value = value <EOL> else : <EOL> cell . _value = value <EOL> cell . data_type = data_type <EOL> def parse_merge ( self , element ) : <EOL> for mergeCell in safe_iterator ( element , ( '<STR_LIT>' % SHEET_MAIN_NS ) ) : <EOL> self . ws . merge_cells ( mergeCell . get ( '<STR_LIT>' ) ) <EOL> def parse_column_dimensions ( self , col ) : <EOL> attrs = dict ( col . attrib ) <EOL> column = get_column_letter ( int ( attrs [ '<STR_LIT>' ] ) ) <EOL> attrs [ '<STR_LIT:index>' ] = column <EOL> if '<STR_LIT>' in attrs : <EOL> attrs [ '<STR_LIT>' ] = self . styles [ int ( attrs [ '<STR_LIT>' ] ) ] <EOL> dim = ColumnDimension ( self . ws , ** attrs ) <EOL> self . ws . column_dimensions [ column ] = dim <EOL> def parse_row_dimensions ( self , row ) : <EOL> attrs = dict ( row . attrib ) <EOL> keys = set ( attrs ) <EOL> for key in keys : <EOL> if key == "<STR_LIT:s>" : <EOL> attrs [ '<STR_LIT:s>' ] = self . styles [ int ( attrs [ '<STR_LIT:s>' ] ) ] <EOL> elif key . startswith ( '<STR_LIT:{>' ) : <EOL> del attrs [ key ] <EOL> keys = set ( attrs ) <EOL> if keys != set ( [ '<STR_LIT:r>' , '<STR_LIT>' ] ) and keys != set ( [ '<STR_LIT:r>' ] ) : <EOL> dim = RowDimension ( self . ws , ** attrs ) <EOL> self . ws . row_dimensions [ dim . index ] = dim <EOL> for cell in safe_iterator ( row , self . CELL_TAG ) : <EOL> self . parse_cell ( cell ) <EOL> def parse_print_options ( self , element ) : <EOL> self . ws . print_options = PrintOptions . from_tree ( element ) <EOL> def parse_margins ( self , element ) : <EOL> self . page_margins = PageMargins . from_tree ( element ) <EOL> def parse_page_setup ( self , element ) : <EOL> self . ws . page_setup = PrintPageSetup . from_tree ( element ) <EOL> def parse_header_footer ( self , element ) : <EOL> oddHeader = element . find ( '<STR_LIT>' % SHEET_MAIN_NS ) <EOL> if oddHeader is not None and oddHeader . text is not None : <EOL> self . ws . header_footer . setHeader ( oddHeader . text ) <EOL> oddFooter = element . find ( '<STR_LIT>' % SHEET_MAIN_NS ) <EOL> if oddFooter is not None and oddFooter . text is not None : <EOL> self . ws . header_footer . setFooter ( oddFooter . text ) <EOL> def parser_conditional_formatting ( self , element ) : <EOL> range_string = element . get ( '<STR_LIT>' ) <EOL> cfRules = element . findall ( '<STR_LIT>' % SHEET_MAIN_NS ) <EOL> self . ws . conditional_formatting . cf_rules [ range_string ] = [ ] <EOL> for node in cfRules : <EOL> rule = Rule . from_tree ( node ) <EOL> if rule . dxfId is not None : <EOL> rule . dxf = self . differential_styles [ rule . dxfId ] <EOL> self . ws . conditional_formatting . cf_rules [ range_string ] . append ( rule ) <EOL> def parse_auto_filter ( self , element ) : <EOL> self . ws . auto_filter . ref = element . get ( "<STR_LIT>" ) <EOL> for fc in safe_iterator ( element , '<STR_LIT>' % SHEET_MAIN_NS ) : <EOL> filters = fc . find ( '<STR_LIT>' % SHEET_MAIN_NS ) <EOL> if filters is None : <EOL> continue <EOL> vals = [ f . get ( "<STR_LIT>" ) for f in safe_iterator ( filters , '<STR_LIT>' % SHEET_MAIN_NS ) ] <EOL> blank = filters . get ( "<STR_LIT:blank>" ) <EOL> self . ws . auto_filter . add_filter_column ( fc . get ( "<STR_LIT>" ) , vals , blank = blank ) <EOL> for sc in safe_iterator ( element , '<STR_LIT>' % SHEET_MAIN_NS ) : <EOL> self . ws . auto_filter . add_sort_condition ( sc . get ( "<STR_LIT>" ) , sc . get ( "<STR_LIT>" ) ) <EOL> def parse_sheet_protection ( self , element ) : <EOL> self . ws . protection = SheetProtection . from_tree ( element ) <EOL> password = element . get ( "<STR_LIT:password>" ) <EOL> if password is not None : <EOL> self . ws . protection . set_password ( password , True ) <EOL> def parse_data_validation ( self , element ) : <EOL> for node in safe_iterator ( element , "<STR_LIT>" % SHEET_MAIN_NS ) : <EOL> dv = DataValidation . from_tree ( node ) <EOL> self . ws . _data_validations . append ( dv ) <EOL> def parse_properties ( self , element ) : <EOL> self . ws . sheet_properties = WorksheetProperties . from_tree ( element ) <EOL> def parse_legacy_drawing ( self , element ) : <EOL> if self . keep_vba : <EOL> self . ws . vba_controls = '<STR_LIT>' <EOL> def parse_sheet_views ( self , element ) : <EOL> for el in element . findall ( "<STR_LIT>" % SHEET_MAIN_NS ) : <EOL> pass <EOL> self . ws . sheet_view = SheetView . from_tree ( el ) <EOL> def parse_extensions ( self , element ) : <EOL> extLst = ExtensionList . from_tree ( element ) <EOL> for e in extLst . ext : <EOL> ext_type = EXT_TYPES . get ( e . uri . upper ( ) , "<STR_LIT>" ) <EOL> msg = "<STR_LIT>" . format ( ext_type ) <EOL> warn ( msg ) <EOL> def fast_parse ( xml_source , parent , sheet_title , shared_strings ) : <EOL> parser = WorkSheetParser ( parent , sheet_title , xml_source , shared_strings ) <EOL> parser . parse ( ) <EOL> return parser . ws </s>
