U
    cqe7                     @  s  d Z ddlmZ ddlmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZmZmZ erddlmZ dd	lmZmZ dd
lmZmZ ddlmZ ddlmZ G dd deZG dd de	ZG dd deZ G dd deZ!G dd deZ"G dd deZ#dS )z-The |Table| object and related proxy classes.    )annotations)TYPE_CHECKINGListTupleoverload)BlockItemContainer)WD_STYLE_TYPE)ST_Merge)InchesParentedlazyproperty)types)WD_TABLE_ALIGNMENTWD_TABLE_DIRECTION)CT_TblCT_TblPr)Length)_TableStylec                      sb  e Zd ZdZddd fddZddd	d
Zdd ZeddddZej	ddddZeddddZ
e
j	ddddZ
ddddddZdddddZed d! Zddd"d#d$Zed%dd&d'Zed(dd)d*Zej	d(d+d,d*Zed-d. Zed/dd0d1Zej	d/dd2d1Zeddd3d4Zed5d6 Zed7dd8d9Z  ZS ):Tablez7Proxy class for a WordprocessingML ``<w:tbl>`` element.r   zt.StoryChild)tblparentc                   s    t t| | | | _| _d S N)superr   __init___element_tblselfr   r   	__class__ ./tmp/pip-unpacked-wheel-gx5hpqp3/docx/table.pyr      s    zTable.__init__r   )widthc                 C  s<   | j j}| }||_| j jD ]}| }||_qt|| S )zIReturn a |_Column| object of `width`, newly added rightmost to the table.)r   tblGridZadd_gridColwtr_lstadd_tcr"   _Column)r   r"   r#   gridColtrtcr    r    r!   
add_column   s    zTable.add_columnc                 C  s6   | j }| }|jjD ]}| }|j|_qt|| S )z?Return a |_Row| instance, newly added bottom-most to the table.)r   Zadd_trr#   gridCol_lstr&   r$   r"   _Row)r   r   r)   r(   r*   r    r    r!   add_row%   s    
zTable.add_rowzWD_TABLE_ALIGNMENT | Nonereturnc                 C  s   | j jS )a  Read/write.

        A member of :ref:`WdRowAlignment` or None, specifying the positioning of this
        table between the page margins. |None| if no setting is specified, causing the
        effective value to be inherited from the style hierarchy.
        _tblPr	alignmentr   r    r    r!   r3   .   s    zTable.alignment)valuec                 C  s   || j _d S r   r1   r   r5   r    r    r!   r3   8   s    boolc                 C  s   | j jS )a  |True| if column widths can be automatically adjusted to improve the fit of
        cell contents.

        |False| if table layout is fixed. Column widths are adjusted in either case if
        total column width exceeds page width. Read/write boolean.
        r2   autofitr4   r    r    r!   r9   <   s    zTable.autofitc                 C  s   || j _d S r   r8   r6   r    r    r!   r9   F   s    int_Cell)row_idxcol_idxr0   c                 C  s   ||| j   }| j| S )zb|_Cell| at `row_idx`, `col_idx` intersection.

        (0, 0) is the top, left-most cell.
        _column_count_cells)r   r<   r=   Zcell_idxr    r    r!   cellJ   s    z
Table.cellzList[_Cell])
column_idxr0   c                   s*   | j  t|t | j} fdd|D S )z>Sequence of cells in the column at `column_idx` in this table.c                   s   g | ]} | qS r    r    ).0idxcellsr    r!   
<listcomp>V   s     z&Table.column_cells.<locals>.<listcomp>)r@   rangelenr?   )r   rB   Zidxsr    rE   r!   column_cellsR   s    zTable.column_cellsc                 C  s   t | j| S )zG|_Columns| instance representing the sequence of columns in this table.)_Columnsr   r4   r    r    r!   columnsX   s    zTable.columns)r<   r0   c                 C  s$   | j }|| }|| }| j|| S )z8Sequence of cells in the row at `row_idx` in this table.r>   )r   r<   Zcolumn_countstartendr    r    r!   	row_cells]   s    zTable.row_cells_Rowsc                 C  s   t | j| S )z?|_Rows| instance containing the sequence of rows in this table.)rP   r   r4   r    r    r!   rowsd   s    z
Table.rowsz_TableStyle | Nonec                 C  s   | j j}| j|tjS )am  |_TableStyle| object representing the style applied to this table.

        Read/write. The default table style for the document (often `Normal Table`) is
        returned if the table has no directly-applied style. Assigning |None| to this
        property removes any directly-applied table style causing it to inherit the
        default table style of the document.

        Note that the style name of a table style differs slightly from that displayed
        in the user interface; a hyphen, if it appears, must be removed. For example,
        `Light Shading - Accent 1` becomes `Light Shading Accent 1`.
        )r   tblStyle_valpartZ	get_styler   TABLE)r   style_idr    r    r!   stylei   s    zTable.style)style_or_namec                 C  s   | j |tj}|| j_d S r   )rS   Zget_style_idr   rT   r   rR   )r   rW   rU   r    r    r!   rV   y   s    c                 C  s   | S )a  Provide child objects with reference to the |Table| object they belong to,
        without them having to know their direct parent is a |Table| object.

        This is the terminus of a series of `parent._table` calls from an arbitrary
        child through its ancestors.
        r    r4   r    r    r!   table~   s    zTable.tablezWD_TABLE_DIRECTION | Nonec                 C  s   | j jS )zMember of :ref:`WdTableDirection` indicating cell-ordering direction.

        For example: `WD_TABLE_DIRECTION.LTR`. |None| indicates the value is inherited
        from the style hierarchy.
        r   ZbidiVisual_valr4   r    r    r!   table_direction   s    zTable.table_directionc                 C  s   || j _d S r   rY   r6   r    r    r!   rZ      s    c                 C  st   | j }g }| j D ]Z}t|jD ]J}|jtjkrD|||   q"|dkr\||d  q"|t	||  q"q|S )zA sequence of |_Cell| objects, one for each cell of the layout grid.

        If the table contains a span, one or more |_Cell| object references are
        repeated.
        r   )
r?   r   Ziter_tcsrH   Z	grid_spanZvMerger	   CONTINUEappendr;   )r   	col_countrF   r*   Zgrid_span_idxr    r    r!   r@      s    zTable._cellsc                 C  s   | j jS )z)The number of grid columns in this table.)r   r^   r4   r    r    r!   r?      s    zTable._column_countr   c                 C  s   | j jS r   )r   ZtblPrr4   r    r    r!   r2      s    zTable._tblPr)__name__
__module____qualname____doc__r   r+   r.   propertyr3   setterr9   rA   rJ   r   rL   rO   rQ   rV   rX   rZ   r@   r?   r2   __classcell__r    r    r   r!   r      sF   
			

	
r   c                      s   e Zd ZdZ fddZd fdd	Z fdd	Zd
d Ze fddZ	e fddZ
eddddZejdd Zedd Zejdd Zedd Zejdd Z  ZS )r;   zTable cell.c                   s"   t t| || | | _| _d S r   )r   r;   r   _tcr   )r   r*   r   r   r    r!   r      s    z_Cell.__init__ Nc                   s   t t| ||S )a  Return a paragraph newly added to the end of the content in this cell.

        If present, `text` is added to the paragraph in a single run. If specified, the
        paragraph style `style` is applied. If `style` is not specified or is |None|,
        the result is as though the 'Normal' style was applied. Note that the formatting
        of text in a cell can be influenced by the table style. `text` can contain tab
        (``\t``) characters, which are converted to the appropriate XML form for a tab.
        `text` can also include newline (``\n``) or carriage return (``\r``)
        characters, each of which is converted to a line break.
        )r   r;   add_paragraph)r   textrV   r   r    r!   rh      s    z_Cell.add_paragraphc                   s8   | j dk	r| j ntd}tt| |||}|   |S )a
  Return a table newly added to this cell after any existing cell content,
        having `rows` rows and `cols` columns.

        An empty paragraph is added after the table because Word requires a paragraph
        element as the last element in every cell.
        N   )r"   r
   r   r;   	add_tablerh   )r   rQ   colsr"   rX   r   r    r!   rk      s    z_Cell.add_tablec                 C  s$   | j |j  }}||}t|| jS )zReturn a merged cell created by spanning the rectangular region having this
        cell and `other_cell` as diagonal corners.

        Raises |InvalidSpanError| if the cells do not define a rectangular region.
        )rf   merger;   _parent)r   Z
other_cellr*   Ztc_2Z	merged_tcr    r    r!   rm      s    
z_Cell.mergec                   s   t t| jS )zList of paragraphs in the cell.

        A table cell is required to contain at least one block-level element and end
        with a paragraph. By default, a new cell contains a single paragraph. Read-only
        )r   r;   
paragraphsr4   r   r    r!   ro      s    z_Cell.paragraphsc                   s   t t| jS )zRList of tables in the cell, in the order they appear.

        Read-only.
        )r   r;   tablesr4   r   r    r!   rp      s    z_Cell.tablesstrr/   c                 C  s   d dd | jD S )zThe entire contents of this cell as a string of text.

        Assigning a string to this property replaces all existing content with a single
        paragraph containing the assigned text in a single run.
        
c                 s  s   | ]}|j V  qd S r   )ri   )rC   pr    r    r!   	<genexpr>   s     z_Cell.text.<locals>.<genexpr>)joinro   r4   r    r    r!   ri      s    z
_Cell.textc                 C  s(   | j }|  | }| }||_dS )zWrite-only.

        Set entire contents of cell to the string `text`. Any existing content or
        revisions are replaced.
        N)rf   clear_contentZadd_pZadd_rri   )r   ri   r*   rs   rr    r    r!   ri      s
    c                 C  s   | j j}|dkrdS |jS )a  Member of :ref:`WdCellVerticalAlignment` or None.

        A value of |None| indicates vertical alignment for this cell is inherited.
        Assigning |None| causes any explicitly defined vertical alignment to be removed,
        restoring inheritance.
        N)r   tcPr
vAlign_val)r   rx   r    r    r!   vertical_alignment  s    z_Cell.vertical_alignmentc                 C  s   | j  }||_d S r   )r   Zget_or_add_tcPrry   )r   r5   rx   r    r    r!   rz     s    
c                 C  s   | j jS )zEThe width of this cell in EMU, or |None| if no explicit width is set.rf   r"   r4   r    r    r!   r"     s    z_Cell.widthc                 C  s   || j _d S r   r{   r6   r    r    r!   r"     s    )rg   N)r_   r`   ra   rb   r   rh   rk   rm   rc   ro   rp   ri   rd   rz   r"   re   r    r    r   r!   r;      s*   




r;   c                      s^   e Zd ZdZ fddZedd Zedd Zedd	 Zej	d
d	 Zedd Z
  ZS )r'   zTable column.c                   s   t t| | || _d S r   )r   r'   r   _gridCol)r   r(   r   r   r    r!   r   "  s    z_Column.__init__c                 C  s   t | j| jS )zDSequence of |_Cell| instances corresponding to cells in this column.)tuplerX   rJ   _indexr4   r    r    r!   rF   &  s    z_Column.cellsc                 C  s   | j jS )z7Reference to the |Table| object this column belongs to.rn   rX   r4   r    r    r!   rX   +  s    z_Column.tablec                 C  s   | j jS )zGThe width of this column in EMU, or |None| if no explicit width is set.r|   r$   r4   r    r    r!   r"   0  s    z_Column.widthc                 C  s   || j _d S r   r   r6   r    r    r!   r"   5  s    c                 C  s   | j jS )z6Index of this column in its table, starting from zero.)r|   ZgridCol_idxr4   r    r    r!   r~   9  s    z_Column._index)r_   r`   ra   rb   r   rc   rF   rX   r"   rd   r~   re   r    r    r   r!   r'     s   



r'   c                      sP   e Zd ZdZ fddZdd Zdd Zdd	 Zed
d Z	edd Z
  ZS )rK   zSequence of |_Column| instances corresponding to the columns in a table.

    Supports ``len()``, iteration and indexed access.
    c                   s   t t| | || _d S r   )r   rK   r   r   r   r   r    r!   r   E  s    z_Columns.__init__c                 C  s>   z| j | }W n$ tk
r2   d| }t|Y nX t|| S )z*Provide indexed access, e.g. 'columns[0]'.z!column index [%d] is out of range)_gridCol_lst
IndexErrorr'   )r   rD   r(   msgr    r    r!   __getitem__I  s    z_Columns.__getitem__c                 c  s   | j D ]}t|| V  qd S r   )r   r'   )r   r(   r    r    r!   __iter__R  s    
z_Columns.__iter__c                 C  s
   t | jS r   )rI   r   r4   r    r    r!   __len__V  s    z_Columns.__len__c                 C  s   | j jS )zBReference to the |Table| object this column collection belongs to.r   r4   r    r    r!   rX   Y  s    z_Columns.tablec                 C  s   | j j}|jS )zfSequence containing ``<w:gridCol>`` elements for this table, each
        representing a table column.)r   r#   r,   )r   r#   r    r    r!   r   ^  s    z_Columns._gridCol_lst)r_   r`   ra   rb   r   r   r   r   rc   rX   r   re   r    r    r   r!   rK   ?  s   	
rK   c                      s~   e Zd ZdZ fddZeddddZedd	 Zejd
d	 Zedd Z	e	jdd Z	edd Z
edd Z  ZS )r-   z
Table row.c                   s    t t| | | | _| _d S r   )r   r-   r   _trr   )r   r)   r   r   r    r!   r   i  s    z_Row.__init__zTuple[_Cell]r/   c                 C  s   t | j| jS )zASequence of |_Cell| instances corresponding to cells in this row.)r}   rX   rO   r~   r4   r    r    r!   rF   m  s    z
_Row.cellsc                 C  s   | j jS )znReturn a |Length| object representing the height of this cell, or |None| if
        no explicit height is set.r   ZtrHeight_valr4   r    r    r!   heightr  s    z_Row.heightc                 C  s   || j _d S r   r   r6   r    r    r!   r   x  s    c                 C  s   | j jS )zReturn the height rule of this cell as a member of the :ref:`WdRowHeightRule`
        enumeration, or |None| if no explicit height_rule is set.r   ZtrHeight_hRuler4   r    r    r!   height_rule|  s    z_Row.height_rulec                 C  s   || j _d S r   r   r6   r    r    r!   r     s    c                 C  s   | j jS )z4Reference to the |Table| object this row belongs to.r   r4   r    r    r!   rX     s    z
_Row.tablec                 C  s   | j jS )z3Index of this row in its table, starting from zero.)r   Ztr_idxr4   r    r    r!   r~     s    z_Row._index)r_   r`   ra   rb   r   rc   rF   r   rd   r   rX   r~   re   r    r    r   r!   r-   f  s    




r-   c                      st   e Zd ZdZ fddZedddddZed	d
dddZdddddZdd Zdd Ze	dd Z
  ZS )rP   zSequence of |_Row| objects corresponding to the rows in a table.

    Supports ``len()``, iteration, indexed access, and slicing.
    c                   s   t t| | || _d S r   )r   rP   r   r   r   r   r    r!   r     s    z_Rows.__init__r:   r-   )rD   r0   c                 C  s   d S r   r    r   rD   r    r    r!   r     s    z_Rows.__getitem__slicez
List[_Row]c                 C  s   d S r   r    r   r    r    r!   r     s    zint | slicez_Row | List[_Row]c                 C  s   t | | S )z7Provide indexed access, (e.g. `rows[0]` or `rows[1:3]`))listr   r    r    r!   r     s    c                   s    fdd j jD S )Nc                 3  s   | ]}t | V  qd S r   )r-   )rC   r)   r4   r    r!   rt     s     z!_Rows.__iter__.<locals>.<genexpr>)r   r%   r4   r    r4   r!   r     s    z_Rows.__iter__c                 C  s   t | jjS r   )rI   r   r%   r4   r    r    r!   r     s    z_Rows.__len__c                 C  s   | j jS )z?Reference to the |Table| object this row collection belongs to.r   r4   r    r    r!   rX     s    z_Rows.table)r_   r`   ra   rb   r   r   r   r   r   rc   rX   re   r    r    r   r!   rP     s   rP   N)$rb   
__future__r   typingr   r   r   r   Zdocx.blkcntnrr   Zdocx.enum.styler   Zdocx.oxml.simpletypesr	   Zdocx.sharedr
   r   r   Zdocxr   tZdocx.enum.tabler   r   Zdocx.oxml.tabler   r   r   Zdocx.styles.styler   r   r;   r'   rK   r-   rP   r    r    r    r!   <module>   s&    m '+