
    hiJ                       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e          Z/ G d de          Z0 G d de          Z1 G d de          Z2 G d de          Z3 G d de          Z4 G d de          Z5 G d d e          Z6 G d! d"e          Z7 G d# d$e          Z8 G d% d&e          Z9 G d' d(e          Z: G d) d*e          Z; G 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                  X    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*        g/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/docx/oxml/table.pyr%   r%   &   su         LL** C     (9'8%( (E      r1   r%   c                  j   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            Ze
d%d            Zd&dZe
d%d            Ze
d'd            Zej        d(d            Ze
d             Zej        d)d            Zd*d Zd+d"Zd# Zd$S ),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                &    | j         }|dS |j        S zCThe number of unpopulated layout-grid cells at the end of this row.Nr   )r:   
grid_afterselfr:   s     r2   r?   zCT_Row.grid_after?   s     y<1r1   c                &    | j         }|dS |j        S zEThe number of unpopulated layout-grid cells at the start of this row.Nr   )r:   grid_beforer@   s     r2   rD   zCT_Row.grid_beforeG   s     y<1r1   grid_offsetCT_Tcc                    || j         z
  }| j        D ]}|dk     r n|dk    r|c S ||j        z  }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=)rD   r8   	grid_span
ValueError)rA   rE   remaining_offsettcs       r2   tc_at_grid_offsetzCT_Row.tc_at_grid_offsetO   sp     ')99+ 		- 		-B!##1$$			 ,H;HHIIIr1   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)rA   tbls     r2   tr_idxzCT_Row.tr_idxd   s3     64>>++,,z%%%r1   r)   c                &    | j         }|dS |j        S )zFThe value of `./w:trPr/w:trHeight/@w:hRule`, or |None| if not present.N)r:   trHeight_hRuler@   s     r2   rU   zCT_Row.trHeight_hRulej   s     y<4""r1   valuec                <    |                                  }||_        d S N)r6   rU   rA   rV   r:   s      r2   rU   zCT_Row.trHeight_hRuler   s!    ##%%#r1   c                &    | j         }|dS |j        S )zHReturn the value of `w:trPr/w:trHeight@w:val`, or |None| if not present.N)r:   trHeight_valr@   s     r2   r[   zCT_Row.trHeight_valw   s     y<4  r1   r'   c                <    |                                  }||_        d S rX   )r6   r[   rY   s      r2   r[   zCT_Row.trHeight_val   s!    ##%%!r1   
CT_TblPrExc                2    |                      d|           d S Nr   insert)rA   r9   s     r2   _insert_tblPrExzCT_Row._insert_tblPrEx   s    Awr1   CT_TrPrc                r    | j         }||                    |           d S |                     d|           d S r_   )r9   addnextra   )rA   r:   r9   s      r2   _insert_trPrzCT_Row._insert_trPr   sA    ,OOD!!!!!KK4     r1   c                4    t                                           S rX   )rF   newrA   s    r2   _new_tczCT_Row._new_tc   s    yy{{r1   Nr;   r<   )rE   r<   r;   rF   r;   r)   rV   r)   rV   r'   )r9   r]   )r:   rc   )r+   r,   r-   r.   r/   r   r9   r:   r   rK   propertyr?   rD   rL   rS   rU   setterr[   rb   rf   rj   r0   r1   r2   r4   r4   1   s        ****$$$$!*;!7!7G7777$9X..D....	F		B   X       X J J J J* & & & X&
 # # # X# $ $ $ $ ! ! X! " " " "       ! ! ! !    r1   r4   c                     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            Zej        d*d            Ze
d             Zd Zed+d            Ze
d,d            Zej        d-d            Zed.d!            Zed/d$            Zed0d&            Zed/d'            Zd(S )1rN   z``<w:tbl>`` element.zCallable[[], CT_Row]add_trzlist[CT_Row]rP   zw:tblPrCT_TblPrtblPrz	w:tblGrid
CT_TblGridtblGridzw:trr;   bool | Nonec                0    | j         j        }|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)rt   
bidiVisualr(   )rA   ry   s     r2   bidiVisual_valzCT_Tbl.bidiVisual_val   s!     Z*
4~r1   rV   WD_TABLE_DIRECTION | Nonec                    | j         }||                                 d S t          |          |                                _        d S rX   )rt   _remove_bidiVisualboolget_or_add_bidiVisualr(   )rA   rV   rt   s      r2   rz   zCT_Tbl.bidiVisual_val   sF    
=$$&&&&&04UE''))---r1   c                4    t          | j        j                  S )z)The number of grid columns in this table.)lenrv   gridCol_lstri   s    r2   	col_countzCT_Tbl.col_count   s     4<+,,,r1   c              #  :   K   | j         D ]}|j        D ]}|V  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)rP   r8   )rA   trrK   s      r2   iter_tcszCT_Tbl.iter_tcs   sD       + 	 	Bi  	 	r1   rowsr<   colswidthr   c           
     p    t          t          t          |                     |||                              S )zReturn a new `w:tbl` element having `rows` rows and `cols` columns.

        `width` is distributed evenly between the columns.
        )r   rN   r   _tbl_xml)clsr   r   r   s       r2   new_tblzCT_Tbl.new_tbl   s,     FIcll4u&E&EFFGGGr1   
str | Nonec                0    | j         j        }|dS |j        S )zH`w:tblPr/w:tblStyle/@w:val` (a table style id) or |None| if not present.N)rt   tblStyler(   rA   r   s     r2   tblStyle_valzCT_Tbl.tblStyle_val   s!     :&4|r1   styleIdNonec                v    | j         }|                                 |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)rt   _remove_tblStyle_add_tblStyler(   )rA   r   rt   s      r2   r   zCT_Tbl.tblStyle_val   sA     
   ?F$+!!!r1   strc           	         |dk    rt          ||z            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_widths        r2   r   zCT_Tbl._tbl_xml   s    *.((C&&&A		gcll 	 	 i00	 ||D$	22	 	 		
r1   r   r   c                R    d}t          |          D ]}|d|j        z  z  }|dz  }|S )Nz  <w:tblGrid>
z    <w:gridCol w:w="%d"/>
z  </w:tblGrid>
)rangetwips)r   r   r   xml_s        r2   r   zCT_Tbl._tblGrid_xml   sD    y!! 	C 	CA09?BBCC!!
r1   	row_countc                <    d|                      ||           d|z  S )Nz	  <w:tr>
z
  </w:tr>
)_tcs_xml)r   r   r   r   s       r2   r   zCT_Tbl._trs_xml   s&    KCLLI>>KKKiWWr1   c                    d|j          d|z  S )Nz;    <w:tc>
      <w:tcPr>
        <w:tcW w:type="dxa" w:w="z-"/>
      </w:tcPr>
      <w:p/>
    </w:tc>
)r   )r   r   r   s      r2   r   zCT_Tbl._tcs_xml   s*    09    	r1   N)r;   rw   )rV   r{   )r   r<   r   r<   r   r   r;   rN   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/   r   rt   rv   r   r   ro   rz   rp   r   r   classmethodr   r   r   r   r   r   r0   r1   r2   rN   rN      s            #mI..E....'-44G4444	F		B   X < < < < - - X-	 	 	 H H H [H    X 	, 	, 	, 	, 
 
 
 [
    [ X X X [X    [  r1   rN   c                  B    e Zd ZU dZded<   ded<    edd          Zd	S )
ru   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]r   z	w:gridCol)zw:tblGridChange
successorsN)r+   r,   r-   r.   r/   r   gridColr0   r1   r2   ru   ru     sI          
 -,,,$$$$j1EFFFGGGr1   ru   c                  N    e Zd ZU dZ ede          Zded<   ed	d            Z	dS )
CT_TblGridColzB`w:gridCol` element, child of `w:tblGrid`, defines a table column.w:wr'   r   r;   r<   c                    t          t          |                                           }|j                            |           S )zHIndex of this `w:gridCol` element within its parent `w:tblGrid` element.)r   ru   rO   r   rQ   )rA   rv   s     r2   gridCol_idxzCT_TblGridCol.gridCol_idx  s4     z4>>#3#344"((...r1   Nrk   )
r+   r,   r-   r.   r   r   r   r/   ro   r   r0   r1   r2   r   r     sg         LL(( A     / / / X/ / /r1   r   c                  6    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/   r0   r1   r2   r   r     sN           )(" D      r1   r   c                     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                   Zded<    ededd                   Zded<    ededd                   Z	ded<    ededd                   Z
ded <   [ed,d#            Zej        d-d%            Zed.d'            Zej        d/d(            Zed)             Zej        d0d+            ZdS )1rs   z}``<w:tblPr>`` element, child of ``<w:tbl>``, holds child elements that define
    table properties such as style and borders.zCallable[[], CT_OnOff]r   zCallable[[], CT_Jc]get_or_add_jczCallable[[], CT_TblLayoutType]get_or_add_tblLayoutzCallable[[], CT_String]r   Callable[[], None]r}   
_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 | Nonery   r      zCT_Jc | Nonejcr      zCT_TblLayoutType | None	tblLayoutr;   WD_TABLE_ALIGNMENT | Nonec                B    | j         }|dS t          d|j                  S )zAHorizontal alignment of table, |None| if `./w:jc` is not present.Nr   )r   r   r(   )rA   r   s     r2   	alignmentzCT_TblPr.alignmentW  s(     W:4/888r1   rV   c                    |                                   |d S |                                 }t          d|          |_        d S )Nr    )r   r   r   r(   )rA   rV   r   s      r2   r   zCT_TblPr.alignment_  sC    =F!!*E22r1   r~   c                .    | j         }|dn
|j        dk    S )zg|False| when there is a `w:tblLayout` child with `@w:type="fixed"`.

        Otherwise |True|.
        NTfixed)r   r   )rA   r   s     r2   autofitzCT_TblPr.autofitg  s!     N	 (ttin.GGr1   c                D    |                                  }|rdnd|_        d S )Nr   r   )r   r   )rA   rV   r   s      r2   r   zCT_TblPr.autofitp  s'    --//	&+8	r1   c                &    | j         }|dS |j        S )ziReturn the value of the ``val`` attribute of the ``<w:tblStyle>`` child or
        |None| if not present.N)r   r(   r   s     r2   stylezCT_TblPr.styleu  s     =4|r1   r   c                h    |                                   |d S ||                                 _        d S rX   )r   r   r(   rA   rV   s     r2   r   zCT_TblPr.style~  s8    =F#(   r1   )r;   r   )rV   r   r;   r~   )rV   r~   rV   r   )r+   r,   r-   r.   r/   _tag_seqr   r   ry   r   r   ro   r   rp   r   r   r0   r1   r2   rs   rs   )  s+        3 3 2111&&&&8888********""""((((H( "+!""" " "H     #,)8ABB<# # #J     !y8ABB<  B     *3(233-* * *I     	9 9 9 X9 3 3 3 3 H H H XH ^9 9 9 ^9   X \) ) ) \) ) )r1   rs   c                      e Zd ZdZdS )r]   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.   r0   r1   r2   r]   r]     s           r1   r]   c                      e Zd ZU dZ ede          Zded<    ede          Z	e
dd            Zej        dd            ZdS )CT_TblWidthzJUsed for `w:tblW` and `w:tcW` and others, specifies a table-related width.r   r<   r   r   r;   r'   c                D    | 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   ri   s    r2   r   zCT_TblWidth.width  s$     94TV}}r1   rV   r   c                F    d| _         t          |          j        | _        d S )Nr   )r   r   r   r   r   s     r2   r   zCT_TblWidth.width  s    	U!r1   Nr;   r'   rV   r   )r+   r,   r-   r.   r   r   r   r/   r   r   ro   r   rp   r0   r1   r2   r   r     s         TT
 uf--A----X{33D   X \" " " \" " "r1   r   c                  ,   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            Zd Ze
dKd            Ze
dKd            Zej        dLd            Ze
dMd            Zd Ze
dKd            ZdNdZedOd             Ze
dKd!            Ze
dKd"            Ze
dPd$            Zej        dQd%            Ze
dRd'            Zej        dSd)            ZdTd*ZdUdVd0ZdWd2Ze
dXd4            ZdTd5ZdYd7Ze
dZd8            Z d9 Z!d: Z"d[d<Z#d\d?Z$d]d@Z%e
d^dB            Z&e
dOdC            Z'e
dZdD            Z(e
d_dF            Z)e
d_dG            Z*e
d`dI            Z+e
dKdJ            Z,d+S )arF   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:tblr;   r<   c                n    | j         %| j        }||j         t          j        k    r|j        S | j        dz   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)rA   tc_belows     r2   r   zCT_Tc.bottom  s=     ;"~H#8;L(L(L&|ar1   c                `    |                      d          D ]}|                     |           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)rA   es     r2   clear_contentzCT_Tc.clear_content  s;     455 	 	AKKNNNN	 	r1   c                |    | j         j        }t          d |                     d          D                       }||z   S )zStarting offset of `tc` in the layout-grid columns of its table.

        A cell in the leftmost grid-column has offset 0.
        c              3  $   K   | ]}|j         V  d S rX   )rH   ).0rK   s     r2   	<genexpr>z$CT_Tc.grid_offset.<locals>.<genexpr>  s5       &
 &
BL&
 &
 &
 &
 &
 &
r1   z./preceding-sibling::w:tc)_trrD   sumr   )rA   rD   preceding_tc_grid_spanss      r2   rE   zCT_Tc.grid_offset  sU     h*"% &
 &
#'::.I#J#J&
 &
 &
 #
 #
 444r1   c                &    | j         }|dn|j        S )zzThe integer number of columns this cell spans.

        Determined by ./w:tcPr/w:gridSpan/@val, it defaults to 1.
        Nr   )r   rH   rA   r   s     r2   rH   zCT_Tc.grid_span  s     yLqqdn4r1   rV   c                <    |                                  }||_        d S rX   )r   rH   rA   rV   r   s      r2   rH   zCT_Tc.grid_span  s    ##%%r1   list[CT_P | CT_Tbl]c                ,    |                      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   ri   s    r2   inner_content_elementszCT_Tc.inner_content_elements  s     zz+,,,r1   c              #     K   t          d          t          d          t          d          f}| D ]}|j        |v r|V  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)rA   block_item_tagschilds      r2   iter_block_itemszCT_Tc.iter_block_items  sV       e99bkk2g;;? 	 	EyO++	 	r1   c                    | j         S )z?The grid column index at which this ``<w:tc>`` element appears.)rE   ri   s    r2   leftz
CT_Tc.left  s     r1   other_tcc                    |                      |          \  }}}}| 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_tblrP   rL   _grow_to)rA   r  topr
  heightr   top_tcs          r2   mergezCT_Tc.merge  sV     $(#8#8#B#B T65!#&88>>v&&&r1   c           	     f    t          t          t          dt          d          z                      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   rF   r   r   )r   s    r2   rh   z	CT_Tc.new  s(     E9%=%LMMNNNr1   c                     | j         | j        z   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.
        )rE   rH   ri   s    r2   rightzCT_Tc.right  s     $.00r1   c                `    | j         | j         t          j        k    r| j        S | j        j        S )z9The top-most row index in the vertical span of this cell.)r   r   RESTARTr   	_tc_abover  ri   s    r2   r  z	CT_Tc.top  s.     ;$+1A"A"A<~!!r1   r   c                &    | j         }|dS |j        S )zCValue of ./w:tcPr/w:vMerge/@val, |None| if w:vMerge is not present.N)r   
vMerge_valr   s     r2   r   zCT_Tc.vMerge  s     y<4r1   c                <    |                                  }||_        d S rX   )r   r  r   s      r2   r   zCT_Tc.vMerge%  s    ##%%r1   r'   c                &    | j         }|dS |j        S )zDEMU length represented in `./w:tcPr/w:tcW` or |None| if not present.N)r   r   r   s     r2   r   zCT_Tc.width*  s     y<4zr1   r   c                <    |                                  }||_        d S rX   )r   r   r   s      r2   r   zCT_Tc.width2  s    ##%%


r1   c                l    | j         r*|j         r%t          | j         |j         z             | _         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   )rA   r  s     r2   _add_width_ofzCT_Tc._add_width_of7  sG    
 : 	=(. 	=
X^ ;<<DJJJ	= 	= 	= 	=r1   Nr   r  r  CT_Tc | Nonec                     d fd}| n|}                      || ||                     dk    r' j        }|J |                    |dz
  |           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  rF   c                L    | urt           j        ndk    rd nt           j        S Nr   )r   r   r  )r  r  rA   s    r2   r  z"CT_Tc._grow_to.<locals>.vMerge_valF  s8     %% !! Q;; T%r1   Nr   )r  rF   )_span_to_widthr   r  )rA   r   r  r  r  r   s   ` `   r2   r  zCT_Tc._grow_to?  s    	 	 	 	 	 	 	  VE6::f+=+=>>>A::~H'''eVaZ88888 :r1   CT_TcPrc                2    |                      d|           |S )z#Override default `._insert_tcPr()`.r   r`   r   s     r2   _insert_tcPrzCT_Tc._insert_tcPrV  s     	Atr1   r~   c                    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  r   
isinstancer   r_lst)rA   block_items	only_items      r2   	_is_emptyzCT_Tc._is_empty]  sd     4002233{a5  N	)T**Hs9?/C/Cq/HHr1   c                    || u rdS | j         rdS |                                 |                                 D ]}|                    |           |                     |                                            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   )rA   r  block_elements      r2   _move_content_tozCT_Tc._move_content_toh  s    
 tF> 	F))+++!2244 	+ 	+MOOM****DKKMM"""""r1   r   c                     t          d          )NzDuse CT_Tbl.new_tbl() to add a new table, specifying rows and columns)NotImplementedErrorri   s    r2   _new_tblzCT_Tc._new_tblx  s    !R
 
 	
r1   c                D    |                      d          }|r|d         n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  )rA   following_tcss     r2   _next_tczCT_Tc._next_tc}  s+     

#>??#0:}Qd:r1   c                `    |                                  }|J |                    |            dS )z-Remove this `w:tc` element from the XML tree.N)rO   r   )rA   parent_elements     r2   _removezCT_Tc._remove  s7    )))))d#####r1   c                    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   r   r,  r   )rA   r-  last_content_elmps       r2   r1  zCT_Tc._remove_trailing_empty_p  sm    4002233&r?*D11 	Fqw<<!FAr1   tuple[int, int, int, int]c                *   dd}dd} || |            || |           t          | j        |j                  }t          | j        |j                  }t          | j        |j                  }t          | j        |j                  }||||z
  ||z
  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.arF   bc                    | 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  )rD  rE  s     r2   raise_on_inverted_Lz3CT_Tc._span_dimensions.<locals>.raise_on_inverted_L  sf    u~~!(ah"6"6&'GHHHvAGqw$6$6&'GHHH  $6$6r1   c                2   | 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    rt          d          d S d S rG  rH  )rD  rE  top_mostother	left_mosts        r2   raise_on_tee_shapedz3CT_Tc._span_dimensions.<locals>.raise_on_tee_shaped  s    ()q!ffAq6OHe|ei''HOel,J,J&'GHHH)*!&1vvq!fIu~
**y/L/L&'GHHH +*/L/Lr1   )rD  rF   rE  rF   )minr  r
  maxr   r  )rA   r  rI  rN  r  r
  r   r  s           r2   r  zCT_Tc._span_dimensions  s    
	I 	I 	I 	I	I 	I 	I 	I 	D(+++D(+++$(HL))49hm,,T[(/22DJ//D&3,44r1   
grid_widthr   c                    |                      |           | j        |k     r!|                     ||           | j        |k     !|| _        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)r4  rH   _swallow_next_tcr   )rA   rQ  r  r   s       r2   r%  zCT_Tc._span_to_width  sU     	f%%%nz))!!*f555 nz))r1   c                     d fd} j         } ||           |J |                    |                                |            xj        |j        z  c_        |                                 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                n    | t          d          j        | j        z   k    rt          d          d S )Nznot enough grid columnszspan is not rectangular)r
   rH   )rU  rQ  rA   s    r2   raise_on_invalid_swallowz8CT_Tc._swallow_next_tc.<locals>.raise_on_invalid_swallow  sC    &'@AAA~ 11J>>&'@AAA ?>r1   N)rU  r!  )r:  r4  r   rH   r=  )rA   rQ  r  rW  rU  s   ``   r2   rS  zCT_Tc._swallow_next_tc  s    	B 	B 	B 	B 	B 	B 	B -  )))"""  (((7###'++r1   rN   c                ^    t          t          |                     d          d                   S )z+The tbl element this tc element appears in.z./ancestor::w:tbl[position()=1]r   )r   rN   r   ri   s    r2   r  z
CT_Tc._tbl  s&     FDJJ'HII!LMMMr1   c                @    | j                             | j                  S )zAThe `w:tc` element immediately above this one in its grid column.)	_tr_aboverL   rE   ri   s    r2   r  zCT_Tc._tc_above  s     ~//0@AAAr1   c                L    | j         }|dS |                    | j                  S )z=The tc element immediately below this one in its grid column.N)	_tr_belowrL   rE   )rA   tr_belows     r2   r   zCT_Tc._tc_below  s-     >4))$*:;;;r1   r4   c                ^    t          t          |                     d          d                   S )z*The tr element this tc element appears in.z./ancestor::w:tr[position()=1]r   )r   r4   r   ri   s    r2   r   z	CT_Tc._tr  s&     FDJJ'GHHKLLLr1   c                ^    |                      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   rI   )rA   	tr_abovess     r2   rZ  zCT_Tc._tr_above  s8     JJZ[[	 	@>???|r1   CT_Row | Nonec                    | j         j        }|                    | j                  }	 ||dz            S # t          $ r Y dS w xY 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  rP   rQ   r   
IndexError)rA   rP   rS   s      r2   r\  zCT_Tc._tr_below  sW     !dh''	&1*%% 	 	 	44	s   
3 
A Ac                J    | j         j                            | j                  S )z;The row index of the tr element this tc element appears in.)r  rP   rQ   r   ri   s    r2   r   zCT_Tc._tr_idx
  s     y%%dh///r1   rk   rV   r<   )r;   r   )r  rF   r;   rF   )r;   rF   r   r   r   r   )r  rF   rX   )r   r<   r  r<   r  r!  )r   r&  r;   r&  r   )r;   r   )r;   r!  )r  rF   r;   rB  )rQ  r<   r  rF   r   r   )rQ  r<   r  rF   )r;   rN   )r;   r4   )r;   ra  )-r+   r,   r-   r.   r/   r   r   r   rA  rR   ro   r   r   rE   rH   rp   r  r  r
  r  r   rh   r  r  r   r   r   r  r(  r/  r4  r7  r:  r=  r1  r  r%  rS  r  r  r   r   rZ  r\  r   r0   r1   r2   rF   rF     sn        $$****++++ %9X..D....	%A
)G

C
  
  
  X
 	 	 	 	5 	5 	5 X	5 5 5 5 X5     - - - X-         X 	 	 	 	 O O O [O 1 1 1 X1 " " " X"    X ]      ]     X \   \= = = =9 9 9 9 9.    I I I XI# # # # 
 
 
 

 ; ; ; X;$ $ $	 	 	5 5 5 5<   "   2 N N N XN B B B XB < < < X< M M M XM    X    X 0 0 0 X0 0 0r1   rF   c                     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                   Zded<    ededd                   Zded<    ededd                   Z	ded<    ededd                   Z
ded <   [ed0d#            Zej        d1d%            Zed&             Zej        d2d(            Zed)             Zej        d3d+            Zed4d-            Zej        d5d/            ZdS )6r&  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:tcPrChangero     Nr   zCT_TblWidth | NonetcWrp     CT_DecimalNumber | NonegridSpanrq     zCT_VMerge | Noner   rr     zCT_VerticalJc | NonevAlignr;   r<   c                &    | j         }|dn|j        S )zsThe integer number of columns this cell spans.

        Determined by ./w:gridSpan/@val, it defaults to 1.
        Nr   )rw  r(   )rA   rw  s     r2   rH   zCT_TcPr.grid_span=  s     =$qq(,6r1   rV   c                p    |                                   |dk    r||                                 _        d S d S r$  )rk  rg  r(   r   s     r2   rH   zCT_TcPr.grid_spanF  s>    199-2D$$&&*** 9r1   c                &    | j         }|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)rz  r(   )rA   rz  s     r2   
vAlign_valzCT_TcPr.vAlign_valL  s     >4zr1   !WD_CELL_VERTICAL_ALIGNMENT | Nonec                h    ||                                   d S ||                                 _        d S rX   )rl  ri  r(   r   s     r2   r~  zCT_TcPr.vAlign_valX  s8    =!!!F',  $$$r1   c                &    | j         }|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(   )rA   r   s     r2   r  zCT_TcPr.vMerge_val_  s     >4zr1   r   c                h    |                                   |||                                 _        d S d S rX   )rm  rj  r(   r   s     r2   r  zCT_TcPr.vMerge_valh  s<    %*D""" r1   r'   c                &    | j         }|dS |j        S )zJEMU length in `./w:tcW` or |None| if not present or its type is not 'dxa'.N)rt  r   )rA   rt  s     r2   r   zCT_TcPr.widthn  s     h;4yr1   r   c                <    |                                  }||_        d S rX   )rh  r   )rA   rV   rt  s      r2   r   zCT_TcPr.widthv  s    !!##			r1   rk   re  )rV   r  r   r   r   )r+   r,   r-   r.   r/   r   r   rt  rw  r   rz  ro   rH   rp   r~  r  r   r0   r1   r2   r&  r&    sb        ??7777----2222((((((((&&&&&&&&H( (iHQRRL  C     )2	!"") ) )H      )yx|     F     $-9x}$ $ $F     	7 7 7 X7 3 3 3 3
 	 	 X	 - - - -   X + + + +
    X \   \  r1   r&  c                  b   e Zd ZU dZded<   dZ ededd                   Zd	ed
<    ededd                   Zd	ed<    ededd                   Z	ded<   [e
dd            Ze
dd            Ze
dd            Zej        dd            Ze
d             Zej        d d            ZdS )!rc   z4``<w:trPr>`` element, defining table row properties.zCallable[[], CT_Height]get_or_add_trHeight)rn  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   rv  	gridAfterr  ru  
gridBeforer  r   zCT_Height | NonetrHeightr;   r<   c                &    | j         }|dn|j        S r>   )r  r(   )rA   r  s     r2   r?   zCT_TrPr.grid_after  s     N	%qq9=8r1   c                &    | j         }|dn|j        S rC   )r  r(   )rA   r  s     r2   rD   zCT_TrPr.grid_before  s     _
&qqJN:r1   r)   c                &    | j         }|dn|j        S )zCReturn the value of `w:trHeight@w:hRule`, or |None| if not present.N)r  r*   rA   r  s     r2   rU   zCT_TrPr.trHeight_hRule  s     ='ttX^;r1   rV   c                R    |	| j         d S |                                 }||_        d S rX   )r  r  r*   rA   rV   r  s      r2   rU   zCT_TrPr.trHeight_hRule  s/    =T]2F++--r1   c                &    | j         }|dn|j        S )zAReturn the value of `w:trHeight@w:val`, or |None| if not present.N)r  r(   r  s     r2   r[   zCT_TrPr.trHeight_val  s     ='ttX\9r1   r'   c                R    |	| j         d S |                                 }||_        d S rX   )r  r  r(   r  s      r2   r[   zCT_TrPr.trHeight_val  s/    =T]2F++--r1   rk   rl   rm   rn   )r+   r,   r-   r.   r/   r   r   r  r  r  ro   r?   rD   rU   rp   r[   r0   r1   r2   rc   rc   |  s        >>0000H" *3(122,* * *I     +4)8ABB<+ + +J     "+!""" " "H     	9 9 9 X9
 ; ; ; X;
 < < < X<
     : : X:
      r1   rc   c                  6    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/   r0   r1   r2   r  r    sH         DD&7&7+' 'C      r1   r  c                  D    e Zd ZU dZ edeej                  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/   r0   r1   r2   r  r    sQ         QQ''8#4  C      r1   r  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%   r4   rN   ru   r   r   rs   r]   r   rF   r&  rc   r  r  r0   r1   r2   <module>r     se   ( ( " " " " " " 0 0 0 0 0 0 0 0 0 0 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ , , , , , , $ $ $ $ $ $ $ $ & & & & & & - - - - - -              * ) ) ) ) )                  + * * * * * * * * * ,22222211111144444444++++++       ^ ^ ^ ^ ^_ ^ ^ ^Bn n n n n_ n n nb	G 	G 	G 	G 	G 	G 	G 	G/ / / / /O / / /	 	 	 	 	 	 	 	Z) Z) Z) Z) Z) Z) Z) Z)z       " " " " "/ " " ",h0 h0 h0 h0 h0O h0 h0 h0Vi i i i io i i iXE E E E Eo E E EP    O            r1   