o
    1j                     @   s   d dl mZ ddlmZmZ ddlmZ edrd dlmZ edr.d d	l	m
Z
 d d
lmZ edr8d dlmZ dd Zeddd ZeddddZdd ZedddddZd ddZdd Zdd ZdS )!    )absolute_import   )function_requires_depsis_dep_available   Tablelxml)htmlopenpyxl)Workbook)get_column_letter	premailer)	Premailerc                 C   s   |   rt| S dS )z(
    Convert a string to an integer
    r   )isdigitint)s r   e/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/utils/io/tablepyxl.pystring_to_int   s   r   c                 C   s:   t | }|d}|D ]}|  qdd |dD S )z(
    Find all the tables in the doc
    z//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_widthr0   r   r   r   
write_rows2   sH   









r6   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_sheete   s   r<   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   r<   )r   r:   r=   Zinline_styles_docZtablesr   r   r   r   document_to_workbookn   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
    )r=   N)r@   save)r   filenamer=   r:   r   r   r   document_to_xl   s   rC   c                 C   s4   | j rt|| j ||}| jrt|| j||}dS dS )zL
    Inserts a table into the worksheet at the specified column and row
    N)headr6   body)r   r2   r    r   r   r   r   r9      s
   r9   c                 C   s&   |j }|j|j}}t| ||| dS )zE
    Inserts a table at the location of an openpyxl Cell object.
    N)parentr    r   r9   )r   r)   r;   r    r   r   r   r   insert_table_at_cell   s   rG   )r   )NN)N)
__future__r   Z
utils.depsr   r   styler   r	   r
   r   r   Zopenpyxl.utilsr   r   r   r   r   r6   r<   r@   rC   r9   rG   r   r   r   r   <module>   s*   	
2	
	
