o
    Z2h`                     @  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rd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.   `/home/skpark/git/infrasmart_work/infrasmart/venv/lib/python3.10/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Ze
d.ddZd/ddZe
d.ddZe
d0ddZejd1d dZe
d!d" Zejd2d$d"Zd3d&d'Zd4d)d*Zd+d, Zd-S )5CT_Rowz``<w:tr>`` element.zCallable[[], CT_Tc]add_tczCallable[[], CT_TrPr]get_or_add_trPr	_add_trPrzlist[CT_Tc]tc_lstz	w:tblPrExzCT_TblPrEx | NonetblPrExzw:trPrzCT_TrPr | NonetrPrzw:tcreturnintc                 C     | j }|du r	dS |jS zCThe number of unpopulated layout-grid cells at the end of this row.Nr   )r6   
grid_afterselfr6   r.   r.   r/   r;   ?      zCT_Row.grid_afterc                 C  r9   zEThe number of unpopulated layout-grid cells at the start of this row.Nr   )r6   grid_beforer<   r.   r.   r/   r@   G   r>   zCT_Row.grid_beforegrid_offsetCT_Tcc                 C  sJ   || j  }| jD ]}|dk r n|dkr|  S ||j8 }qtd| )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=)r@   r4   	grid_span
ValueError)r=   rA   remaining_offsettcr.   r.   r/   tc_at_grid_offsetO   s   

zCT_Row.tc_at_grid_offsetc                 C     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      zCT_Row.tr_idxr'   c                 C     | j }|du r	dS |jS )zFThe value of `./w:trPr/w:trHeight/@w:hRule`, or |None| if not present.N)r6   trHeight_hRuler<   r.   r.   r/   rQ   j   r>   zCT_Row.trHeight_hRulevaluec                 C     |   }||_d S N)r2   rQ   r=   rR   r6   r.   r.   r/   rQ   r      
c                 C  rP   )zHReturn the value of `w:trPr/w:trHeight@w:val`, or |None| if not present.N)r6   trHeight_valr<   r.   r.   r/   rW   w   r>   zCT_Row.trHeight_valr%   c                 C  rS   rT   )r2   rW   rU   r.   r.   r/   rW      rV   
CT_TblPrExc                 C  s   |  d| d S Nr   insert)r=   r5   r.   r.   r/   _insert_tblPrEx   s   zCT_Row._insert_tblPrExCT_TrPrc                 C  s,   | j }|d ur|| d S | d| d S rY   )r5   addnextr[   )r=   r6   r5   r.   r.   r/   _insert_trPr   s   zCT_Row._insert_trPrc                 C  s   t  S rT   )rB   newr=   r.   r.   r/   _new_tc   s   zCT_Row._new_tcNr7   r8   )rA   r8   r7   rB   r7   r'   rR   r'   rR   r%   )r5   rX   )r6   r]   )r)   r*   r+   r,   r-   r   r5   r6   r   rF   propertyr;   r@   rG   rN   rQ   setterrW   r\   r_   rb   r.   r.   r.   r/   r0   1   s6   
 



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
d2ddZejd3ddZe
dd Zdd Zed4ddZe
d5d d!Zejd6d$d!Zed7d&d'Zed8d*d+Zed9d-d.Zed8d/d0Zd1S ):rI   z``<w:tbl>`` element.zCallable[[], CT_Row]add_trzlist[CT_Row]rK   zw:tblPrCT_TblPrtblPrz	w:tblGrid
CT_TblGridtblGridzw:trr7   bool | Nonec                 C     | 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)rk   
bidiVisualr&   )r=   rp   r.   r.   r/   bidiVisual_val   s   zCT_Tbl.bidiVisual_valrR   WD_TABLE_DIRECTION | Nonec                 C  s,   | j }|d u r|  d S t|| _d S rT   )rk   _remove_bidiVisualboolget_or_add_bidiVisualr&   )r=   rR   rk   r.   r.   r/   rq      s   c                 C  s   t | jjS )z)The number of grid columns in this table.)lenrm   gridCol_lstra   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)rK   r4   )r=   trrF   r.   r.   r/   iter_tcs   s   

zCT_Tbl.iter_tcsrowsr8   colswidthr   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   rI   r   _tbl_xml)clsr{   r|   r}   r.   r.   r/   new_tbl   s   zCT_Tbl.new_tbl
str | Nonec                 C  ro   )zH`w:tblPr/w:tblStyle/@w:val` (a table style id) or |None| if not present.N)rk   tblStyler&   r=   r   r.   r.   r/   tblStyle_val   s   zCT_Tbl.tblStyle_valstyleIdNonec                 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)rk   _remove_tblStyle_add_tblStyler&   )r=   r   rk   r.   r.   r/   r      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)r   r{   r|   r}   	col_widthr.   r.   r/   r~      s   
zCT_Tbl._tbl_xmlrx   r   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)r   rx   r   xml_r.   r.   r/   r      s
   zCT_Tbl._tblGrid_xml	row_countc                 C  s   d|  || d| S )Nz	  <w:tr>
z
  </w:tr>
)_tcs_xml)r   r   rx   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   )r   rx   r   r.   r.   r/   r      s
   zCT_Tbl._tcs_xmlN)r7   rn   )rR   rr   )r{   r8   r|   r8   r}   r   r7   rI   r7   r   )r   r   r7   r   )r{   r8   r|   r8   r}   r   r7   r   )rx   r8   r   r   r7   r   )r   r8   rx   r8   r   r   r7   r   )r)   r*   r+   r,   r-   r   rk   rm   r   ry   rg   rq   rh   rx   rz   classmethodr   r   r~   r   r   r   r.   r.   r.   r/   rI      s8   
 

rI   c                   @  s.   e Zd ZU dZded< ded< edddZd	S )
rl   zq`w:tblGrid` element.

    Child of `w:tbl`, holds `w:gridCol> elements that define column count, width, etc.
    zCallable[[], CT_TblGridCol]add_gridColzlist[CT_TblGridCol]rw   z	w:gridCol)zw:tblGridChange
successorsN)r)   r*   r+   r,   r-   r   gridColr.   r.   r.   r/   rl     s
   
 rl   c                   @  s2   e Zd ZU dZedeZded< ed
ddZ	d	S )CT_TblGridColzB`w:gridCol` element, child of `w:tblGrid`, defines a table column.w:wr%   r   r7   r8   c                 C  rH   )zHIndex of this `w:gridCol` element within its parent `w:tblGrid` element.)r   rl   rJ   rw   rL   )r=   rm   r.   r.   r/   gridCol_idx  rO   zCT_TblGridCol.gridCol_idxNrc   )
r)   r*   r+   r,   r   r   r   r-   rg   r   r.   r.   r.   r/   r     s   
 r   c                   @  $   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:typer   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$Zejd0d&d$Zed1d(d)Zejd2d*d)Zed+d, Zejd3d.d,ZdS )4rj   z}``<w:tblPr>`` element, child of ``<w:tbl>``, holds child elements that define
    table properties such as style and borders.zCallable[[], CT_OnOff]ru   zCallable[[], CT_Jc]get_or_add_jczCallable[[], CT_TblLayoutType]get_or_add_tblLayoutzCallable[[], CT_String]r   Callable[[], None]rs   
_remove_jcr   )
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 | Noner   r      zCT_OnOff | Nonerp   r      zCT_Jc | Nonejcr      zCT_TblLayoutType | None	tblLayoutr7   WD_TABLE_ALIGNMENT | Nonec                 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     zCT_TblPr.alignmentrR   c                 C  s,   |    |d u r
d S |  }td||_d S )Nr   )r   r   r   r&   )r=   rR   r   r.   r.   r/   r   _  s
   rt   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|_d S d|_d S )Nr   r   )r   r   )r=   rR   r   r.   r.   r/   r   p  s   c                 C  rP   )ziReturn the value of the ``val`` attribute of the ``<w:tblStyle>`` child or
        |None| if not present.N)r   r&   r   r.   r.   r/   styleu     zCT_TblPr.styler   c                 C  s"   |    |d u r
d S ||  _d S rT   )r   r   r&   r=   rR   r.   r.   r/   r   ~     )r7   r   )rR   r   r7   rt   )rR   rt   rR   r   )r)   r*   r+   r,   r-   _tag_seqr   r   rp   r   r   rg   r   rh   r   r   r.   r.   r.   r/   rj   )  sF   
 
rj   c                   @  s   e Zd ZdZdS )rX   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/   rX     s    rX   c                   @  sL   e Zd ZU dZedeZded< edeZ	e
ddd	Zejddd	ZdS )CT_TblWidthzJUsed for `w:tblW` and `w:tcW` and others, specifies a table-related width.r   r8   r   r   r7   r%   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   r   ra   r.   r.   r/   r}     s   

zCT_TblWidth.widthrR   r   c                 C  s   d| _ t|j| _d S )Nr   )r   r   r   r   r   r.   r.   r/   r}     s   Nr7   r%   rR   r   )r)   r*   r+   r,   r   r   r   r-   r   r   rg   r}   rh   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
dkddZdd Ze
dkddZe
dkddZejdlddZe
dmdd Zd!d" Ze
dkd#d$Zdnd&d'Zedod(d)Ze
dkd*d+Ze
dkd,d-Ze
dpd/d0Zejdqd1d0Ze
drd3d4Zejdsd6d4Zdtd7d8Zdudvd>d?ZdwdAdBZe
dxdDdEZdtdFdGZdydIdJZe
dzdKdLZ dMdN Z!dOdP Z"d{dRdSZ#d|dVdWZ$d}dXdYZ%e
d~d[d\Z&e
dod]d^Z'e
dzd_d`Z(e
ddbdcZ)e
ddddeZ*e
ddgdhZ+e
dkdidjZ,d9S )rB   z`w:tc` table cell element.zCallable[[], CT_P]add_pzCallable[[], CT_TcPr]get_or_add_tcPrz
list[CT_P]p_lstzlist[CT_Tbl]tbl_lstzCallable[[CT_Tbl], CT_Tbl]_insert_tbl_new_pzw:tcPrzCT_TcPr | NonetcPrw:pw:tblr7   r8   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 rT   )rC   ).0rF   r.   r.   r/   	<genexpr>  s    
z$CT_Tc.grid_offset.<locals>.<genexpr>z./preceding-sibling::w:tc)_trr@   sumr   )r=   r@   preceding_tc_grid_spansr.   r.   r/   rA     s
   zCT_Tc.grid_offsetc                 C  r9   )zzThe integer number of columns this cell spans.

        Determined by ./w:tcPr/w:gridSpan/@val, it defaults to 1.
        Nr   )r   rC   r=   r   r.   r.   r/   rC        zCT_Tc.grid_spanrR   c                 C  rS   rT   )r   rC   r=   rR   r   r.   r.   r/   rC     rV   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   ra   r.   r.   r/   inner_content_elements  s   
zCT_Tc.inner_content_elementsc                 c  s6    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=   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.)rA   ra   r.   r.   r/   left  s   z
CT_Tc.leftother_tcc                 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_tblrK   rG   _grow_to)r=   r   topr   heightr}   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>r   )r   rB   r   r
   )r   r.   r.   r/   r`     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.
        )rA   rC   ra   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   ra   r.   r.   r/   r     s   z	CT_Tc.topr   c                 C  rP   )zCValue of ./w:tcPr/w:vMerge/@val, |None| if w:vMerge is not present.N)r   
vMerge_valr   r.   r.   r/   r     r>   zCT_Tc.vMergec                 C  rS   rT   )r   r   r   r.   r.   r/   r   %  rV   r%   c                 C  rP   )zDEMU length represented in `./w:tcPr/w:tcW` or |None| if not present.N)r   r}   r   r.   r.   r/   r}   *  r>   zCT_Tc.widthr   c                 C  rS   rT   )r   r}   r   r.   r.   r/   r}   2  rV   c                 C  s*   | j r|j rt| j |j  | _ dS dS 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)r}   r   )r=   r   r.   r.   r/   _add_width_of7  s   zCT_Tc._add_width_ofNr}   r   r   CT_Tc | Nonec                   sf   d fdd}|du rn|} ||||  dkr1j}|dus&J || d | dS 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   rB   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   )r   rB   )_span_to_widthr   r   )r=   r}   r   r   r   r   r.   r   r/   r   ?  s   zCT_Tc._grow_toCT_TcPrc                 C  s   |  d| |S )z#Override default `._insert_tcPr()`.r   rZ   r   r.   r.   r/   _insert_tcPrT  s   zCT_Tc._insert_tcPrrt   c                 C  sD   t |  }t|dkrdS |d }t|tr t|jdkr dS dS )z=True if this cell contains only a single empty `w:p` element.r   Fr   T)listr   rv   
isinstancer   r_lst)r=   block_items	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   block_elementr.   r.   r/   _move_content_toh  s   zCT_Tc._move_content_tor   c                 C  s   t d)NzDuse CT_Tbl.new_tbl() to add a new table, specifying rows and columns)NotImplementedErrorra   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=   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)rJ   r   )r=   parent_elementr.   r.   r/   _remove  s   zCT_Tc._removec                 C  sF   t |  }|d }t|tsdS |}t|jdkrdS | | dS )zJRemove last content element from this cell if it's an empty `w:p` element.Nr   )r   r   r   r   rv   r   r   )r=   r   last_content_elmpr.   r.   r/   r     s   
zCT_Tc._remove_trailing_empty_ptuple[int, int, int, int]c                 C  st   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.arB   bc                 S  sH   | j |j kr| j|jkrtd| j|jkr | j|jkr"tdd S d S Nzrequested span not rectangularr   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 r*| |fn|| f\}}|j|jk r@|j|jkrBtdd S d S r  r  )r  r  top_mostother	left_mostr.   r.   r/   raise_on_tee_shaped  s     z3CT_Tc._span_dimensions.<locals>.raise_on_tee_shapedN)r  rB   r  rB   )minr   r   maxr   r   )r=   r   r  r  r   r   r   r   r.   r.   r/   r     s   


	
zCT_Tc._span_dimensions
grid_widthr   c                 C  s4   |  | | j|k r| || | j|k s
|| _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   rC   _swallow_next_tcr   )r=   r  r   r   r.   r.   r/   r     s
   



zCT_Tc._span_to_widthc                   sZ   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.
        next_tcr   c                   s,   | d u rt dj| j  krt dd S )Nznot enough grid columnszspan is not rectangular)r	   rC   )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   rC   r  )r=   r  r   r  r  r.   r  r/   r    s   

zCT_Tc._swallow_next_tcrI   c                 C     t t| dd S )z+The tbl element this tc element appears in.z./ancestor::w:tbl[position()=1]r   )r   rI   r   ra   r.   r.   r/   r        z
CT_Tc._tblc                 C  s   | j | jS )zAThe `w:tc` element immediately above this one in its grid column.)	_tr_aboverG   rA   ra   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_belowrG   rA   )r=   tr_belowr.   r.   r/   r     r   zCT_Tc._tc_belowr0   c                 C  r  )z*The tr element this tc element appears in.z./ancestor::w:tr[position()=1]r   )r   r0   r   ra   r.   r.   r/   r     r  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   rD   )r=   	tr_abovesr.   r.   r/   r    s   
zCT_Tc._tr_aboveCT_Row | Nonec                 C  s8   | j j}|| j}z||d  W S  ty   Y dS w )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   rK   rL   r   
IndexError)r=   rK   rN   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   rK   rL   r   ra   r.   r.   r/   r   
  s   zCT_Tc._tr_idxrc   rR   r8   )r7   r   )r   rB   r7   rB   )r7   rB   r   r   r   r   )r   rB   rT   )r}   r8   r   r8   r   r   )r   r   r7   r   r   )r7   r   )r7   r   )r   rB   r7   r
  )r  r8   r   rB   r   r   )r  r8   r   rB   )r7   rI   )r7   r0   )r7   r!  )-r)   r*   r+   r,   r-   r   r   r   r	  rM   rg   r   r   rA   rC   rh   r   r   r   r   r   r`   r   r   r   r}   r   r   r   r   r   r  r  r  r   r   r   r  r   r   r   r   r  r  r   r.   r.   r.   r/   rB     s   
 
	








rB   c                   @  s4  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d4d#d$Zejd5d&d$Zed'd( Zejd6d*d(Zed+d, Zejd7d.d,Zed8d0d1Zejd9d3d1ZdS ):r   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vAlignr7   r8   c                 C  r9   )zsThe integer number of columns this cell spans.

        Determined by ./w:gridSpan/@val, it defaults to 1.
        Nr   )r4  r&   )r=   r4  r.   r.   r/   rC   =  r   zCT_TcPr.grid_spanrR   c                 C  s"   |    |dkr||  _d S d S r   )r(  r$  r&   r   r.   r.   r/   rC   F     c                 C  rP   )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)r7  r&   )r=   r7  r.   r.   r/   
vAlign_valL  s   zCT_TcPr.vAlign_val!WD_CELL_VERTICAL_ALIGNMENT | Nonec                 C  s"   |d u r
|    d S ||  _d S rT   )r)  r&  r&   r   r.   r.   r/   r9  X  r   c                 C  rP   )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   _  r   zCT_TcPr.vMerge_valr   c                 C  s"   |    |d ur||  _d S d S rT   )r*  r'  r&   r   r.   r.   r/   r   h  r8  r%   c                 C  rP   )zJEMU length in `./w:tcW` or |None| if not present or its type is not 'dxa'.N)r1  r}   )r=   r1  r.   r.   r/   r}   n  r>   zCT_TcPr.widthr   c                 C  rS   rT   )r%  r}   )r=   rR   r1  r.   r.   r/   r}   v  rV   rc   r#  )rR   r:  r   r   r   )r)   r*   r+   r,   r-   r   r   r1  r4  r   r7  rg   rC   rh   r9  r   r}   r.   r.   r.   r/   r     sN   
 

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Ze
d!ddZe
d"ddZejd#ddZe
dd Zejd$d dZdS )%r]   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   r3  	gridAfterr<  r2  
gridBeforer>  r   zCT_Height | NonetrHeightr7   r8   c                 C  r9   r:   )r?  r&   )r=   r?  r.   r.   r/   r;        zCT_TrPr.grid_afterc                 C  r9   r?   )r@  r&   )r=   r@  r.   r.   r/   r@     rB  zCT_TrPr.grid_beforer'   c                 C  rP   )zCReturn the value of `w:trHeight@w:hRule`, or |None| if not present.N)rA  r(   r=   rA  r.   r.   r/   rQ     rB  zCT_TrPr.trHeight_hRulerR   c                 C  (   |d u r| j d u rd S |  }||_d S rT   )rA  r;  r(   r=   rR   rA  r.   r.   r/   rQ        
c                 C  rP   )zAReturn the value of `w:trHeight@w:val`, or |None| if not present.N)rA  r&   rC  r.   r.   r/   rW     rB  zCT_TrPr.trHeight_valr%   c                 C  rD  rT   )rA  r;  r&   rE  r.   r.   r/   rW     rF  rc   rd   re   rf   )r)   r*   r+   r,   r-   r   r   r?  r@  rA  rg   r;   r@   rQ   rh   rW   r.   r.   r.   r/   r]   |  s4   
 
r]   c                   @  r   )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/   rG    s
   
 rG  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$   )defaultr   r&   N)	r)   r*   r+   r,   r   r   r   r&   r-   r.   r.   r.   r/   rH    s
   
 rH  N)=r,   
__future__r   typingr   r   r   docx.enum.tabler   r   r   docx.exceptionsr	   docx.oxml.nsr
   r   docx.oxml.parserr   docx.oxml.sharedr   docx.oxml.simpletypesr   r   r   r   r   docx.oxml.text.paragraphr   docx.oxml.xmlchemyr   r   r   r   r   r   r   docx.sharedr   r   r   r   docx.enum.textr   r    r!   docx.oxml.text.parfmtr"   r#   r0   rI   rl   r   r   rj   rX   r   rB   r   r]   rG  rH  r.   r.   r.   r/   <module>   sB    $	aq]	  mlH