o
    'j                     @   s   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ dd Zd	d
 ZdddZdd ZdddZdddZdd Zdd ZdS )    )absolute_import)html)Workbook)get_column_letter)	PremailerTablec                 C   s   |   rt| S dS )Nr   )isdigitint)s r   p/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddleocr/ppstructure/table/tablepyxl/tablepyxl.pystring_to_int   s   r   c                 C   s:   t | }|d}|D ]}|  qdd |dD S )Nz//comment()c                 S   s   g | ]}t |qS r   r   ).0tabler   r   r   
<listcomp>   s    zget_Tables.<locals>.<listcomp>z//table)r   Z
fromstringZxpathZdrop_tag)doctreecommentscommentr   r   r   
get_Tables   s
   


r      c                 C   sR  ddl m} |}|jD ]}|jD ]}| j||d}t||r.|d7 }| j||d}t||st|jdd}	t|jdd}
|
dksH|	dkrY| j	||||
 d ||	 d d |j
|_
|| |d	}|d
}|	dkrt| jt| jpzdt|j
d }|r||kr|}n|r||k r|}|| jt| _||	7 }q|d7 }|}q|S )z}
    Writes every tr child element of elem to a row in the worksheet
    returns the next row after all rows are written
    r   )
MergedCell)rowcolumnr   colspan1rowspan)Z	start_rowstart_columnZend_row
end_columnz	min-widthz	max-width   )Zopenpyxl.cell.cellr   rowscellscell
isinstancer   elementgetZmerge_cellsvalueformatZget_dimensionmaxZcolumn_dimensionsr   widthlen)	worksheetelemr   r   r   Zinitial_columnZ	table_rowZ
table_cellr#   r   r   	min_width	max_widthr*   r   r   r   
write_rows   s<   






$
r0   c                 C   s&   |j | jdd}t| |dd dS )z
    Takes a table and workbook and writes the table to a new sheet.
    The sheet title will be the same as the table attribute name.
    name)titler   N)Zcreate_sheetr%   r&   insert_table)r   wbwsr   r   r   table_to_sheetD   s   r6   Nc                 C   sH   |st  }||j t| |dd }t|}|D ]}t|| q|S )z
    Takes a string representation of an html document and writes one sheet for
    every table in the document.
    The workbook is returned
    F)base_urlZremove_classes)r   removeZactiver   	transformr   r6   )r   r4   r7   Zinline_styles_docZtablesr   r   r   r   document_to_workbookM   s   r:   c                 C   s   t | |d}|| dS )z
    Takes a string representation of an html document and writes one sheet for
    every table in the document. The workbook is written out to a file called filename
    )r7   N)r:   save)r   filenamer7   r4   r   r   r   document_to_xl`   s   r=   c                 C   s4   | j rt|| j ||}| jrt|| j||}d S d S N)headr0   body)r   r,   r   r   r   r   r   r3   i   s
   r3   c                 C   s&   |j }|j|j}}t| ||| dS )zE
    Inserts a table at the location of an openpyxl Cell object.
    N)parentr   r   r3   )r   r#   r5   r   r   r   r   r   insert_table_at_cellp   s   rB   )r   )NNr>   )
__future__r   Zlxmlr   Zopenpyxlr   Zopenpyxl.utilsr   Z	premailerr   Ztablepyxl.styler   r   r   r0   r6   r:   r=   r3   rB   r   r   r   r   <module>   s   
+
	
	