o
    	'f7                     @  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rTd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                      s  e Zd ZdZd; fddZd<d
dZdd Zed=ddZej	d>ddZed?ddZ
e
j	d@ddZ
dAddZdBd d!Zed"d# ZdCd$d%ZedDd'd(ZedEd*d+Zej	dFd-d+Zed.d/ ZedGd1d2Zej	dHd3d2ZedId4d5Zed6d7 ZedJd9d:Z  ZS )KTablez7Proxy class for a WordprocessingML ``<w:tbl>`` element.tblr   parentt.StoryChildc                       t t| | | | _| _d S N)superr   __init___element_tblselfr   r   	__class__ B/home/ubuntu/flask/venv/lib/python3.10/site-packages/docx/table.pyr         zTable.__init__widthr   c                 C  s<   | j j}| }||_| j jD ]	}| }||_qt|| S )zIReturn a |_Column| object of `width`, newly added rightmost to the table.)r   tblGrid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   add_trr&   gridCol_lstr*   r(   r%   _Row)r   r   r-   r,   r.   r"   r"   r#   add_row%   s   

zTable.add_rowreturnWD_TABLE_ALIGNMENT | Nonec                 C     | 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#   r9   .      zTable.alignmentvaluec                 C     || j _d S r   r7   r   r<   r"   r"   r#   r9   8      boolc                 C  r6   )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.
        r8   autofitr:   r"   r"   r#   rB   <   r;   zTable.autofitc                 C  r=   r   rA   r>   r"   r"   r#   rB   F   r?   row_idxintcol_idx_Cellc                 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   rC   rE   cell_idxr"   r"   r#   cellJ   s   
z
Table.cell
column_idxList[_Cell]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>)rI   rangelenrH   )r   rL   idxsr"   rP   r#   column_cellsR   s   zTable.column_cellsc                 C     t | j| S )zG|_Columns| instance representing the sequence of columns in this table.)_Columnsr   r:   r"   r"   r#   columnsX      zTable.columnsc                 C  s$   | j }|| }|| }| j|| S )z8Sequence of cells in the row at `row_idx` in this table.rG   )r   rC   column_countstartendr"   r"   r#   	row_cells]   s   zTable.row_cells_Rowsc                 C  rW   )z?|_Rows| instance containing the sequence of rows in this table.)r_   r   r:   r"   r"   r#   rowsd   rZ   z
Table.rows_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part	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   )rc   get_style_idr   re   r   rb   )r   rh   rf   r"   r"   r#   rg   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"   r:   r"   r"   r#   table~   s   zTable.tableWD_TABLE_DIRECTION | Nonec                 C  r6   )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   bidiVisual_valr:   r"   r"   r#   table_direction   s   zTable.table_directionc                 C  r=   r   rl   r>   r"   r"   r#   rn      r?   c                 C  st   | j }g }| j D ]-}t|jD ]%}|jtjkr"|||   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   )
rH   r   iter_tcsrS   	grid_spanvMerger	   CONTINUEappendrF   )r   	col_countrQ   r.   grid_span_idxr"   r"   r#   rI      s   zTable._cellsc                 C  r6   )z)The number of grid columns in this table.)r   ru   r:   r"   r"   r#   rH         zTable._column_countr   c                 C  r6   r   )r   tblPrr:   r"   r"   r#   r8      s   zTable._tblPr)r   r   r   r   )r%   r   )r4   r5   )r<   r5   )r4   r@   )r<   r@   )rC   rD   rE   rD   r4   rF   )rL   rD   r4   rM   )rC   rD   r4   rM   )r4   r_   )r4   ra   )rh   ra   )r4   rk   )r<   rk   )r4   rM   )r4   r   )__name__
__module____qualname____doc__r   r/   r3   propertyr9   setterrB   rK   rV   r   rY   r^   r`   rg   rj   rn   rI   rH   r8   __classcell__r"   r"   r    r#   r      sH    

			




	
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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 )rF   zTable cell.c                   s"   t t| || | | _| _d S r   )r   rF   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   rF   add_paragraph)r   textrg   r    r"   r#   r      s   z_Cell.add_paragraphc                   s8   | j du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   rF   	add_tabler   )r   r`   colsr%   rj   r    r"   r#   r      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.
        )r   mergerF   _parent)r   
other_cellr.   tc_2	merged_tcr"   r"   r#   r      s   
z_Cell.mergec                      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   rF   
paragraphsr:   r    r"   r#   r      s   z_Cell.paragraphsc                   r   )zRList of tables in the cell, in the order they appear.

        Read-only.
        )r   rF   tablesr:   r    r"   r#   r      s   z_Cell.tablesr4   strc                 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   )r   )rN   pr"   r"   r#   	<genexpr>   s    z_Cell.text.<locals>.<genexpr>)joinr   r:   r"   r"   r#   r      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)r   clear_contentadd_padd_rr   )r   r   r.   r   rr"   r"   r#   r      s
   
c                 C  s   | j j}|du 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   r   r"   r"   r#   vertical_alignment  s   z_Cell.vertical_alignmentc                 C  s   | j  }||_d S r   )r   get_or_add_tcPrr   )r   r<   r   r"   r"   r#   r     s   

c                 C  r6   )zEThe width of this cell in EMU, or |None| if no explicit width is set.r   r%   r:   r"   r"   r#   r%     rw   z_Cell.widthc                 C  r=   r   r   r>   r"   r"   r#   r%     r?   )r   N)r4   r   )ry   rz   r{   r|   r   r   r   r   r}   r   r   r   r~   r   r%   r   r"   r"   r    r#   rF      s,    




rF   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                      t t| | || _d S r   )r   r+   r   _gridCol)r   r,   r   r    r"   r#   r   "     
z_Column.__init__c                 C     t | j| jS )zDSequence of |_Cell| instances corresponding to cells in this column.)tuplerj   rV   _indexr:   r"   r"   r#   rQ   &     z_Column.cellsc                 C  r6   )z7Reference to the |Table| object this column belongs to.r   rj   r:   r"   r"   r#   rj   +  rw   z_Column.tablec                 C  r6   )zGThe width of this column in EMU, or |None| if no explicit width is set.r   r(   r:   r"   r"   r#   r%   0  rw   z_Column.widthc                 C  r=   r   r   r>   r"   r"   r#   r%   5  r?   c                 C  r6   )z6Index of this column in its table, starting from zero.)r   gridCol_idxr:   r"   r"   r#   r   9  rw   z_Column._index)ry   rz   r{   r|   r   r}   rQ   rj   r%   r~   r   r   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 )rX   zSequence of |_Column| instances corresponding to the columns in a table.

    Supports ``len()``, iteration and indexed access.
    c                   r   r   )r   rX   r   r   r   r    r"   r#   r   E  r   z_Columns.__init__c                 C  s8   z| j | }W n ty   d| }t|w t|| S )z*Provide indexed access, e.g. 'columns[0]'.z!column index [%d] is out of range)_gridCol_lst
IndexErrorr+   )r   rO   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   )rT   r   r:   r"   r"   r#   __len__V  s   
z_Columns.__len__c                 C  r6   )zBReference to the |Table| object this column collection belongs to.r   r:   r"   r"   r#   rj   Y  rw   z_Columns.tablec                 C  s   | j j}|jS )zfSequence containing ``<w:gridCol>`` elements for this table, each
        representing a table column.)r   r&   r1   )r   r&   r"   r"   r#   r   ^  s   z_Columns._gridCol_lst)ry   rz   r{   r|   r   r   r   r   r}   rj   r   r   r"   r"   r    r#   rX   ?  s    	
rX   c                      sz   e Zd ZdZ fddZe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 )r2   z
Table row.c                   r   r   )r   r2   r   _trr   )r   r-   r   r    r"   r#   r   i  r$   z_Row.__init__r4   Tuple[_Cell]c                 C  r   )zASequence of |_Cell| instances corresponding to cells in this row.)r   rj   r^   r   r:   r"   r"   r#   rQ   m  r   z
_Row.cellsc                 C  r6   )znReturn a |Length| object representing the height of this cell, or |None| if
        no explicit height is set.r   trHeight_valr:   r"   r"   r#   heightr     z_Row.heightc                 C  r=   r   r   r>   r"   r"   r#   r   x  r?   c                 C  r6   )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   trHeight_hRuler:   r"   r"   r#   height_rule|  r   z_Row.height_rulec                 C  r=   r   r   r>   r"   r"   r#   r     r?   c                 C  r6   )z4Reference to the |Table| object this row belongs to.r   r:   r"   r"   r#   rj     rw   z
_Row.tablec                 C  r6   )z3Index of this row in its table, starting from zero.)r   tr_idxr:   r"   r"   r#   r     rw   z_Row._index)r4   r   )ry   rz   r{   r|   r   r}   rQ   r   r~   r   rj   r   r   r"   r"   r    r#   r2   f  s"    




r2   c                      sb   e Zd ZdZ fddZeddd	Zeddd	Zddd	Zdd Zdd Ze	dd Z
  ZS )r_   zSequence of |_Row| objects corresponding to the rows in a table.

    Supports ``len()``, iteration, indexed access, and slicing.
    c                   r   r   )r   r_   r   r   r   r    r"   r#   r     r   z_Rows.__init__rO   rD   r4   r2   c                 C     d S r   r"   r   rO   r"   r"   r#   r        z_Rows.__getitem__slice
List[_Row]c                 C  r   r   r"   r   r"   r"   r#   r     r   int | slice_Row | List[_Row]c                 C  s   t | | S )z7Provide indexed access, (e.g. `rows[0]` or `rows[1:3]`))listr   r"   r"   r#   r     r?   c                   s    fdd j jD S )Nc                 3  s    | ]}t | V  qd S r   )r2   )rN   r-   r:   r"   r#   r     s    z!_Rows.__iter__.<locals>.<genexpr>)r   r)   r:   r"   r:   r#   r     s   z_Rows.__iter__c                 C  s   t | jjS r   )rT   r   r)   r:   r"   r"   r#   r     s   z_Rows.__len__c                 C  r6   )z?Reference to the |Table| object this row collection belongs to.r   r:   r"   r"   r#   rj     rw   z_Rows.table)rO   rD   r4   r2   )rO   r   r4   r   )rO   r   r4   r   )ry   rz   r{   r|   r   r   r   r   r   r}   rj   r   r"   r"   r    r#   r_     s    
r_   N)$r|   
__future__r   typingr   r   r   r   docx.blkcntnrr   docx.enum.styler   docx.oxml.simpletypesr	   docx.sharedr
   r   r   docxr   tdocx.enum.tabler   r   docx.oxml.tabler   r   r   docx.styles.styler   r   rF   r+   rX   r2   r_   r"   r"   r"   r#   <module>   s(     m '+