o
    e'                     @   s  d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
mZ d dlmZmZ d dlmZ dZdd Zd	d
 Zdd Zi Zd"dd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d Zdd Z G d d! d!eZ!dS )#    )cell)Font	AlignmentPatternFill
NamedStyleBorderSideColor)
FILL_SOLID)FORMAT_CURRENCY_USD_SIMPLEFORMAT_PERCENTAGE)BLACKz
mm/dd/yyyyc                 C   s   dt i}|| | S )z1
    Convenience for looking up known colors
    black)r   get)colorZcmap r   [D:\Projects\ConvertPro\env\Lib\site-packages\paddleocr/ppstructure/table/tablepyxl/style.pycolormap   s   r   c                    s(   dd   fdd|  dD }t|S )z9
    Convert css style string to a python dictionary
    c                 S   s   dd |  |D S )Nc                 s   s    | ]}|  V  qd S N)strip.0sr   r   r   	<genexpr>       z<style_string_to_dict.<locals>.clean_split.<locals>.<genexpr>)split)stringdelimr   r   r   clean_split   s   z)style_string_to_dict.<locals>.clean_splitc                    s   g | ]}d |v r |d qS ):r   r   r   r   r   
<listcomp>   s    z(style_string_to_dict.<locals>.<listcomp>;)r   dict)stylestylesr   r    r   style_string_to_dict   s   r&   c                 C   s&   |  d|t|  d|dS )Nzborder-{}-stylezborder-{}-color)border_styler   )r   formatr   )r$   namer   r   r   get_side   s   r*   Nc                 C   s`  t | | j|d}|tvrt| ddk| dd| dd}t| dd	| d
| dddkd}| d}| dt }| d}|rU|dkrUt|pOt	||d}nt }t
tdi t| dtdi t| dtdi t| dtdi t| dtdi t| ddtdi t| dddd	}	dttd }
t|
||||	|d}|t|< t| S )zQ
    Change css style (stored in a python dictionary) to openpyxl NamedStyle
    )
style_dictparentnumber_formatzfont-weightboldr   Nz	font-size)r.   r   sizez
text-alignZgeneralzvertical-alignzwhite-spaceZnowrapnormal)
horizontalverticalZ	wrap_textzbackground-colorzforeground-colorz	fill-typetransparent)	fill_typeZstart_colorZ	end_colorleftrighttopbottomdiagonaloutline)	r5   r6   r7   r8   r9   Zdiagonal_directionr:   r2   r1   zStyle {}   )r)   fontfill	alignmentborderr-   r   )strr,   known_stylesr   r   	get_colorr   r	   r   r
   r   r   r*   r(   lenr   )r+   r-   Zstyle_and_format_stringr<   r>   Zbg_colorZfg_colorr4   r=   r?   r)   Z
pyxl_styler   r   r   style_dict_to_named_style&   sN   



rD   c                       sP   e Zd ZdZ fddZ fddZdd Zdd	 ZdddZdddZ	  Z
S )	StyleDictzQ
    It's like a dictionary, but it looks for items in the parent dictionary
    c                    s(   | dd | _tt| j|i | d S )Nr,   )popr,   superrE   __init__)selfargskwargs	__class__r   r   rH   `   s   zStyleDict.__init__c                    s6   || v rt t| |S | jr| j| S td|)Nz{} not found)rG   rE   __getitem__r,   KeyErrorr(   )rI   itemrL   r   r   rN   d   s
   
zStyleDict.__getitem__c                    s   t t fdd  D S )Nc                    s   g | ]	}|  |fqS r   )r   )r   krI   r   r   r!   m   s    z&StyleDict.__hash__.<locals>.<listcomp>)hashtuple_keysrR   r   rR   r   __hash__l   s   zStyleDict.__hash__c                 c   s\    t  }|  D ]
}|| |V  q| jr*| j D ]}||vr)|| |V  qd S d S r   )setkeysaddr,   rU   )rI   yieldedrQ   r   r   r   rU   r   s   

zStyleDict._keysNc                 C   s"   z| | W S  t y   | Y S w r   )rO   )rI   rQ   dr   r   r   r   }   s
   
zStyleDict.getc                 C   sP   |  ||}t|dr&|dr&|dd }t|dkr&ddd |D }|S )	z9
        Strip leading # off colors if necessary
        
startswith#r;   N    c                 s   s    | ]}d | V  qdS )   Nr   )r   cr   r   r   r      r   z&StyleDict.get_color.<locals>.<genexpr>)r   hasattrr\   rC   join)rI   rQ   r[   r   r   r   r   rB      s   zStyleDict.get_colorr   )__name__
__module____qualname____doc__rH   rN   rV   rU   r   rB   __classcell__r   r   rL   r   rE   \   s    
rE   c                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
Elementz
    Our base class for representing an html element along with a cascading style.
    The element is created along with a parent so that the StyleDict that we store
    can point to the parent's StyleDict.
    Nc                 C   s>   || _ d | _|r|jnd }tt|dd|d| _d | _d S )Nr$   r_   r,   )elementr-   r+   rE   r&   r   _style_cache)rI   rk   r,   Zparent_styler   r   r   rH      s
   
zElement.__init__c                 C   s   | j st| j| jd| _ | j S )zS
        Turn the css styles for this element into an openpyxl NamedStyle.
        )r-   )rl   rD   r+   r-   rR   r   r   r   r$      s   zElement.stylec                 C   s8   | j |}|r|dd dv r|dd }t|}|S )zf
        Extracts the dimension from the style dict of the Element and returns it as a float.
        N)ZpxZemptincm)r+   r   float)rI   Zdimension_key	dimensionr   r   r   get_dimension   s   zElement.get_dimensionr   )rd   re   rf   rg   rH   r$   rs   r   r   r   r   ri      s
    
ri   c                       s    e Zd ZdZ fddZ  ZS )TableaX  
    The concrete implementations of Elements are semantically named for the types of elements we are interested in.
    This defines a very concrete tree structure for html tables that we expect to deal with. I prefer this compared to
    allowing Element to have an arbitrary number of children and dealing with an abstract element tree.
    c                    s\   t t| | |d}|durt|| dnd| _|d}t|dur&|n|| d| _dS )z8
        takes an html table object (from lxml)
        ZtheadNrj   Ztbody)rG   rt   rH   find	TableHeadhead	TableBodybody)rI   tableZ
table_headZ
table_bodyrL   r   r   rH      s
   

zTable.__init__rd   re   rf   rg   rH   rh   r   r   rL   r   rt      s    rt   c                       "   e Zd ZdZd fdd	Z  ZS )rv   zB
    This class maps to the `<th>` element of the html table.
    Nc                    2   t t j||d  fdd|dD  _d S )Nrj   c                       g | ]}t | d qS rj   TableRowr   trrR   r   r   r!          z&TableHead.__init__.<locals>.<listcomp>r   )rG   rv   rH   findallrows)rI   rw   r,   rL   rR   r   rH         zTableHead.__init__r   r{   r   r   rL   r   rv          rv   c                       r|   )rx   zE
    This class maps to the `<tbody>` element of the html table.
    Nc                    r}   )Nrj   c                    r~   r   r   r   rR   r   r   r!      r   z&TableBody.__init__.<locals>.<listcomp>r   )rG   rx   rH   r   r   )rI   ry   r,   rL   rR   r   rH      r   zTableBody.__init__r   r{   r   r   rL   r   rx      r   rx   c                       r|   )r   zB
    This class maps to the `<tr>` element of the html table.
    Nc                    s<   t t j||d  fdd|d|d D  _d S )Nrj   c                    r~   r   )	TableCell)r   r   rR   r   r   r!      r   z%TableRow.__init__.<locals>.<listcomp>thtd)rG   r   rH   r   cells)rI   r   r,   rL   rR   r   rH      s   (zTableRow.__init__r   r{   r   r   rL   r   r      r   r   c                 C   s   t |  S r   )_element_to_stringr   )elr   r   r   element_to_string   s   r   c                 C   sZ   d}|   D ]
}|dt| 7 }q| jr| j nd}| jr#| j nd}|| d | S )Nr_   
)Ziterchildrenr   textr   tail)r   r   xr   r   r   r   r   r      s   r   c                       sB   e Zd ZdZh dZd fdd	Zdd Zdd	 Zd
d Z  Z	S )r   zB
    This class maps to the `<td>` element of the html table.
    >   TYPE_STRINGTYPE_CURRENCYZ	TYPE_BOOLZTYPE_FORMULA_CACHE_STRINGTYPE_PERCENTAGETYPE_INTEGERZ	TYPE_NULLTYPE_FORMULAZ
TYPE_ERRORTYPE_NUMERICZTYPE_INLINENc                    s,   t t| j||d t|| _|  | _d S )Nrj   )rG   r   rH   r   valueget_number_formatr-   )rI   r   r,   rL   r   r   rH      s   
zTableCell.__init__c                 C   sX   | j t| jdd @ }|r%d|v rd}n|h d@ r d}n| }nd}tt|S )Nclassr_   r   >   r   r   r   r   r   )
CELL_TYPESrW   rk   r   r   rF   getattrr   )rI   Z
cell_typesZ	cell_typer   r   r   	data_type   s   

zTableCell.data_typec                 C   s   d| j dd v rtS d| j dd v rdS d| j dd v r'tS d| j dd v r4tS |  tjkrNzt	| j
 W dS  tyM   Y dS w d S )	Nr   r   r_   r   z#,##0r   Z	TYPE_DATEz#,##0.##)rk   r   r   r   r   FORMAT_DATE_MMDDYYYYr   r   r   intr   
ValueErrorrR   r   r   r   r     s    zTableCell.get_number_formatc                 C   s$   |   |_ |  }|r||_d S d S r   )r$   r   )rI   r   r   r   r   r   r(     s
   

zTableCell.formatr   )
rd   re   rf   rg   r   rH   r   r   r(   rh   r   r   rL   r   r      s    r   r   )"Zopenpyxl.cellr   Zopenpyxl.stylesr   r   r   r   r   r   r	   Zopenpyxl.styles.fillsr
   Zopenpyxl.styles.numbersr   r   Zopenpyxl.styles.colorsr   r   r   r&   r*   rA   rD   r#   rE   objectri   rt   rv   rx   r   r   r   r   r   r   r   r   <module>   s(   $

63!			