<s> """<STR_LIT>""" <EOL> from __future__ import ( <EOL> absolute_import , division , print_function , unicode_literals <EOL> ) <EOL> from . import parse_xml <EOL> from . . exceptions import InvalidSpanError <EOL> from . ns import nsdecls , qn <EOL> from . . shared import Emu , Twips <EOL> from . simpletypes import ( <EOL> ST_Merge , ST_TblLayoutType , ST_TblWidth , ST_TwipsMeasure , XsdInt <EOL> ) <EOL> from . xmlchemy import ( <EOL> BaseOxmlElement , OneAndOnlyOne , OneOrMore , OptionalAttribute , <EOL> RequiredAttribute , ZeroOrOne , ZeroOrMore <EOL> ) <EOL> class CT_Row ( BaseOxmlElement ) : <EOL> """<STR_LIT>""" <EOL> tblPrEx = ZeroOrOne ( '<STR_LIT>' ) <EOL> trPr = ZeroOrOne ( '<STR_LIT>' ) <EOL> tc = ZeroOrMore ( '<STR_LIT>' ) <EOL> def tc_at_grid_col ( self , idx ) : <EOL> """<STR_LIT>""" <EOL> grid_col = <NUM_LIT:0> <EOL> for tc in self . tc_lst : <EOL> if grid_col == idx : <EOL> return tc <EOL> grid_col += tc . grid_span <EOL> if grid_col > idx : <EOL> raise ValueError ( '<STR_LIT>' % idx ) <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> @ property <EOL> def tr_idx ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . getparent ( ) . tr_lst . index ( self ) <EOL> def _insert_tblPrEx ( self , tblPrEx ) : <EOL> self . insert ( <NUM_LIT:0> , tblPrEx ) <EOL> def _insert_trPr ( self , trPr ) : <EOL> tblPrEx = self . tblPrEx <EOL> if tblPrEx is not None : <EOL> tblPrEx . addnext ( trPr ) <EOL> else : <EOL> self . insert ( <NUM_LIT:0> , trPr ) <EOL> def _new_tc ( self ) : <EOL> return CT_Tc . new ( ) <EOL> class CT_Tbl ( BaseOxmlElement ) : <EOL> """<STR_LIT>""" <EOL> tblPr = OneAndOnlyOne ( '<STR_LIT>' ) <EOL> tblGrid = OneAndOnlyOne ( '<STR_LIT>' ) <EOL> tr = ZeroOrMore ( '<STR_LIT>' ) <EOL> @ property <EOL> def bidiVisual_val ( self ) : <EOL> """<STR_LIT>""" <EOL> bidiVisual = self . tblPr . bidiVisual <EOL> if bidiVisual is None : <EOL> return None <EOL> return bidiVisual . val <EOL> @ bidiVisual_val . setter <EOL> def bidiVisual_val ( self , value ) : <EOL> tblPr = self . tblPr <EOL> if value is None : <EOL> tblPr . _remove_bidiVisual ( ) <EOL> else : <EOL> tblPr . get_or_add_bidiVisual ( ) . val = value <EOL> @ property <EOL> def col_count ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . tblGrid . gridCol_lst ) <EOL> def iter_tcs ( self ) : <EOL> """<STR_LIT>""" <EOL> for tr in self . tr_lst : <EOL> for tc in tr . tc_lst : <EOL> yield tc <EOL> @ classmethod <EOL> def new_tbl ( cls , rows , cols , width ) : <EOL> """<STR_LIT>""" <EOL> return parse_xml ( cls . _tbl_xml ( rows , cols , width ) ) <EOL> @ property <EOL> def tblStyle_val ( self ) : <EOL> """<STR_LIT>""" <EOL> tblStyle = self . tblPr . tblStyle <EOL> if tblStyle is None : <EOL> return None <EOL> return tblStyle . val <EOL> @ tblStyle_val . setter <EOL> def tblStyle_val ( self , styleId ) : <EOL> """<STR_LIT>""" <EOL> tblPr = self . tblPr <EOL> tblPr . _remove_tblStyle ( ) <EOL> if styleId is None : <EOL> return <EOL> tblPr . _add_tblStyle ( ) . val = styleId <EOL> @ classmethod <EOL> def _tbl_xml ( cls , rows , cols , width ) : <EOL> col_width = Emu ( width / cols ) if cols > <NUM_LIT:0> else Emu ( <NUM_LIT:0> ) <EOL> return ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:%s>' <EOL> '<STR_LIT:%s>' <EOL> '<STR_LIT>' <EOL> ) % ( <EOL> nsdecls ( '<STR_LIT:w>' ) , <EOL> cls . _tblGrid_xml ( cols , col_width ) , <EOL> cls . _trs_xml ( rows , cols , col_width ) <EOL> ) <EOL> @ classmethod <EOL> def _tblGrid_xml ( cls , col_count , col_width ) : <EOL> xml = '<STR_LIT>' <EOL> for i in range ( col_count ) : <EOL> xml += '<STR_LIT>' % col_width . twips <EOL> xml += '<STR_LIT>' <EOL> return xml <EOL> @ classmethod <EOL> def _trs_xml ( cls , row_count , col_count , col_width ) : <EOL> xml = '<STR_LIT>' <EOL> for i in range ( row_count ) : <EOL> xml += ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT:%s>' <EOL> '<STR_LIT>' <EOL> ) % cls . _tcs_xml ( col_count , col_width ) <EOL> return xml <EOL> @ classmethod <EOL> def _tcs_xml ( cls , col_count , col_width ) : <EOL> xml = '<STR_LIT>' <EOL> for i in range ( col_count ) : <EOL> xml += ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) % col_width . twips <EOL> return xml <EOL> class CT_TblGrid ( BaseOxmlElement ) : <EOL> """<STR_LIT>""" <EOL> gridCol = ZeroOrMore ( '<STR_LIT>' , successors = ( '<STR_LIT>' , ) ) <EOL> class CT_TblGridCol ( BaseOxmlElement ) : <EOL> """<STR_LIT>""" <EOL> w = OptionalAttribute ( '<STR_LIT>' , ST_TwipsMeasure ) <EOL> @ property <EOL> def gridCol_idx ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . getparent ( ) . gridCol_lst . index ( self ) <EOL> class CT_TblLayoutType ( BaseOxmlElement ) : <EOL> """<STR_LIT>""" <EOL> type = OptionalAttribute ( '<STR_LIT>' , ST_TblLayoutType ) <EOL> class CT_TblPr ( BaseOxmlElement ) : <EOL> """<STR_LIT>""" <EOL> _tag_seq = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ) <EOL> tblStyle = ZeroOrOne ( '<STR_LIT>' , successors = _tag_seq [ <NUM_LIT:1> : ] ) <EOL> bidiVisual = ZeroOrOne ( '<STR_LIT>' , successors = _tag_seq [ <NUM_LIT:4> : ] ) <EOL> jc = ZeroOrOne ( '<STR_LIT>' , successors = _tag_seq [ <NUM_LIT:8> : ] ) <EOL> tblLayout = ZeroOrOne ( '<STR_LIT>' , successors = _tag_seq [ <NUM_LIT> : ] ) <EOL> del _tag_seq <EOL> @ property <EOL> def alignment ( self ) : <EOL> """<STR_LIT>""" <EOL> jc = self . jc <EOL> if jc is None : <EOL> return None <EOL> return jc . val <EOL> @ alignment . setter <EOL> def alignment ( self , value ) : <EOL> self . _remove_jc ( ) <EOL> if value is None : <EOL> return <EOL> jc = self . get_or_add_jc ( ) <EOL> jc . val = value <EOL> @ property <EOL> def autofit ( self ) : <EOL> """<STR_LIT>""" <EOL> tblLayout = self . tblLayout <EOL> if tblLayout is None : <EOL> return True <EOL> return False if tblLayout . type == '<STR_LIT>' else True <EOL> @ autofit . setter <EOL> def autofit ( self , value ) : <EOL> tblLayout = self . get_or_add_tblLayout ( ) <EOL> tblLayout . type = '<STR_LIT>' if value else '<STR_LIT>' <EOL> @ property <EOL> def style ( self ) : <EOL> """<STR_LIT>""" <EOL> tblStyle = self . tblStyle <EOL> if tblStyle is None : <EOL> return None <EOL> return tblStyle . val <EOL> @ style . setter <EOL> def style ( self , value ) : <EOL> self . _remove_tblStyle ( ) <EOL> if value is None : <EOL> return <EOL> self . _add_tblStyle ( val = value ) <EOL> class CT_TblWidth ( BaseOxmlElement ) : <EOL> """<STR_LIT>""" <EOL> w = RequiredAttribute ( '<STR_LIT>' , XsdInt ) <EOL> type = RequiredAttribute ( '<STR_LIT>' , ST_TblWidth ) <EOL> @ property <EOL> def width ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . type != '<STR_LIT>' : <EOL> return None <EOL> return Twips ( self . w ) <EOL> @ width . setter <EOL> def width ( self , value ) : <EOL> self . type = '<STR_LIT>' <EOL> self . w = Emu ( value ) . twips <EOL> class CT_Tc ( BaseOxmlElement ) : <EOL> """<STR_LIT>""" <EOL> tcPr = ZeroOrOne ( '<STR_LIT>' ) <EOL> p = OneOrMore ( '<STR_LIT>' ) <EOL> tbl = OneOrMore ( '<STR_LIT>' ) <EOL> @ property <EOL> def bottom ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . vMerge is not None : <EOL> tc_below = self . _tc_below <EOL> if tc_below is not None and tc_below . vMerge == ST_Merge . CONTINUE : <EOL> return tc_below . bottom <EOL> return self . _tr_idx + <NUM_LIT:1> <EOL> def clear_content ( self ) : <EOL> """<STR_LIT>""" <EOL> new_children = [ ] <EOL> tcPr = self . tcPr <EOL> if tcPr is not None : <EOL> new_children . append ( tcPr ) <EOL> self [ : ] = new_children <EOL> @ property <EOL> def grid_span ( self ) : <EOL> """<STR_LIT>""" <EOL> tcPr = self . tcPr <EOL> if tcPr is None : <EOL> return <NUM_LIT:1> <EOL> return tcPr . grid_span <EOL> @ grid_span . setter <EOL> def grid_span ( self , value ) : <EOL> tcPr = self . get_or_add_tcPr ( ) <EOL> tcPr . grid_span = value <EOL> def iter_block_items ( self ) : <EOL> """<STR_LIT>""" <EOL> block_item_tags = ( qn ( '<STR_LIT>' ) , qn ( '<STR_LIT>' ) , qn ( '<STR_LIT>' ) ) <EOL> for child in self : <EOL> if child . tag in block_item_tags : <EOL> yield child <EOL> @ property <EOL> def left ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _grid_col <EOL> def merge ( self , other_tc ) : <EOL> """<STR_LIT>""" <EOL> top , left , height , width = self . _span_dimensions ( other_tc ) <EOL> top_tc = self . _tbl . tr_lst [ top ] . tc_at_grid_col ( left ) <EOL> top_tc . _grow_to ( width , height ) <EOL> return top_tc <EOL> @ classmethod <EOL> def new ( cls ) : <EOL> """<STR_LIT>""" <EOL> return parse_xml ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % nsdecls ( '<STR_LIT:w>' ) <EOL> ) <EOL> @ property <EOL> def right ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _grid_col + self . grid_span <EOL> @ property <EOL> def top ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . vMerge is None or self . vMerge == ST_Merge . RESTART : <EOL> return self . _tr_idx <EOL> return self . _tc_above . top <EOL> @ property <EOL> def vMerge ( self ) : <EOL> """<STR_LIT>""" <EOL> tcPr = self . tcPr <EOL> if tcPr is None : <EOL> return None <EOL> return tcPr . vMerge_val <EOL> @ vMerge . setter <EOL> def vMerge ( self , value ) : <EOL> tcPr = self . get_or_add_tcPr ( ) <EOL> tcPr . vMerge_val = value <EOL> @ property <EOL> def width ( self ) : <EOL> """<STR_LIT>""" <EOL> tcPr = self . tcPr <EOL> if tcPr is None : <EOL> return None <EOL> return tcPr . width <EOL> @ width . setter <EOL> def width ( self , value ) : <EOL> tcPr = self . get_or_add_tcPr ( ) <EOL> tcPr . width = value <EOL> def _add_width_of ( self , other_tc ) : <EOL> """<STR_LIT>""" <EOL> if self . width and other_tc . width : <EOL> self . width += other_tc . width <EOL> @ property <EOL> def _grid_col ( self ) : <EOL> """<STR_LIT>""" <EOL> tr = self . _tr <EOL> idx = tr . tc_lst . index ( self ) <EOL> preceding_tcs = tr . tc_lst [ : idx ] <EOL> return sum ( tc . grid_span for tc in preceding_tcs ) <EOL> def _grow_to ( self , width , height , top_tc = None ) : <EOL> """<STR_LIT>""" <EOL> def vMerge_val ( top_tc ) : <EOL> if top_tc is not self : <EOL> return ST_Merge . CONTINUE <EOL> if height == <NUM_LIT:1> : <EOL> return None <EOL> return ST_Merge . RESTART <EOL> top_tc = self if top_tc is None else top_tc <EOL> self . _span_to_width ( width , top_tc , vMerge_val ( top_tc ) ) <EOL> if height > <NUM_LIT:1> : <EOL> self . _tc_below . _grow_to ( width , height - <NUM_LIT:1> , top_tc ) <EOL> def _insert_tcPr ( self , tcPr ) : <EOL> """<STR_LIT>""" <EOL> self . insert ( <NUM_LIT:0> , tcPr ) <EOL> return tcPr <EOL> @ property <EOL> def _is_empty ( self ) : <EOL> """<STR_LIT>""" <EOL> block_items = list ( self . iter_block_items ( ) ) <EOL> if len ( block_items ) > <NUM_LIT:1> : <EOL> return False <EOL> p = block_items [ <NUM_LIT:0> ] <EOL> if len ( p . r_lst ) == <NUM_LIT:0> : <EOL> return True <EOL> return False <EOL> def _move_content_to ( self , other_tc ) : <EOL> """<STR_LIT>""" <EOL> if other_tc is self : <EOL> return <EOL> if self . _is_empty : <EOL> return <EOL> other_tc . _remove_trailing_empty_p ( ) <EOL> for block_element in self . iter_block_items ( ) : <EOL> other_tc . append ( block_element ) <EOL> self . append ( self . _new_p ( ) ) <EOL> def _new_tbl ( self ) : <EOL> return CT_Tbl . new ( ) <EOL> @ property <EOL> def _next_tc ( self ) : <EOL> """<STR_LIT>""" <EOL> following_tcs = self . xpath ( '<STR_LIT>' ) <EOL> return following_tcs [ <NUM_LIT:0> ] if following_tcs else None <EOL> def _remove ( self ) : <EOL> """<STR_LIT>""" <EOL> self . getparent ( ) . remove ( self ) <EOL> def _remove_trailing_empty_p ( self ) : <EOL> """<STR_LIT>""" <EOL> block_items = list ( self . iter_block_items ( ) ) <EOL> last_content_elm = block_items [ - <NUM_LIT:1> ] <EOL> if last_content_elm . tag != qn ( '<STR_LIT>' ) : <EOL> return <EOL> p = last_content_elm <EOL> if len ( p . r_lst ) > <NUM_LIT:0> : <EOL> return <EOL> self . remove ( p ) <EOL> def _span_dimensions ( self , other_tc ) : <EOL> """<STR_LIT>""" <EOL> def raise_on_inverted_L ( a , b ) : <EOL> if a . top == b . top and a . bottom != b . bottom : <EOL> raise InvalidSpanError ( '<STR_LIT>' ) <EOL> if a . left == b . left and a . right != b . right : <EOL> raise InvalidSpanError ( '<STR_LIT>' ) <EOL> def raise_on_tee_shaped ( a , b ) : <EOL> top_most , other = ( a , b ) if a . top < b . top else ( b , a ) <EOL> if top_most . top < other . top and top_most . bottom > other . bottom : <EOL> raise InvalidSpanError ( '<STR_LIT>' ) <EOL> left_most , other = ( a , b ) if a . left < b . left else ( b , a ) <EOL> if left_most . left < other . left and left_most . right > other . right : <EOL> raise InvalidSpanError ( '<STR_LIT>' ) <EOL> raise_on_inverted_L ( self , other_tc ) <EOL> raise_on_tee_shaped ( self , other_tc ) <EOL> top = min ( self . top , other_tc . top ) <EOL> left = min ( self . left , other_tc . left ) <EOL> bottom = max ( self . bottom , other_tc . bottom ) <EOL> right = max ( self . right , other_tc . right ) <EOL> return top , left , bottom - top , right - left <EOL> def _span_to_width ( self , grid_width , top_tc , vMerge ) : <EOL> """<STR_LIT>""" <EOL> self . _move_content_to ( top_tc ) <EOL> while self . grid_span < grid_width : <EOL> self . _swallow_next_tc ( grid_width , top_tc ) <EOL> self . vMerge = vMerge <EOL> def _swallow_next_tc ( self , grid_width , top_tc ) : <EOL> """<STR_LIT>""" <EOL> def raise_on_invalid_swallow ( next_tc ) : <EOL> if next_tc is None : <EOL> raise InvalidSpanError ( '<STR_LIT>' ) <EOL> if self . grid_span + next_tc . grid_span > grid_width : <EOL> raise InvalidSpanError ( '<STR_LIT>' ) <EOL> next_tc = self . _next_tc <EOL> raise_on_invalid_swallow ( next_tc ) <EOL> next_tc . _move_content_to ( top_tc ) <EOL> self . _add_width_of ( next_tc ) <EOL> self . grid_span += next_tc . grid_span <EOL> next_tc . _remove ( ) <EOL> @ property <EOL> def _tbl ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . xpath ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> @ property <EOL> def _tc_above ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _tr_above . tc_at_grid_col ( self . _grid_col ) <EOL> @ property <EOL> def _tc_below ( self ) : <EOL> """<STR_LIT>""" <EOL> tr_below = self . _tr_below <EOL> if tr_below is None : <EOL> return None <EOL> return tr_below . tc_at_grid_col ( self . _grid_col ) <EOL> @ property <EOL> def _tr ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . xpath ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> @ property <EOL> def _tr_above ( self ) : <EOL> """<STR_LIT>""" <EOL> tr_lst = self . _tbl . tr_lst <EOL> tr_idx = tr_lst . index ( self . _tr ) <EOL> if tr_idx == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return tr_lst [ tr_idx - <NUM_LIT:1> ] <EOL> @ property <EOL> def _tr_below ( self ) : <EOL> """<STR_LIT>""" <EOL> tr_lst = self . _tbl . tr_lst <EOL> tr_idx = tr_lst . index ( self . _tr ) <EOL> try : <EOL> return tr_lst [ tr_idx + <NUM_LIT:1> ] <EOL> except IndexError : <EOL> return None <EOL> @ property <EOL> def _tr_idx ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _tbl . tr_lst . index ( self . _tr ) <EOL> class CT_TcPr ( BaseOxmlElement ) : <EOL> """<STR_LIT>""" <EOL> _tag_seq = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ) <EOL> tcW = ZeroOrOne ( '<STR_LIT>' , successors = _tag_seq [ <NUM_LIT:2> : ] ) <EOL> gridSpan = ZeroOrOne ( '<STR_LIT>' , successors = _tag_seq [ <NUM_LIT:3> : ] ) <EOL> vMerge = ZeroOrOne ( '<STR_LIT>' , successors = _tag_seq [ <NUM_LIT:5> : ] ) <EOL> del _tag_seq <EOL> @ property <EOL> def grid_span ( self ) : <EOL> """<STR_LIT>""" <EOL> gridSpan = self . gridSpan <EOL> if gridSpan is None : <EOL> return <NUM_LIT:1> <EOL> return gridSpan . val <EOL> @ grid_span . setter <EOL> def grid_span ( self , value ) : <EOL> self . _remove_gridSpan ( ) <EOL> if value > <NUM_LIT:1> : <EOL> self . get_or_add_gridSpan ( ) . val = value <EOL> @ property <EOL> def vMerge_val ( self ) : <EOL> """<STR_LIT>""" <EOL> vMerge = self . vMerge <EOL> if vMerge is None : <EOL> return None <EOL> return vMerge . val <EOL> @ vMerge_val . setter <EOL> def vMerge_val ( self , value ) : <EOL> self . _remove_vMerge ( ) <EOL> if value is not None : <EOL> self . _add_vMerge ( ) . val = value <EOL> @ property <EOL> def width ( self ) : <EOL> """<STR_LIT>""" <EOL> tcW = self . tcW <EOL> if tcW is None : <EOL> return None <EOL> return tcW . width <EOL> @ width . setter <EOL> def width ( self , value ) : <EOL> tcW = self . get_or_add_tcW ( ) <EOL> tcW . width = value <EOL> class CT_VMerge ( BaseOxmlElement ) : <EOL> """<STR_LIT>""" <EOL> val = OptionalAttribute ( '<STR_LIT>' , ST_Merge , default = ST_Merge . CONTINUE ) </s>
