B
    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                s    t t| | | | _| _d S )N)superr   __init___element_tbl)selftblparent)	__class__ </var/www/html/venv/lib/python3.7/site-packages/docx/table.pyr      s    zTable.__init__c             C   s@   | j j}| }||_x| j jD ]}| }||_q W 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   s:   | j }| }x |jjD ]}| }|j|_qW 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   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   r%   /   s    zTable.alignmentc             C   s   || j _d S )N)r$   r%   )r   valuer   r   r   r%   9   s    c             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.
        )r$   autofit)r   r   r   r   r'   =   s    zTable.autofitc             C   s   || j _d S )N)r$   r'   )r   r&   r   r   r   r'   G   s    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>)r)   rangelenr(   )r   Z
column_idxZidxsr   )r.   r   column_cellsS   s    zTable.column_cellsc             C   s   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.
        )r(   r)   )r   r*   Zcolumn_countstartendr   r   r   	row_cellsc   s    zTable.row_cellsc             C   s   t | j| S )zQ
        |_Rows| instance containing the sequence of rows in this table.
        )_Rowsr   )r   r   r   r   rowsl   s    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 )N)r;   Zget_style_idr   r<   r   r:   )r   Zstyle_or_namer=   r   r   r   r>      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   s   | j jS )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   bidiVisual_val)r   r   r   r   table_direction   s    zTable.table_directionc             C   s   || j _d S )N)r   r@   )r   r&   r   r   r   rA      s    c             C   s|   | j }g }xl| j D ]^}xXt|jD ]J}|jtjkrH|||   q&|dkr`||d  q&|t	||  q&W qW |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   )
r(   r   Ziter_tcsr0   Z	grid_spanZvMerger   ZCONTINUEappend_Cell)r   	col_countr.   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   rE   )r   r   r   r   r(      s    zTable._column_countc             C   s   | j jS )N)r   ZtblPr)r   r   r   r   r$      s    zTable._tblPr)__name__
__module____qualname____doc__r   r    r#   propertyr%   setterr'   r+   r2   r
   r4   r7   r9   r>   r?   rA   r)   r(   r$   __classcell__r   r   )r   r   r      s*   

	
	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 )rD   z
Table cellc                s"   t t| || | | _| _d S )N)r   rD   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   rD   add_paragraph)r   textr>   )r   r   r   rO      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.
        Nr   )r   r	   r   rD   	add_tablerO   )r   r9   colsr   r?   )r   r   r   rQ      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.
        )rM   mergerD   _parent)r   Z
other_cellr   Ztc_2Z	merged_tcr   r   r   rS      s    
z_Cell.mergec                s   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   rD   
paragraphs)r   )r   r   r   rU      s    z_Cell.paragraphsc                s   t t| jS )zR
        List of tables in the cell, in the order they appear. Read-only.
        )r   rD   tables)r   )r   r   r   rV      s    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 )N)rP   )r,   pr   r   r   	<genexpr>  s    z_Cell.text.<locals>.<genexpr>)joinrU   )r   r   r   r   rP      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)rM   clear_contentZadd_pZadd_rrP   )r   rP   r   rX   rr   r   r   rP     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   r]   r   r   r   vertical_alignment  s    z_Cell.vertical_alignmentc             C   s   | j  }||_d S )N)r   Zget_or_add_tcPrr^   )r   r&   r]   r   r   r   r_     s    
c             C   s   | j jS )zW
        The width of this cell in EMU, or |None| if no explicit width is set.
        )rM   r   )r   r   r   r   r   "  s    z_Cell.widthc             C   s   || j _d S )N)rM   r   )r   r&   r   r   r   r   )  s    )rN   N)rF   rG   rH   rI   r   rO   rQ   rS   rJ   rU   rV   rP   rK   r_   r   rL   r   r   )r   r   rD      s   
		rD   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 )N)r   r   r   _gridCol)r   r   r   )r   r   r   r   2  s    z_Column.__init__c             C   s   t | j| jS )zV
        Sequence of |_Cell| instances corresponding to cells in this column.
        )tupler?   r2   _index)r   r   r   r   r.   6  s    z_Column.cellsc             C   s   | j jS )zI
        Reference to the |Table| object this column belongs to.
        )rT   r?   )r   r   r   r   r?   =  s    z_Column.tablec             C   s   | j jS )za
        The width of this column in EMU, or |None| if no explicit width is
        set.
        )r`   r   )r   r   r   r   r   D  s    z_Column.widthc             C   s   || j _d S )N)r`   r   )r   r&   r   r   r   r   L  s    c             C   s   | j jS )zH
        Index of this column in its table, starting from zero.
        )r`   ZgridCol_idx)r   r   r   r   rb   P  s    z_Column._index)rF   rG   rH   rI   r   rJ   r.   r?   r   rK   rb   rL   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 )r3   z
    Sequence of |_Column| instances corresponding to the columns in a table.
    Supports ``len()``, iteration and indexed access.
    c                s   t t| | || _d S )N)r   r3   r   r   )r   r   r   )r   r   r   r   ]  s    z_Columns.__init__c             C   s>   y| 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   r-   r   msgr   r   r   __getitem__a  s    z_Columns.__getitem__c             c   s    x| j D ]}t|| V  qW d S )N)rc   r   )r   r   r   r   r   __iter__l  s    z_Columns.__iter__c             C   s
   t | jS )N)r1   rc   )r   r   r   r   __len__p  s    z_Columns.__len__c             C   s   | j jS )zT
        Reference to the |Table| object this column collection belongs to.
        )rT   r?   )r   r   r   r   r?   s  s    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   rc   z  s    z_Columns._gridCol_lst)rF   rG   rH   rI   r   rf   rg   rh   rJ   r?   rc   rL   r   r   )r   r   r3   X  s   r3   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                s    t t| | | | _| _d S )N)r   r"   r   _trr   )r   r   r   )r   r   r   r     s    z_Row.__init__c             C   s   t | j| jS )zS
        Sequence of |_Cell| instances corresponding to cells in this row.
        )ra   r?   r7   rb   )r   r   r   r   r.     s    z
_Row.cellsc             C   s   | j jS )z
        Return a |Length| object representing the height of this cell, or
        |None| if no explicit height is set.
        )ri   trHeight_val)r   r   r   r   height  s    z_Row.heightc             C   s   || j _d S )N)ri   rj   )r   r&   r   r   r   rk     s    c             C   s   | j jS )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.
        )ri   trHeight_hRule)r   r   r   r   height_rule  s    z_Row.height_rulec             C   s   || j _d S )N)ri   rl   )r   r&   r   r   r   rm     s    c             C   s   | j jS )zF
        Reference to the |Table| object this row belongs to.
        )rT   r?   )r   r   r   r   r?     s    z
_Row.tablec             C   s   | j jS )zE
        Index of this row in its table, starting from zero.
        )ri   Ztr_idx)r   r   r   r   rb     s    z_Row._index)rF   rG   rH   rI   r   rJ   r.   rk   rK   rm   r?   rb   rL   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 )r8   z
    Sequence of |_Row| objects corresponding to the rows in a table.
    Supports ``len()``, iteration, indexed access, and slicing.
    c                s   t t| | || _d S )N)r   r8   r   r   )r   r   r   )r   r   r   r     s    z_Rows.__init__c             C   s   t | | S )z:
        Provide indexed access, (e.g. 'rows[0]')
        )list)r   r-   r   r   r   rf     s    z_Rows.__getitem__c                s    fdd j jD S )Nc             3   s   | ]}t | V  qd S )N)r"   )r,   r   )r   r   r   rY     s    z!_Rows.__iter__.<locals>.<genexpr>)r   r   )r   r   )r   r   rg     s    z_Rows.__iter__c             C   s   t | jjS )N)r1   r   r   )r   r   r   r   rh     s    z_Rows.__len__c             C   s   | j jS )zQ
        Reference to the |Table| object this row collection belongs to.
        )rT   r?   )r   r   r   r   r?     s    z_Rows.table)rF   rG   rH   rI   r   rf   rg   rh   rJ   r?   rL   r   r   )r   r   r8     s   r8   N)rI   
__future__r   r   r   Zblkcntnrr   Z
enum.styler   Zoxml.simpletypesr   sharedr	   r
   r   r   rD   r   r3   r"   r8   r   r   r   r   <module>   s    2n*,7