o
    jqiJ                     @  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.   V/home/app/PaddleOCR-VL/.venv_paddleocr/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]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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   )r4   
grid_afterselfr4   r.   r.   r/   r9   ?      zCT_Row.grid_afterc                 C  r7   zEThe number of unpopulated layout-grid cells at the start of this row.Nr   )r4   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>   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     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)r4   trHeight_hRuler:   r.   r.   r/   rN   j   r<   zCT_Row.trHeight_hRulevaluec                 C     |   }||_d S N)r1   rN   r;   rO   r4   r.   r.   r/   rN   r      
c                 C  rM   )zHReturn the value of `w:trPr/w:trHeight@w:val`, or |None| if not present.N)r4   trHeight_valr:   r.   r.   r/   rT   w   r<   zCT_Row.trHeight_valr%   c                 C  rP   rQ   )r1   rT   rR   r.   r.   r/   rT      rS   
CT_TblPrExc                 C  s   |  d| d S Nr   insert)r;   r3   r.   r.   r/   _insert_tblPrEx   s   zCT_Row._insert_tblPrExCT_TrPrc                 C  s,   | j }|d ur|| d S | d| d S rV   )r3   ZaddnextrX   )r;   r4   r3   r.   r.   r/   _insert_trPr   s   zCT_Row._insert_trPrc                 C  s   t  S rQ   )r@   newr;   r.   r.   r/   _new_tc   s   zCT_Row._new_tcNr5   r6   )r?   r6   r5   r@   r5   r'   rO   r'   rO   r%   )r3   rU   )r4   rZ   )r)   r*   r+   r,   r-   r   r3   r4   r   rC   propertyr9   r>   rD   rK   rN   setterrT   rY   r[   r^   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 ):rF   z``<w:tbl>`` element.zCallable[[], CT_Row]Zadd_trzlist[CT_Row]rH   zw:tblPrCT_TblPrtblPrz	w:tblGrid
CT_TblGridtblGridzw:trr5   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)rf   
bidiVisualr&   )r;   rk   r.   r.   r/   bidiVisual_val   s   zCT_Tbl.bidiVisual_valrO   WD_TABLE_DIRECTION | Nonec                 C  s,   | j }|d u r|  d S t|| _d S rQ   )rf   _remove_bidiVisualboolget_or_add_bidiVisualr&   )r;   rO   rf   r.   r.   r/   rl      s   c                 C  s   t | jjS )z)The number of grid columns in this table.)lenrh   gridCol_lstr]   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)rH   r2   )r;   trrC   r.   r.   r/   iter_tcs   s   

zCT_Tbl.iter_tcsrowsr6   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   rF   r   _tbl_xml)clsrv   rw   rx   r.   r.   r/   new_tbl   s   zCT_Tbl.new_tbl
str | Nonec                 C  rj   )zH`w:tblPr/w:tblStyle/@w:val` (a table style id) or |None| if not present.N)rf   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)rf   _remove_tblStyle_add_tblStyler&   )r;   r   rf   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)rz   rv   rw   rx   	col_widthr.   r.   r/   ry      s   
zCT_Tbl._tbl_xmlrs   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)rz   rs   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)rz   r   rs   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   )rz   rs   r   r.   r.   r/   r      s
   zCT_Tbl._tcs_xmlN)r5   ri   )rO   rm   )rv   r6   rw   r6   rx   r   r5   rF   r5   r|   )r   r|   r5   r   )rv   r6   rw   r6   rx   r   r5   r   )rs   r6   r   r   r5   r   )r   r6   rs   r6   r   r   r5   r   )r)   r*   r+   r,   r-   r   rf   rh   r   rt   rc   rl   rd   rs   ru   classmethodr{   r   ry   r   r   r   r.   r.   r.   r/   rF      s8   
 

rF   c                   @  s.   e Zd ZU dZded< ded< edddZd	S )
rg   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]rr   z	w:gridCol)zw:tblGridChange
successorsN)r)   r*   r+   r,   r-   r   ZgridColr.   r.   r.   r/   rg     s
   
 rg   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   r5   r6   c                 C  rE   )zHIndex of this `w:gridCol` element within its parent `w:tblGrid` element.)r   rg   rG   rr   rI   )r;   rh   r.   r.   r/   gridCol_idx  rL   zCT_TblGridCol.gridCol_idxNr_   )
r)   r*   r+   r,   r   r   r   r-   rc   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 )4re   z}``<w:tblPr>`` element, child of ``<w:tbl>``, holds child elements that define
    table properties such as style and borders.zCallable[[], CT_OnOff]rp   zCallable[[], CT_Jc]get_or_add_jczCallable[[], CT_TblLayoutType]get_or_add_tblLayoutzCallable[[], CT_String]r   Callable[[], None]rn   
_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 | Nonerk   r      zCT_Jc | Nonejcr      zCT_TblLayoutType | None	tblLayoutr5   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.alignmentrO   c                 C  s,   |    |d u r
d S |  }td||_d S )Nr   )r   r   r   r&   )r;   rO   r   r.   r.   r/   r   _  s
   ro   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;   rO   r   r.   r.   r/   r   p  s   c                 C  rM   )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 rQ   )r   r   r&   r;   rO   r.   r.   r/   r   ~     )r5   r   )rO   r   r5   ro   )rO   ro   rO   r|   )r)   r*   r+   r,   r-   _tag_seqr   r}   rk   r   r   rc   r   rd   r   r   r.   r.   r.   r/   re   )  sF   
 
re   c                   @  s   e Zd ZdZdS )rU   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/   rU     s    rU   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   r6   r   r   r5   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   r]   r.   r.   r/   rx     s   

zCT_TblWidth.widthrO   r   c                 C  s   d| _ t|j| _d S )Nr   )r   r   r   r   r   r.   r.   r/   rx     s   Nr5   r%   rO   r   )r)   r*   r+   r,   r   r   r   r-   r   r   rc   rx   rd   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 )r@   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 rQ   )rA   ).0rC   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  r7   )zzThe integer number of columns this cell spans.

        Determined by ./w:tcPr/w:gridSpan/@val, it defaults to 1.
        Nr   )r   rA   r;   r   r.   r.   r/   rA        zCT_Tc.grid_spanrO   c                 C  rP   rQ   )r   rA   r;   rO   r   r.   r.   r/   rA     rS   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   r]   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;   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?   r]   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_tblrH   rD   _grow_to)r;   r   topr   heightrx   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   r@   r   r
   )rz   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.
        )r?   rA   r]   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   r]   r.   r.   r/   r     s   z	CT_Tc.topr|   c                 C  rM   )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  rP   rQ   )r   r   r   r.   r.   r/   r   %  rS   r%   c                 C  rM   )zDEMU length represented in `./w:tcPr/w:tcW` or |None| if not present.N)r   rx   r   r.   r.   r/   rx   *  r<   zCT_Tc.widthr   c                 C  rP   rQ   )r   rx   r   r.   r.   r/   rx   2  rS   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)rx   r   )r;   r   r.   r.   r/   _add_width_of7  s   zCT_Tc._add_width_ofNrx   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   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   )r   r@   )_span_to_widthr   r   )r;   rx   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   rW   r   r.   r.   r/   _insert_tcPrV  s   zCT_Tc._insert_tcPrro   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   rq   
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_tor   c                 C  s   t d)NzDuse CT_Tbl.new_tbl() to add a new table, specifying rows and columns)NotImplementedErrorr]   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)rG   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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   rq   r   r   )r;   r   Z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.ar@   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   Ztop_mostotherZ	left_mostr.   r.   r/   raise_on_tee_shaped  s     z3CT_Tc._span_dimensions.<locals>.raise_on_tee_shapedN)r   r@   r   r@   )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   rA   _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	   rA   )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   rA   r   )r;   r  r   r  r  r.   r  r/   r    s   

zCT_Tc._swallow_next_tcrF   c                 C     t t| dd S )z+The tbl element this tc element appears in.z./ancestor::w:tbl[position()=1]r   )r   rF   r   r]   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_aboverD   r?   r]   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_belowrD   r?   )r;   Z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   r]   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   rB   )r;   Z	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   rH   rI   r   
IndexError)r;   rH   rK   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   rH   rI   r   r]   r.   r.   r/   r   
  s   zCT_Tc._tr_idxr_   rO   r6   )r5   r   )r   r@   r5   r@   )r5   r@   r   r   r   r   )r   r@   rQ   )rx   r6   r   r6   r   r   )r   r   r5   r   r   )r5   r   )r5   r   )r   r@   r5   r   )r  r6   r   r@   r   r|   )r  r6   r   r@   )r5   rF   )r5   r0   )r5   r  )-r)   r*   r+   r,   r-   r   r   r   r   rJ   rc   r   r   r?   rA   rd   r   r   r   r   r   r\   r   r   r   rx   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                   @  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vAlignr5   r6   c                 C  r7   )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/   rA   =  r   zCT_TcPr.grid_spanrO   c                 C  s"   |    |dkr||  _d S d S r   )r  r  r&   r   r.   r.   r/   rA   F     c                 C  rM   )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_val!WD_CELL_VERTICAL_ALIGNMENT | Nonec                 C  s"   |d u r
|    d S ||  _d S rQ   )r  r  r&   r   r.   r.   r/   r#  X  r   c                 C  rM   )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 rQ   )r  r  r&   r   r.   r.   r/   r   h  r"  r%   c                 C  rM   )zJEMU length in `./w:tcW` or |None| if not present or its type is not 'dxa'.N)r  rx   )r;   r  r.   r.   r/   rx   n  r<   zCT_TcPr.widthr   c                 C  rP   rQ   )r  rx   )r;   rO   r  r.   r.   r/   rx   v  rS   r_   r  )rO   r$  r   r   r   )r)   r*   r+   r,   r-   r   r   r  r  r   r!  rc   rA   rd   r#  r   rx   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 )%rZ   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  r7   r8   )r)  r&   )r;   r)  r.   r.   r/   r9        zCT_TrPr.grid_afterc                 C  r7   r=   )r*  r&   )r;   r*  r.   r.   r/   r>     r,  zCT_TrPr.grid_beforer'   c                 C  rM   )zCReturn the value of `w:trHeight@w:hRule`, or |None| if not present.N)r+  r(   r;   r+  r.   r.   r/   rN     r,  zCT_TrPr.trHeight_hRulerO   c                 C  (   |d u r| j d u rd S |  }||_d S rQ   )r+  r%  r(   r;   rO   r+  r.   r.   r/   rN        
c                 C  rM   )zAReturn the value of `w:trHeight@w:val`, or |None| if not present.N)r+  r&   r-  r.   r.   r/   rT     r,  zCT_TrPr.trHeight_valr%   c                 C  r.  rQ   )r+  r%  r&   r/  r.   r.   r/   rT     r0  r_   r`   ra   rb   )r)   r*   r+   r,   r-   r   r   r)  r*  r+  rc   r9   r>   rN   rd   rT   r.   r.   r.   r/   rZ   |  s4   
 
rZ   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/   r1    s
   
 r1  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/   r2    s
   
 r2  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   rF   rg   r   r   re   rU   r   r@   r   rZ   r1  r2  r.   r.   r.   r/   <module>   sB    $	aq]	  mlH