o
    Xx[17                     @   s   d Z ddl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 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.
    )absolute_importprint_functionunicode_literals   )BlockItemContainer)WD_STYLE_TYPE)ST_Merge)IncheslazypropertyParentedc                       s   e Zd ZdZ fddZdd Zdd Zedd	 Zej	d
d	 Zedd Z
e
j	dd Z
dd Z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d Zedd Zej	dd Zed d! Zed"d# Zed$d% Z  ZS )&TablezA
    Proxy class for a WordprocessingML ``<w:tbl>`` element.
    c                        t t| | | | _| _d S N)superr   __init___element_tblselftblparent	__class__ :D:\Projects\ConvertPro\env\Lib\site-packages\docx\table.pyr         zTable.__init__c                 C   s<   | j j}| }||_| j jD ]	}| }||_qt|| S )zc
        Return a |_Column| object of *width*, newly added rightmost to the
        table.
        )r   tblGridZadd_gridColwtr_lstadd_tcwidth_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 )zQ
        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_rowc                 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   r,   /      zTable.alignmentc                 C      || j _d S r   r*   r   valuer   r   r   r,   9      c                 C   r)   )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.
        r+   autofitr-   r   r   r   r4   =   r.   zTable.autofitc                 C   r/   r   r3   r0   r   r   r   r4   G   r2   c                 C   s   ||| j   }| j| S )z
        Return |_Cell| instance correponding to table cell at *row_idx*,
        *col_idx* intersection, where (0, 0) is the top, left-most cell.
        _column_count_cells)r   row_idxZcol_idxZcell_idxr   r   r   cellK   s   
z
Table.cellc                    s*   | j  t|t | j} fdd|D S )zP
        Sequence of cells in the column at *column_idx* in this table.
        c                    s   g | ]} | qS r   r   ).0idxcellsr   r   
<listcomp>Y   s    z&Table.column_cells.<locals>.<listcomp>)r7   rangelenr6   )r   Z
column_idxidxsr   r<   r   column_cellsS   s   zTable.column_cellsc                 C      t | j| S )za
        |_Columns| instance representing the sequence of columns in this
        table.
        )_Columnsr   r-   r   r   r   columns[   s   zTable.columnsc                 C   s$   | j }|| }|| }| j|| S )zJ
        Sequence of cells in the row at *row_idx* in this table.
        r5   )r   r8   Zcolumn_countstartendr   r   r   	row_cellsc   s   zTable.row_cellsc                 C   rC   )zQ
        |_Rows| instance containing the sequence of rows in this table.
        )_Rowsr   r-   r   r   r   rowsl      z
Table.rowsc                 C   s   | j j}| j|tjS )a~  
        Read/write. A |_TableStyle| object representing the style applied to
        this table. 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   styles   s   zTable.stylec                 C   s   | j |tj}|| j_d S r   )rM   Zget_style_idr   rN   r   rL   )r   Zstyle_or_namerO   r   r   r   rP      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.tablec                 C   r)   )z
        A member of :ref:`WdTableDirection` indicating the direction in which
        the table cells are ordered, e.g. `WD_TABLE_DIRECTION.LTR`. |None|
        indicates the value is inherited from the style hierarchy.
        r   ZbidiVisual_valr-   r   r   r   table_direction      zTable.table_directionc                 C   r/   r   rR   r0   r   r   r   rS      r2   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 )z
        A 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   )
r6   r   Ziter_tcsr?   Z	grid_spanZvMerger   ZCONTINUEappend_Cell)r   	col_countr=   r$   Zgrid_span_idxr   r   r   r7      s   zTable._cellsc                 C   r)   )z;
        The number of grid columns in this table.
        )r   rX   r-   r   r   r   r6         zTable._column_countc                 C   r)   r   )r   ZtblPrr-   r   r   r   r+      s   zTable._tblPr)__name__
__module____qualname____doc__r   r%   r(   propertyr,   setterr4   r9   rB   r
   rE   rH   rJ   rP   rQ   rS   r7   r6   r+   __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 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 )rW   z
Table cellc                    s"   t t| || | | _| _d S r   )r   rW   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   rW   add_paragraph)r   textrP   r   r   r   rc      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.
        Nr   )r    r	   r   rW   	add_tablerc   )r   rJ   colsr    rQ   r   r   r   re      s   z_Cell.add_tablec                 C   s$   | j |j }}||}t|| jS )z
        Return 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.
        )ra   mergerW   _parent)r   Z
other_cellr$   Ztc_2Z	merged_tcr   r   r   rg      s   
z_Cell.mergec                       t t| jS )z
        List 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   rW   
paragraphsr-   r   r   r   rj      s   z_Cell.paragraphsc                    ri   )zR
        List of tables in the cell, in the order they appear. Read-only.
        )r   rW   tablesr-   r   r   r   rk      rK   z_Cell.tablesc                 C   s   d dd | jD S )z
        The 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   )rd   )r:   pr   r   r   	<genexpr>  s    z_Cell.text.<locals>.<genexpr>)joinrj   r-   r   r   r   rd      s   z
_Cell.textc                 C   s(   | j }|  | }| }||_dS )z
        Write-only. Set entire contents of cell to the string *text*. Any
        existing content or revisions are replaced.
        N)ra   clear_contentZadd_pZadd_rrd   )r   rd   r$   rm   rr   r   r   rd     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   rr   r   r   r   vertical_alignment  s   z_Cell.vertical_alignmentc                 C   s   | j  }||_d S r   )r   Zget_or_add_tcPrrs   )r   r1   rr   r   r   r   rt     s   

c                 C   r)   )zW
        The width of this cell in EMU, or |None| if no explicit width is set.
        ra   r    r-   r   r   r   r    "  rY   z_Cell.widthc                 C   r/   r   ru   r0   r   r   r   r    )  r2   )rb   N)rZ   r[   r\   r]   r   rc   re   rg   r^   rj   rk   rd   r_   rt   r    r`   r   r   r   r   rW      s,    





rW   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   2     
z_Column.__init__c                 C      t | j| jS )zV
        Sequence of |_Cell| instances corresponding to cells in this column.
        )tuplerQ   rB   _indexr-   r   r   r   r=   6     z_Column.cellsc                 C   r)   )zI
        Reference to the |Table| object this column belongs to.
        rh   rQ   r-   r   r   r   rQ   =  rY   z_Column.tablec                 C   r)   )za
        The width of this column in EMU, or |None| if no explicit width is
        set.
        rw   r   r-   r   r   r   r    D     z_Column.widthc                 C   r/   r   r~   r0   r   r   r   r    L  r2   c                 C   r)   )zH
        Index of this column in its table, starting from zero.
        )rw   ZgridCol_idxr-   r   r   r   r{   P  rY   z_Column._index)rZ   r[   r\   r]   r   r^   r=   rQ   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 )rD   z
    Sequence of |_Column| instances corresponding to the columns in a table.
    Supports ``len()``, iteration and indexed access.
    c                    rv   r   )r   rD   r   r   r   r   r   r   r   ]  rx   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   r;   r"   msgr   r   r   __getitem__a  s   
z_Columns.__getitem__c                 c   s    | j D ]}t|| V  qd S r   )r   r!   )r   r"   r   r   r   __iter__l  s   
z_Columns.__iter__c                 C   s
   t | jS r   )r@   r   r-   r   r   r   __len__p  s   
z_Columns.__len__c                 C   r)   )zT
        Reference to the |Table| object this column collection belongs to.
        r}   r-   r   r   r   rQ   s  rY   z_Columns.tablec                 C   s   | j j}|jS )zx
        Sequence containing ``<w:gridCol>`` elements for this table, each
        representing a table column.
        )r   r   r&   )r   r   r   r   r   r   z  s   z_Columns._gridCol_lst)rZ   r[   r\   r]   r   r   r   r   r^   rQ   r   r`   r   r   r   r   rD   X  s    
rD   c                       sx   e Zd ZdZ fddZe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                    r   r   )r   r'   r   _trr   )r   r#   r   r   r   r   r     r   z_Row.__init__c                 C   ry   )zS
        Sequence of |_Cell| instances corresponding to cells in this row.
        )rz   rQ   rH   r{   r-   r   r   r   r=     r|   z
_Row.cellsc                 C   r)   )z
        Return a |Length| object representing the height of this cell, or
        |None| if no explicit height is set.
        r   ZtrHeight_valr-   r   r   r   height  r   z_Row.heightc                 C   r/   r   r   r0   r   r   r   r     r2   c                 C   r)   )z
        Return 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_hRuler-   r   r   r   height_rule  rT   z_Row.height_rulec                 C   r/   r   r   r0   r   r   r   r     r2   c                 C   r)   )zF
        Reference to the |Table| object this row belongs to.
        r}   r-   r   r   r   rQ     rY   z
_Row.tablec                 C   r)   )zE
        Index of this row in its table, starting from zero.
        )r   Ztr_idxr-   r   r   r   r{     rY   z_Row._index)rZ   r[   r\   r]   r   r^   r=   r   r_   r   rQ   r{   r`   r   r   r   r   r'     s"    





r'   c                       sD   e Zd ZdZ fddZdd Zdd Zdd	 Zed
d Z	  Z
S )rI   z
    Sequence of |_Row| objects corresponding to the rows in a table.
    Supports ``len()``, iteration, indexed access, and slicing.
    c                    rv   r   )r   rI   r   r   r   r   r   r   r     rx   z_Rows.__init__c                 C   s   t | | S )z:
        Provide indexed access, (e.g. 'rows[0]')
        )list)r   r;   r   r   r   r     s   z_Rows.__getitem__c                    s    fdd j jD S )Nc                 3   s    | ]}t | V  qd S r   )r'   )r:   r#   r-   r   r   rn     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   )r@   r   r   r-   r   r   r   r     s   z_Rows.__len__c                 C   r)   )zQ
        Reference to the |Table| object this row collection belongs to.
        r}   r-   r   r   r   rQ     rY   z_Rows.table)rZ   r[   r\   r]   r   r   r   r   r^   rQ   r`   r   r   r   r   rI     s    rI   N)r]   Z
__future__r   r   r   Zblkcntnrr   Z
enum.styler   Zoxml.simpletypesr   sharedr	   r
   r   r   rW   r!   rD   r'   rI   r   r   r   r   <module>   s    2n*,7