o
    e                     @   sH   d Z ddlmZ ddlmZ ddlmZ ddlmZ G dd deZd	S )
a  Table block object parsed from raw image and text blocks.

Data Structure::

    {
        'type': int
        'bbox': (x0, y0, x1, y1),
        'rows': [
            {
                "bbox": (x0, y0, x1, y1),
                "height": float,
                "cells": [
                    {
                        'bbox': (x0, y0, x1, y1),
                        'border_color': (sRGB,,,), # top, right, bottom, left
                        'bg_color': sRGB,
                        'border_width': (,,,),
                        'merged_cells': (x,y), # this is the bottom-right cell of merged region: x rows, y cols
                        'blocks': [ {text blocks} ]
                    }, # end of cell
                    {},
                    None, # merged cell
                    ...
                ]
            }, # end of row
            {...} # more rows
        ] # end of row
    }
   )Row)Rows   )Block)docxc                       s   e Zd ZdZd%def fddZdd Zdd	 Zd
d Ze	dd Z
e	dd Ze	dd Ze	dd ZdefddZ fddZdefddZdefddZdd  Zd!d" Zd#d$ Z  ZS )&
TableBlockzTable block.Nrawc                    s>   |d u ri }t  | t| d|dg | _|   d S )N)parentrows)super__init__r   restoreget_rowsZset_lattice_table_block)selfr   	__class__ ID:\Projects\ConvertPro\env\Lib\site-packages\pdf2docx/table/TableBlock.pyr   *   s   zTableBlock.__init__c                 C   s4   z| j | }W |S  ty   d| d}t|w )Nz
Row index z out of range)r   
IndexError)r   idxrowmsgr   r   r   __getitem__4   s   zTableBlock.__getitem__c                 C      dd | j D S )Nc                 s   s    | ]}|V  qd S Nr   .0r   r   r   r   	<genexpr>>   s    z&TableBlock.__iter__.<locals>.<genexpr>r   r   r   r   r   __iter__=   s   zTableBlock.__iter__c                 C   
   t | jS r   lenr   r    r   r   r   __len__@   s   
zTableBlock.__len__c                 C   r"   )zCount of rows.r#   r    r   r   r   num_rowsC   s   
zTableBlock.num_rowsc                 C   s   | j r
t| jd S dS )zCount of columns.    )r&   r$   r   r    r   r   r   num_colsH   s   zTableBlock.num_colsc                 C   r   )zGet text contained in each cell.

        Returns:
            list: 2D-list with each element representing text in cell.
        c                 S   s   g | ]	}d d |D qS )c                 S   s   g | ]}|j qS r   )text)r   cellr   r   r   
<listcomp>T   s    z.TableBlock.text.<locals>.<listcomp>.<listcomp>r   r   r   r   r   r+   T   s    z#TableBlock.text.<locals>.<listcomp>r   r    r   r   r   r)   M   s   zTableBlock.textc                 C   sf   | j \}}}}| d d j\}}}}| d d j\}	}
}}||d  ||d  ||
d  ||d  fS )z"Outer bbox with border considered.r'   g       @)ZbboxZborder_width)r   Zx0Zy0x1y1Zw0_topZw0_rightZ	w0_bottomZw0_leftZw1_topZw1_rightZ	w1_bottomZw1_leftr   r   r   
outer_bboxV   s   ,zTableBlock.outer_bboxr   c                 C   s   | j | dS )zrAppend row to table and update bbox accordingly.

        Args:
            row (Row): Target row to add.
        N)r   append)r   r   r   r   r   r0   _   s   zTableBlock.appendc                    s"   t   }|d| j i |S )Nr
   )r   storeupdater   )r   resr   r   r   r1   h   s
   

zTableBlock.storeblocksc                 C   *   | j D ]}|D ]
}|sq|| qqdS )zuAssign ``blocks`` to associated cell.

        Args:
            blocks (list): A list of text/table blocks.
        N)r   assign_blocks)r   r4   r   r*   r   r   r   r6   p      
zTableBlock.assign_blocksshapesc                 C   r5   )ziAssign ``shapes`` to associated cell.

        Args:
            shapes (list): A list of Shape.
        N)r   assign_shapes)r   r8   r   r*   r   r   r   r9   |   r7   zTableBlock.assign_shapesc                 K   s0   | j D ]}|D ]}|sq|jdi | qqdS )znParse layout under cell level.

        Args:
            settings (dict): Layout parsing parameters.
        Nr   )r   parse)r   settingsr   r*   r   r   r   r:      s   
zTableBlock.parsec                 C   r5   )a`  Plot table block, i.e. cell/line/span, for debug purpose.
        
        Args:
            page (fitz.Page): pdf page.
            content (bool): Plot text blocks contained in cells if True.
            style (bool): Plot cell style if True, e.g. border width, shading.
            color (bool): Plot border stroke color if ``style=False``.
        N)r   plot)r   pager   r*   r   r   r   r<      s   
	zTableBlock.plotc                 C   s8   t || j tt|jD ]}| j| || qdS )zmCreate docx table.
        
        Args:
            table (Table): ``python-docx`` table instance.
        N)r   Zindent_tableZ
left_spaceranger$   r
   r   	make_docx)r   tableZidx_rowr   r   r   r?      s   zTableBlock.make_docxr   )__name__
__module____qualname____doc__dictr   r   r!   r%   propertyr&   r(   r)   r/   r   r0   r1   listr6   r9   r:   r<   r?   __classcell__r   r   r   r   r   (   s*    
	



	r   N)rD   r   r   Zcommon.Blockr   commonr   r   r   r   r   r   <module>   s   