a
    ]jJ                     @  s  d Z ddlmZ ddlmZmZmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZmZ ddlmZ ddlmZ dd	lmZmZmZmZmZ dd
lmZ ddlmZmZmZm Z m!Z!m"Z"m#Z# ddl$m%Z%m&Z&m'Z' erddlm(Z( ddl)m*Z* ddlm+Z+m,Z, ddl-m.Z. G dd deZ/G dd deZ0G dd deZ1G dd deZ2G dd deZ3G dd deZ4G dd deZ5G dd  d eZ6G d!d" d"eZ7G d#d$ d$eZ8G d%d& d&eZ9G d'd( d(eZ:G d)d* d*eZ;G d+d, d,eZ<d-S ).z"Custom element classes for tables.    )annotations)TYPE_CHECKINGCallablecast)WD_CELL_VERTICAL_ALIGNMENTWD_ROW_HEIGHT_RULEWD_TABLE_DIRECTION)InvalidSpanError)nsdeclsqn)	parse_xml)CT_DecimalNumber)ST_MergeST_TblLayoutTypeST_TblWidthST_TwipsMeasureXsdInt)CT_P)BaseOxmlElementOneAndOnlyOne	OneOrMoreOptionalAttributeRequiredAttribute
ZeroOrMore	ZeroOrOne)EmuLengthTwips)WD_TABLE_ALIGNMENT)WD_ALIGN_PARAGRAPH)CT_OnOff	CT_String)CT_Jcc                   @  s6   e Zd ZU dZedeZded< edeZ	ded< dS )		CT_HeightzBUsed for `w:trHeight` to specify a row height and row height rule.w:valLength | Nonevalzw:hRuleWD_ROW_HEIGHT_RULE | NonehRuleN)
__name__
__module____qualname____doc__r   r   r&   __annotations__r   r(    r.   r.   K/var/www/html/assistant/venv/lib/python3.9/site-packages/docx/oxml/table.pyr#   &   s   
r#   c                   @  s
  e Zd ZU dZded< ded< ded< ded< ed	Zd
ed< edZded< edZ	e
ddddZe
ddddZdddddZe
ddddZe
ddddZejddd dZe
d!d" Zejd#dd$d"Zd%d&d'd(Zd)d*d+d,Zd-d. Zd/S )0CT_Rowz``<w:tr>`` element.zCallable[[], CT_Tc]Zadd_tczCallable[[], CT_TrPr]get_or_add_trPrZ	_add_trPrzlist[CT_Tc]tc_lstz	w:tblPrExzCT_TblPrEx | NonetblPrExzw:trPrzCT_TrPr | NonetrPrzw:tcintreturnc                 C  s   | j }|du rdS |jS zCThe number of unpopulated layout-grid cells at the end of this row.Nr   )r4   
grid_afterselfr4   r.   r.   r/   r9   ?   s    zCT_Row.grid_afterc                 C  s   | j }|du rdS |jS zEThe number of unpopulated layout-grid cells at the start of this row.Nr   )r4   grid_beforer:   r.   r.   r/   r=   G   s    zCT_Row.grid_beforeCT_Tc)grid_offsetr7   c                 C  sN   || j  }| jD ]*}|dk r  q<|dkr0|  S ||j8 }qtd| dS )zThe `tc` element in this tr at exact `grid offset`.

        Raises ValueError when this `w:tr` contains no `w:tc` with exact starting `grid_offset`.
        r   zno `tc` element at grid_offset=N)r=   r2   	grid_span
ValueError)r;   r?   Zremaining_offsettcr.   r.   r/   tc_at_grid_offsetO   s    

zCT_Row.tc_at_grid_offsetc                 C  s   t t|  }|j| S )z?Index of this `w:tr` element within its parent `w:tbl` element.)r   CT_Tbl	getparenttr_lstindex)r;   tblr.   r.   r/   tr_idxd   s    zCT_Row.tr_idxr'   c                 C  s   | j }|du rdS |jS )zFThe value of `./w:trPr/w:trHeight/@w:hRule`, or |None| if not present.N)r4   trHeight_hRuler:   r.   r.   r/   rJ   j   s    zCT_Row.trHeight_hRulevaluec                 C  s   |   }||_d S N)r1   rJ   r;   rL   r4   r.   r.   r/   rJ   r   s    c                 C  s   | j }|du rdS |jS )zHReturn the value of `w:trPr/w:trHeight@w:val`, or |None| if not present.N)r4   trHeight_valr:   r.   r.   r/   rO   w   s    zCT_Row.trHeight_valr%   c                 C  s   |   }||_d S rM   )r1   rO   rN   r.   r.   r/   rO      s    
CT_TblPrEx)r3   c                 C  s   |  d| d S Nr   insert)r;   r3   r.   r.   r/   _insert_tblPrEx   s    zCT_Row._insert_tblPrExCT_TrPr)r4   c                 C  s*   | j }|d ur|| n| d| d S rQ   )r3   ZaddnextrS   )r;   r4   r3   r.   r.   r/   _insert_trPr   s    zCT_Row._insert_trPrc                 C  s   t  S rM   )r>   newr;   r.   r.   r/   _new_tc   s    zCT_Row._new_tcN)r)   r*   r+   r,   r-   r   r3   r4   r   rB   propertyr9   r=   rC   rI   rJ   setterrO   rT   rV   rY   r.   r.   r.   r/   r0   1   s4   

r0   c                   @  s   e Zd ZU dZded< ded< edZded< ed	Zd
ed< edZ	e
ddddZejddddZe
dd Zdd Zedddd dddZe
ddddZejdd d!d"dZedddd#dd$d%Zeddd#d&d'd(Zedddd#d)d*d+Zeddd#d&d,d-Zd.S )/rD   z``<w:tbl>`` element.zCallable[[], CT_Row]Zadd_trzlist[CT_Row]rF   zw:tblPrCT_TblPrtblPrz	w:tblGrid
CT_TblGridtblGridzw:trzbool | Noner6   c                 C  s   | j j}|du rdS |jS )zValue of `./w:tblPr/w:bidiVisual/@w:val` or |None| if not present.

        Controls whether table cells are displayed right-to-left or left-to-right.
        N)r]   
bidiVisualr&   )r;   r`   r.   r.   r/   bidiVisual_val   s    zCT_Tbl.bidiVisual_valzWD_TABLE_DIRECTION | NonerK   c                 C  s*   | j }|d u r|  nt|| _d S rM   )r]   _remove_bidiVisualboolget_or_add_bidiVisualr&   )r;   rL   r]   r.   r.   r/   ra      s    
c                 C  s   t | jjS )z)The number of grid columns in this table.)lenr_   gridCol_lstrX   r.   r.   r/   	col_count   s    zCT_Tbl.col_countc                 c  s"   | j D ]}|jD ]
}|V  qqdS )zGenerate each of the `w:tc` elements in this table, left to right and top to
        bottom.

        Each cell in the first row is generated, followed by each cell in the second
        row, etc.
        N)rF   r2   )r;   trrB   r.   r.   r/   iter_tcs   s    

zCT_Tbl.iter_tcsr5   r   )rowscolswidthr7   c                 C  s   t tt| |||S )zReturn a new `w:tbl` element having `rows` rows and `cols` columns.

        `width` is distributed evenly between the columns.
        )r   rD   r   _tbl_xml)clsrj   rk   rl   r.   r.   r/   new_tbl   s    zCT_Tbl.new_tbl
str | Nonec                 C  s   | j j}|du rdS |jS )zH`w:tblPr/w:tblStyle/@w:val` (a table style id) or |None| if not present.N)r]   tblStyler&   r;   rq   r.   r.   r/   tblStyle_val   s    zCT_Tbl.tblStyle_valNone)styleIdr7   c                 C  s(   | j }|  |du rdS || _dS )zSet the value of `w:tblPr/w:tblStyle/@w:val` (a table style id) to `styleId`.

        If `styleId` is None, remove the `w:tblStyle` element.
        N)r]   _remove_tblStyle_add_tblStyler&   )r;   ru   r]   r.   r.   r/   rs      s
    strc              	   C  sH   |dkrt || nt d}dtd d| || | ||| dS )Nr   z<w:tbl wz>
  <w:tblPr>
    <w:tblW w:type="auto" w:w="0"/>
    <w:tblLook w:firstColumn="1" w:firstRow="1"
               w:lastColumn="0" w:lastRow="0" w:noHBand="0"
               w:noVBand="1" w:val="04A0"/>
  </w:tblPr>
z	</w:tbl>
)r   r
   _tblGrid_xml_trs_xml)rn   rj   rk   rl   	col_widthr.   r.   r/   rm      s    
zCT_Tbl._tbl_xml)rg   r|   r7   c                 C  s,   d}t |D ]}|d|j 7 }q|d7 }|S )Nz  <w:tblGrid>
z    <w:gridCol w:w="%d"/>
z  </w:tblGrid>
)rangetwips)rn   rg   r|   xml_r.   r.   r/   rz      s
    zCT_Tbl._tblGrid_xml)	row_countrg   r|   r7   c                 C  s   d|  || d| S )Nz	  <w:tr>
z
  </w:tr>
)_tcs_xml)rn   r   rg   r|   r.   r.   r/   r{      s    zCT_Tbl._trs_xmlc                 C  s   d|j  d| S )Nz;    <w:tc>
      <w:tcPr>
        <w:tcW w:type="dxa" w:w="z-"/>
      </w:tcPr>
      <w:p/>
    </w:tc>
)r~   )rn   rg   r|   r.   r.   r/   r      s
    zCT_Tbl._tcs_xmlN)r)   r*   r+   r,   r-   r   r]   r_   r   rh   rZ   ra   r[   rg   ri   classmethodro   rs   rm   rz   r{   r   r.   r.   r.   r/   rD      s6   


rD   c                   @  s.   e Zd ZU dZded< ded< edddZd	S )
r^   zq`w:tblGrid` element.

    Child of `w:tbl`, holds `w:gridCol> elements that define column count, width, etc.
    zCallable[[], CT_TblGridCol]Zadd_gridColzlist[CT_TblGridCol]rf   z	w:gridCol)zw:tblGridChange
successorsN)r)   r*   r+   r,   r-   r   ZgridColr.   r.   r.   r/   r^     s   
r^   c                   @  s6   e Zd ZU dZedeZded< eddddZ	d	S )
CT_TblGridColzB`w:gridCol` element, child of `w:tblGrid`, defines a table column.w:wr%   ry   r5   r6   c                 C  s   t t|  }|j| S )zHIndex of this `w:gridCol` element within its parent `w:tblGrid` element.)r   r^   rE   rf   rG   )r;   r_   r.   r.   r/   gridCol_idx  s    zCT_TblGridCol.gridCol_idxN)
r)   r*   r+   r,   r   r   ry   r-   rZ   r   r.   r.   r.   r/   r     s   
r   c                   @  s$   e Zd ZU dZedeZded< dS )CT_TblLayoutTypez`w:tblLayout` element.

    Specifies whether column widths are fixed or can be automatically adjusted based on
    content.
    w:typerp   typeN)r)   r*   r+   r,   r   r   r   r-   r.   r.   r.   r/   r     s   
r   c                   @  s,  e Zd ZU dZded< ded< ded< ded	< d
ed< d
ed< d
ed< dZededd dZded< ededd dZded< ededd dZ	ded< ededd dZ
ded < [ed!d"d#d$Zejd!d%d&d$Zed'd"d(d)Zejd'd%d*d)Zed+d, Zejd-d%d.d,ZdS )/r\   z}``<w:tblPr>`` element, child of ``<w:tbl>``, holds child elements that define
    table properties such as style and borders.zCallable[[], CT_OnOff]rd   zCallable[[], CT_Jc]get_or_add_jczCallable[[], CT_TblLayoutType]get_or_add_tblLayoutzCallable[[], CT_String]rw   Callable[[], None]rb   
_remove_jcrv   )
w:tblStylezw:tblpPrzw:tblOverlapw:bidiVisualzw:tblStyleRowBandSizezw:tblStyleColBandSizezw:tblWw:jcw:tblCellSpacingzw:tblIndzw:tblBordersw:shdw:tblLayoutzw:tblCellMarz	w:tblLookzw:tblCaptionzw:tblDescriptionzw:tblPrChanger      Nr   zCT_String | Nonerq   r      zCT_OnOff | Noner`   r      zCT_Jc | Nonejcr      zCT_TblLayoutType | None	tblLayoutWD_TABLE_ALIGNMENT | Noner6   c                 C  s   | j }|du rdS td|jS )zAHorizontal alignment of table, |None| if `./w:jc` is not present.Nr   )r   r   r&   )r;   r   r.   r.   r/   	alignmentW  s    zCT_TblPr.alignmentrK   c                 C  s,   |    |d u rd S |  }td||_d S )Nr   )r   r   r   r&   )r;   rL   r   r.   r.   r/   r   _  s
    rc   c                 C  s   | j }|du rdS |jdkS )zg|False| when there is a `w:tblLayout` child with `@w:type="fixed"`.

        Otherwise |True|.
        NTfixed)r   r   )r;   r   r.   r.   r/   autofitg  s    zCT_TblPr.autofitc                 C  s   |   }|rdnd|_d S )Nr   r   )r   r   )r;   rL   r   r.   r.   r/   r   p  s    c                 C  s   | j }|du rdS |jS )ziReturn the value of the ``val`` attribute of the ``<w:tblStyle>`` child or
        |None| if not present.N)rq   r&   rr   r.   r.   r/   styleu  s    zCT_TblPr.stylerp   c                 C  s"   |    |d u rd S ||  _d S rM   )rv   rw   r&   r;   rL   r.   r.   r/   r   ~  s    )r)   r*   r+   r,   r-   _tag_seqr   rq   r`   r   r   rZ   r   r[   r   r   r.   r.   r.   r/   r\   )  sD   

r\   c                   @  s   e Zd ZdZdS )rP   z`w:tblPrEx` element, exceptions to table-properties.

    Applied at a lower level, like a `w:tr` to modify the appearance. Possibly used when
    two tables are merged. For more see:
    http://officeopenxml.com/WPtablePropertyExceptions.php
    N)r)   r*   r+   r,   r.   r.   r.   r/   rP     s   rP   c                   @  sT   e Zd ZU dZedeZded< edeZ	e
dddd	Zejd
ddd	ZdS )CT_TblWidthzJUsed for `w:tblW` and `w:tcW` and others, specifies a table-related width.r   r5   ry   r   r%   r6   c                 C  s   | j dkrdS t| jS )z>EMU length indicated by the combined `w:w` and `w:type` attrs.dxaN)r   r   ry   rX   r.   r.   r/   rl     s    
zCT_TblWidth.widthr   rK   c                 C  s   d| _ t|j| _d S )Nr   )r   r   r~   ry   r   r.   r.   r/   rl     s    N)r)   r*   r+   r,   r   r   ry   r-   r   r   rZ   rl   r[   r.   r.   r.   r/   r     s   

r   c                   @  s  e Zd ZU dZded< ded< ded< ded	< d
ed< ded< edZded< edZedZ	e
ddddZdd Ze
ddddZe
ddddZejddddZe
dddd Zd!d" Ze
ddd#d$Zd d d%d&d'Zed dd(d)Ze
ddd*d+Ze
ddd,d-Ze
d.dd/d0Zejd.dd1d0Ze
d2dd3d4Zejd5dd6d4Zd d7d8d9Zdkddd;d<d=d>Zd?d?d@dAdBZe
dCddDdEZd d7dFdGZdHddIdJZe
d;ddKdLZ dMdN Z!dOdP Z"d dQd%dRdSZ#dd d.dTdUdVZ$dd dWdXdYZ%e
dZdd[d\Z&e
d dd]d^Z'e
d;dd_d`Z(e
daddbdcZ)e
daddddeZ*e
dfddgdhZ+e
dddidjZ,d:S )lr>   z`w:tc` table cell element.zCallable[[], CT_P]Zadd_pzCallable[[], CT_TcPr]get_or_add_tcPrz
list[CT_P]Zp_lstzlist[CT_Tbl]Ztbl_lstzCallable[[CT_Tbl], CT_Tbl]Z_insert_tbl_new_pzw:tcPrzCT_TcPr | NonetcPrw:pw:tblr5   r6   c                 C  s4   | j dur*| j}|dur*|j tjkr*|jS | jd S )zThe row index that marks the bottom extent of the vertical span of this cell.

        This is one greater than the index of the bottom-most row of the span, similar
        to how a slice of the cell's rows would be specified.
        Nr   )vMerge	_tc_belowr   CONTINUEbottom_tr_idx)r;   tc_belowr.   r.   r/   r     s
    
zCT_Tc.bottomc                 C  s   |  dD ]}| | q
dS )a/  Remove all content elements, preserving `w:tcPr` element if present.

        Note that this leaves the `w:tc` element in an invalid state because it doesn't
        contain at least one block-level element. It's up to the caller to add a
        `w:p`child element as the last content element.
        z./*[not(self::w:tcPr)]N)xpathremove)r;   er.   r.   r/   clear_content  s    zCT_Tc.clear_contentc                 C  s(   | j j}tdd | dD }|| S )zStarting offset of `tc` in the layout-grid columns of its table.

        A cell in the leftmost grid-column has offset 0.
        c                 s  s   | ]}|j V  qd S rM   )r@   ).0rB   r.   r.   r/   	<genexpr>  s   z$CT_Tc.grid_offset.<locals>.<genexpr>z./preceding-sibling::w:tc)_trr=   sumr   )r;   r=   Zpreceding_tc_grid_spansr.   r.   r/   r?     s
    zCT_Tc.grid_offsetc                 C  s   | j }|du rdS |jS )zzThe integer number of columns this cell spans.

        Determined by ./w:tcPr/w:gridSpan/@val, it defaults to 1.
        Nr   )r   r@   r;   r   r.   r.   r/   r@     s    zCT_Tc.grid_spanrK   c                 C  s   |   }||_d S rM   )r   r@   r;   rL   r   r.   r.   r/   r@     s    zlist[CT_P | CT_Tbl]c                 C  s
   |  dS )zGenerate all `w:p` and `w:tbl` elements in this document-body.

        Elements appear in document order. Elements shaded by nesting in a `w:ins` or
        other "wrapper" element will not be included.
        z./w:p | ./w:tblr   rX   r.   r.   r/   inner_content_elements  s    zCT_Tc.inner_content_elementsc                 c  s4   t dt dt df}| D ]}|j|v r|V  qdS )zpGenerate a reference to each of the block-level content elements in this
        cell, in the order they appear.r   r   zw:sdtN)r   tag)r;   Zblock_item_tagschildr.   r.   r/   iter_block_items  s    
zCT_Tc.iter_block_itemsc                 C  s   | j S )z?The grid column index at which this ``<w:tc>`` element appears.)r?   rX   r.   r.   r/   left  s    z
CT_Tc.left)other_tcr7   c                 C  s4   |  |\}}}}| jj| |}||| |S )zReturn top-left `w:tc` element of a new span.

        Span is formed by merging the rectangular region defined by using this tc
        element and `other_tc` as diagonal corners.
        )_span_dimensions_tblrF   rC   _grow_to)r;   r   topr   heightrl   top_tcr.   r.   r/   merge  s    zCT_Tc.mergec                 C  s   t ttdtd S )zUA new `w:tc` element, containing an empty paragraph as the required EG_BlockLevelElt.z<w:tc %s><w:p/></w:tc>ry   )r   r>   r   r
   )rn   r.   r.   r/   rW     s    z	CT_Tc.newc                 C  s   | j | j S )a  The grid column index that marks the right-side extent of the horizontal span
        of this cell.

        This is one greater than the index of the right-most column of the span, similar
        to how a slice of the cell's columns would be specified.
        )r?   r@   rX   r.   r.   r/   right  s    zCT_Tc.rightc                 C  s$   | j du s| j tjkr| jS | jjS )z9The top-most row index in the vertical span of this cell.N)r   r   RESTARTr   	_tc_abover   rX   r.   r.   r/   r     s    z	CT_Tc.toprp   c                 C  s   | j }|du rdS |jS )zCValue of ./w:tcPr/w:vMerge/@val, |None| if w:vMerge is not present.N)r   
vMerge_valr   r.   r.   r/   r     s    zCT_Tc.vMergec                 C  s   |   }||_d S rM   )r   r   r   r.   r.   r/   r   %  s    r%   c                 C  s   | j }|du rdS |jS )zDEMU length represented in `./w:tcPr/w:tcW` or |None| if not present.N)r   rl   r   r.   r.   r/   rl   *  s    zCT_Tc.widthr   c                 C  s   |   }||_d S rM   )r   rl   r   r.   r.   r/   rl   2  s    )r   c                 C  s"   | j r|j rt| j |j  | _ dS )zAdd the width of `other_tc` to this cell.

        Does nothing if either this tc or `other_tc` does not have a specified width.
        N)rl   r   )r;   r   r.   r.   r/   _add_width_of7  s    zCT_Tc._add_width_ofNCT_Tc | None)rl   r   r   c                   sf   dd fdd}|du r n|} ||||  dkrbj}|dusPJ || d | dS )zGrow this cell to `width` grid columns and `height` rows.

        This is accomplished by expanding horizontal spans and creating continuation
        cells to form vertical spans.
        r>   r   c                   s    | urt jS  dkrd S t jS Nr   )r   r   r   r   r   r;   r.   r/   r   F  s    z"CT_Tc._grow_to.<locals>.vMerge_valNr   )_span_to_widthr   r   )r;   rl   r   r   r   r   r.   r   r/   r   ?  s    	zCT_Tc._grow_toCT_TcPr)r   r7   c                 C  s   |  d| |S )z#Override default `._insert_tcPr()`.r   rR   r   r.   r.   r/   _insert_tcPrV  s    zCT_Tc._insert_tcPrrc   c                 C  s<   t |  }t|dkrdS |d }t|to:t|jdkS )z=True if this cell contains only a single empty `w:p` element.r   Fr   )listr   re   
isinstancer   r_lst)r;   block_itemsZ	only_itemr.   r.   r/   	_is_empty]  s
    zCT_Tc._is_emptyc                 C  sH   || u rdS | j rdS |  |  D ]}|| q&| |   dS )zxAppend the content of this cell to `other_tc`.

        Leaves this cell with a single empty ``<w:p>`` element.
        N)r   _remove_trailing_empty_pr   appendr   )r;   r   Zblock_elementr.   r.   r/   _move_content_toh  s    zCT_Tc._move_content_tort   c                 C  s   t dd S )NzDuse CT_Tbl.new_tbl() to add a new table, specifying rows and columns)NotImplementedErrorrX   r.   r.   r/   _new_tblx  s    zCT_Tc._new_tblc                 C  s   |  d}|r|d S dS )zThe `w:tc` element immediately following this one in this row, or |None| if
        this is the last `w:tc` element in the row.z./following-sibling::w:tcr   Nr   )r;   Zfollowing_tcsr.   r.   r/   _next_tc}  s    
zCT_Tc._next_tcc                 C  s"   |   }|dusJ ||  dS )z-Remove this `w:tc` element from the XML tree.N)rE   r   )r;   Zparent_elementr.   r.   r/   _remove  s    zCT_Tc._removec                 C  sF   t |  }|d }t|ts"dS |}t|jdkr8dS | | dS )zJRemove last content element from this cell if it's an empty `w:p` element.Nr   )r   r   r   r   re   r   r   )r;   r   Zlast_content_elmpr.   r.   r/   r     s    
zCT_Tc._remove_trailing_empty_pztuple[int, int, int, int]c                 C  s   ddddd}ddddd}|| | || | t | j|j}t | j|j}t| j|j}t| j|j}|||| || fS )zReturn a (top, left, height, width) 4-tuple specifying the extents of the
        merged cell formed by using this tc and `other_tc` as opposite corner
        extents.r>   abc                 S  sD   | j |j kr | j|jkr td| j|jkr@| j|jkr@tdd S Nzrequested span not rectangularr   r   r	   r   r   r   r.   r.   r/   raise_on_inverted_L  s    z3CT_Tc._span_dimensions.<locals>.raise_on_inverted_Lc                 S  s   | j |j k r| |fn|| f\}}|j |j k r@|j|jkr@td| j|jk rT| |fn|| f\}}|j|jk r|j|jkrtdd S r   r   )r   r   Ztop_mostotherZ	left_mostr.   r.   r/   raise_on_tee_shaped  s      z3CT_Tc._span_dimensions.<locals>.raise_on_tee_shaped)minr   r   maxr   r   )r;   r   r   r   r   r   r   r   r.   r.   r/   r     s    	

zCT_Tc._span_dimensions)
grid_widthr   r   c                 C  s,   |  | | j|k r"| || q
|| _dS )a  Incorporate `w:tc` elements to the right until this cell spans `grid_width`.

        Incorporated `w:tc` elements are removed (replaced by gridSpan value).

        Raises |ValueError| if `grid_width` cannot be exactly achieved, such as when a
        merged cell would drive the span width greater than `grid_width` or if not
        enough grid columns are available to make this cell that wide. All content from
        incorporated cells is appended to `top_tc`. The val attribute of the vMerge
        element on the single remaining cell is set to `vMerge`. If `vMerge` is |None|,
        the vMerge element is removed if present.
        N)r   r@   _swallow_next_tcr   )r;   r   r   r   r.   r.   r/   r     s    

zCT_Tc._span_to_width)r   r   c                   s^   dd fdd}j }|| |dus.J || |  j|j7  _|  dS )a  Extend the horizontal span of this `w:tc` element to incorporate the
        following `w:tc` element in the row and then delete that following `w:tc`
        element.

        Any content in the following `w:tc` element is appended to the content of
        `top_tc`. The width of the following `w:tc` element is added to this one, if
        present. Raises |InvalidSpanError| if the width of the resulting cell is greater
        than `grid_width` or if there is no next `<w:tc>` element in the row.
        r   next_tcc                   s,   | d u rt dj| j  kr(t dd S )Nznot enough grid columnszspan is not rectangular)r	   r@   r   r   r;   r.   r/   raise_on_invalid_swallow  s    z8CT_Tc._swallow_next_tc.<locals>.raise_on_invalid_swallowN)r   r   r   r@   r   )r;   r   r   r   r   r.   r   r/   r     s    

zCT_Tc._swallow_next_tcrD   c                 C  s   t t| dd S )z+The tbl element this tc element appears in.z./ancestor::w:tbl[position()=1]r   )r   rD   r   rX   r.   r.   r/   r     s    z
CT_Tc._tblc                 C  s   | j | jS )zAThe `w:tc` element immediately above this one in its grid column.)	_tr_aboverC   r?   rX   r.   r.   r/   r     s    zCT_Tc._tc_abovec                 C  s   | j }|du rdS || jS )z=The tc element immediately below this one in its grid column.N)	_tr_belowrC   r?   )r;   Ztr_belowr.   r.   r/   r     s    zCT_Tc._tc_belowr0   c                 C  s   t t| dd S )z*The tr element this tc element appears in.z./ancestor::w:tr[position()=1]r   )r   r0   r   rX   r.   r.   r/   r     s    z	CT_Tc._trc                 C  s   |  d}|std|d S )zThe tr element prior in sequence to the tr this cell appears in.

        Raises |ValueError| if called on a cell in the top-most row.
        z9./ancestor::w:tr[position()=1]/preceding-sibling::w:tr[1]zno tr above topmost tr in w:tblr   )r   rA   )r;   Z	tr_abovesr.   r.   r/   r     s    
zCT_Tc._tr_abovezCT_Row | Nonec                 C  s<   | j j}|| j}z||d  W S  ty6   Y dS 0 dS )zzThe tr element next in sequence after the tr this cell appears in, or |None|
        if this cell appears in the last row.r   N)r   rF   rG   r   
IndexError)r;   rF   rI   r.   r.   r/   r     s    zCT_Tc._tr_belowc                 C  s   | j j| jS )z;The row index of the tr element this tc element appears in.)r   rF   rG   r   rX   r.   r.   r/   r   
  s    zCT_Tc._tr_idx)N)-r)   r*   r+   r,   r-   r   r   r   r   rH   rZ   r   r   r?   r@   r[   r   r   r   r   r   rW   r   r   r   rl   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r.   r.   r.   r/   r>     s   
	


r>   c                   @  sL  e Zd ZU dZded< ded< ded< ded	< d
ed< d
ed< d
ed< dZededd dZded< ededd dZded< ededd dZ	ded< ededd dZ
ded < [ed!d"d#d$Zejd!d%d&d$Zed'd( Zejd)d%d*d(Zed+d, Zejd-d%d.d,Zed/d"d0d1Zejd2d%d3d1ZdS )4r   z5``<w:tcPr>`` element, defining table cell properties.zCallable[[], CT_DecimalNumber]get_or_add_gridSpanzCallable[[], CT_TblWidth]get_or_add_tcWzCallable[[], CT_VerticalJc]get_or_add_vAlignzCallable[[], CT_VMerge]_add_vMerger   _remove_gridSpan_remove_vAlign_remove_vMerge)
w:cnfStylew:tcW
w:gridSpanzw:hMergew:vMergezw:tcBordersr   zw:noWrapzw:tcMarzw:textDirectionzw:tcFitTextw:vAlignz
w:hideMarkz	w:headersz	w:cellInsz	w:cellDelzw:cellMergezw:tcPrChanger      Nr   zCT_TblWidth | NonetcWr      CT_DecimalNumber | NonegridSpanr      zCT_VMerge | Noner   r      zCT_VerticalJc | NonevAlignr5   r6   c                 C  s   | j }|du rdS |jS )zsThe integer number of columns this cell spans.

        Determined by ./w:gridSpan/@val, it defaults to 1.
        Nr   )r  r&   )r;   r  r.   r.   r/   r@   =  s    zCT_TcPr.grid_spanrK   c                 C  s   |    |dkr||  _d S r   )r   r   r&   r   r.   r.   r/   r@   F  s    c                 C  s   | j }|du rdS |jS )zValue of `w:val` attribute on  `w:vAlign` child.

        Value is |None| if `w:vAlign` child is not present. The `w:val` attribute on
        `w:vAlign` is required.
        N)r  r&   )r;   r  r.   r.   r/   
vAlign_valL  s    zCT_TcPr.vAlign_valz!WD_CELL_VERTICAL_ALIGNMENT | Nonec                 C  s"   |d u r|    d S ||  _d S rM   )r   r   r&   r   r.   r.   r/   r	  X  s    c                 C  s   | j }|du rdS |jS )zeThe value of the ./w:vMerge/@val attribute, or |None| if the w:vMerge element
        is not present.N)r   r&   )r;   r   r.   r.   r/   r   _  s    zCT_TcPr.vMerge_valrp   c                 C  s   |    |d ur||  _d S rM   )r   r   r&   r   r.   r.   r/   r   h  s    r%   c                 C  s   | j }|du rdS |jS )zJEMU length in `./w:tcW` or |None| if not present or its type is not 'dxa'.N)r  rl   )r;   r  r.   r.   r/   rl   n  s    zCT_TcPr.widthr   c                 C  s   |   }||_d S rM   )r   rl   )r;   rL   r  r.   r.   r/   rl   v  s    )r)   r*   r+   r,   r-   r   r   r  r  r   r  rZ   r@   r[   r	  r   rl   r.   r.   r.   r/   r     sL   


r   c                   @  s   e Zd ZU dZded< dZededd dZd	ed
< ededd dZd	ed< ededd dZ	ded< [e
ddddZe
ddddZe
ddddZejddddZe
dd Zejddd dZdS )!rU   z4``<w:trPr>`` element, defining table row properties.zCallable[[], CT_Height]get_or_add_trHeight)r   zw:divIdw:gridBeforew:gridAfterz	w:wBeforezw:wAfterzw:cantSplit
w:trHeightzw:tblHeaderr   r   zw:hiddenzw:inszw:delzw:trPrChanger  r   Nr   r  	gridAfterr  r  
gridBeforer  r   zCT_Height | NonetrHeightr5   r6   c                 C  s   | j }|du rdS |jS r8   )r  r&   )r;   r  r.   r.   r/   r9     s    zCT_TrPr.grid_afterc                 C  s   | j }|du rdS |jS r<   )r  r&   )r;   r  r.   r.   r/   r=     s    zCT_TrPr.grid_beforer'   c                 C  s   | j }|du rdS |jS )zCReturn the value of `w:trHeight@w:hRule`, or |None| if not present.N)r  r(   r;   r  r.   r.   r/   rJ     s    zCT_TrPr.trHeight_hRulerK   c                 C  s(   |d u r| j d u rd S |  }||_d S rM   )r  r
  r(   r;   rL   r  r.   r.   r/   rJ     s    c                 C  s   | j }|du rdS |jS )zAReturn the value of `w:trHeight@w:val`, or |None| if not present.N)r  r&   r  r.   r.   r/   rO     s    zCT_TrPr.trHeight_valr%   c                 C  s(   |d u r| j d u rd S |  }||_d S rM   )r  r
  r&   r  r.   r.   r/   rO     s    )r)   r*   r+   r,   r-   r   r   r  r  r  rZ   r9   r=   rJ   r[   rO   r.   r.   r.   r/   rU   |  s2   

rU   c                   @  s$   e Zd ZU dZedeZded< dS )CT_VerticalJcz:`w:vAlign` element, specifying vertical alignment of cell.r$   r   r&   N)r)   r*   r+   r,   r   r   r&   r-   r.   r.   r.   r/   r    s   
r  c                   @  s*   e Zd ZU dZedeejdZded< dS )	CT_VMergezG``<w:vMerge>`` element, specifying vertical merging behavior of a cell.r$   )defaultrp   r&   N)	r)   r*   r+   r,   r   r   r   r&   r-   r.   r.   r.   r/   r    s   
r  N)=r,   
__future__r   typingr   r   r   Zdocx.enum.tabler   r   r   Zdocx.exceptionsr	   Zdocx.oxml.nsr
   r   Zdocx.oxml.parserr   Zdocx.oxml.sharedr   Zdocx.oxml.simpletypesr   r   r   r   r   Zdocx.oxml.text.paragraphr   Zdocx.oxml.xmlchemyr   r   r   r   r   r   r   Zdocx.sharedr   r   r   r   Zdocx.enum.textr   r    r!   Zdocx.oxml.text.parfmtr"   r#   r0   rD   r^   r   r   r\   rP   r   r>   r   rU   r  r  r.   r.   r.   r/   <module>   s@   $	aq]	  mlH