o
    h                     @  sx  d Z ddlmZ ddlmZmZmZmZ ddlZddl	Z	ddl
Z
ddlmZmZmZmZ ddlZddl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mZ ddlmZm Z m!Z!m"Z" ddl#m$  m%Z& ddl'm(Z( ddl)m*Z* ddl+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1 erddl2m3Z3m4Z4m5Z5m6Z6 ddlm7Z7 G dd dZ8G dd de8Z9G dd dZ:G dd dZ;dS )zC
Utilities for conversion to writer-agnostic Excel representation.
    )annotations)HashableIterableMappingSequenceN)TYPE_CHECKINGAnyCallablecast)is_list_like)doc)find_stack_level)missing)is_float	is_scalar)	DataFrameIndex
MultiIndexPeriodIndex)_shared_docs)CSS4_COLORS)CSSResolver
CSSWarning)get_level_lengths)pprint_thing)FilePath
IndexLabelStorageOptionsWriteExcelBufferExcelWriterc                   @  s&   e Zd ZdZeZ			ddddZdS )	ExcelCellrowcolvalstyle
mergestartmergeendNr#   intr$   r'   
int | Noner(   returnNonec                 C  s(   || _ || _|| _|| _|| _|| _d S Nr"   )selfr#   r$   r%   r&   r'   r(    r/   k/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/pandas/io/formats/excel.py__init__C   s   	
zExcelCell.__init__)NNN)
r#   r)   r$   r)   r'   r*   r(   r*   r+   r,   )__name__
__module____qualname__
__fields__	__slots__r1   r/   r/   r/   r0   r!   ?   s    r!   c                      s   e Zd Zd fddZ  ZS )CssExcelCellr#   r)   r$   r&   dict | None
css_styles3dict[tuple[int, int], list[tuple[str, Any]]] | Nonecss_rowcss_colcss_converterCallable | Noner+   r,   c	                   sR   |r|rdd |||f D }
t |
 }||}t jd||||d|	 d S )Nc                 S  s   i | ]	\}}|  |qS r/   lower).0propr%   r/   r/   r0   
<dictcomp>c   s    z)CssExcelCell.__init__.<locals>.<dictcomp>r#   r$   r%   r&   r/   )	frozensetitemssuperr1   )r.   r#   r$   r%   r&   r9   r;   r<   r=   kwargsdeclaration_dictunique_declarations	__class__r/   r0   r1   U   s   
 zCssExcelCell.__init__)r#   r)   r$   r)   r&   r8   r9   r:   r;   r)   r<   r)   r=   r>   r+   r,   )r2   r3   r4   r1   __classcell__r/   r/   rK   r0   r7   T   s    r7   c                   @  s  e Zd ZU dZeZdddddddZddddddddddddddZdddd	Zd
ddddZ	dd dD Z
ded< dgdhddZe ZdiddZdidd Zdjd#d$Zdkd&d'Zdld(d)Zdmd+d,Zdnd.d/Zdod3d4Zdpd6d7Zdqd9d:Zdrd=d>Zdsd?d@ZdtdBdCZdudEdFZdmdGdHZdmdIdJZdvdLdMZdwdOdPZ dmdQdRZ!dvdSdTZ"dxdVdWZ#dydZd[Z$dzd]d^Z%d{dadbZ&d|dcddZ'd{dedfZ(dS )}CSSToExcelConverteraK  
    A callable for converting CSS declarations to ExcelWriter styles

    Supports parts of CSS 2.2, with minimal CSS 3.0 support (e.g. text-shadow),
    focusing on font styling, backgrounds, borders and alignment.

    Operates by first computing CSS styles in a fairly generic
    way (see :meth:`compute_css`) then determining Excel style
    properties from CSS properties (see :meth:`build_xlstyle`).

    Parameters
    ----------
    inherited : str, optional
        CSS declarations understood to be the containing scope for the
        CSS processed by :meth:`__call__`.
    topcenterbottom)rO   ztext-topmiddlebaselinerQ   ztext-bottomTF)boldbolder600700800900normallighter100200300400500)rZ   italicoblique            )serifz
sans-serifcursivefantasyc                 C  s   i | ]}|  |qS r/   r?   )rA   r&   r/   r/   r0   rC      s    zCSSToExcelConverter.<dictcomp>)
dashedmediumDashDot
dashDotDothairdottedmediumDashDotDotdoubledashDotslantDashDotmediumDashedzdict[str, str] | None	inheritedN
str | Noner+   r,   c                 C  s.   |d ur|  || _nd | _t| j| _d S r-   )compute_cssrt   	functoolscache_call_uncached_call_cached)r.   rt   r/   r/   r0   r1      s   zCSSToExcelConverter.__init__declarations str | frozenset[tuple[str, str]]dict[str, dict[str, str]]c                 C  s
   |  |S )a  
        Convert CSS declarations to ExcelWriter style.

        Parameters
        ----------
        declarations : str | frozenset[tuple[str, str]]
            CSS string or set of CSS declaration tuples.
            e.g. "font-weight: bold; background: blue" or
            {("font-weight", "bold"), ("background", "blue")}

        Returns
        -------
        xlstyle : dict
            A style as interpreted by ExcelWriter when found in
            ExcelCell.style.
        )rz   )r.   r{   r/   r/   r0   __call__   s   
zCSSToExcelConverter.__call__c                 C  s   |  || j}| |S r-   )rv   rt   build_xlstyle)r.   r{   
propertiesr/   r/   r0   ry      s   
z"CSSToExcelConverter._call_uncachedpropsMapping[str, str]c                   sH   |  || || || || |d}d fdd  | |S )	N)	alignmentborderfillfontnumber_formatddict[str, str | None]r+   r,   c                   sF   t |  D ]\}}|du r| |= qt|tr  | |s | |= qdS )z4Remove key where value is None, through nested dictsN)listrF   
isinstancedict)r   kvremove_noner/   r0   r      s   
z6CSSToExcelConverter.build_xlstyle.<locals>.remove_none)r   r   r+   r,   )build_alignmentbuild_border
build_fill
build_fontbuild_number_format)r.   r   outr/   r   r0   r      s   

z!CSSToExcelConverter.build_xlstyledict[str, bool | str | None]c                 C  s   | d| || |dS )Nz
text-align)
horizontalvertical	wrap_text)get_get_vertical_alignment_get_is_wrap_textr.   r   r/   r/   r0   r      s   z#CSSToExcelConverter.build_alignmentc                 C     | d}|r| j |S d S )Nzvertical-align)r   VERTICAL_MAP)r.   r   vertical_alignr/   r/   r0   r        
z+CSSToExcelConverter._get_vertical_alignmentbool | Nonec                 C  s"   | dd u r	d S t|d dvS )Nzwhite-space)nowrapprezpre-line)r   boolr   r/   r/   r0   r     s   z%CSSToExcelConverter._get_is_wrap_text dict[str, dict[str, str | None]]c                   s    fdddD S )Nc                   sf   i | ]/}|  d | d d | d d | d d | ddqS )zborder-z-stylez-widthz-color)r&   color)_border_styler   color_to_excel)rA   sider   r.   r/   r0   rC     s    	z4CSSToExcelConverter.build_border.<locals>.<dictcomp>rO   rightrQ   leftr/   r   r/   r   r0   r     s   	z CSSToExcelConverter.build_borderr&   widthr   c                 C  s   |d u r|d u r|d u rd S |d u r|d u rdS |dv rdS |  |}|d u r)dS |dv r/|S |dkr5dS |dkrA|dv r?dS dS |dkrM|dv rKdS d	S || jv rW| j| S tjd
t| tt d dS )Nnone)r   hidden)Ngrooveridgeinsetoutsetsolidrp   rn   )rm   thinro   rj   rs   zUnhandled border style format: 
stacklevel)_get_width_nameBORDER_STYLE_MAPwarningswarnreprr   r   )r.   r&   r   r   
width_namer/   r/   r0   r     s:   


z!CSSToExcelConverter._border_stylewidth_inputc                 C  s2   |  |}|dk rd S |dk rdS |dk rdS dS )Ngh㈵>g?r   gffffff@mediumthick)_width_to_float)r.   r   r   r/   r/   r0   r   V  s   
z#CSSToExcelConverter._get_width_namefloatc                 C  s   |d u rd}|  |S )N2pt)_pt_to_float)r.   r   r/   r/   r0   r   `  s   
z#CSSToExcelConverter._width_to_float	pt_stringstrc                 C  s   | dsJ t|dS )Npt)endswithr   rstrip)r.   r   r/   r/   r0   r   e  s   z CSSToExcelConverter._pt_to_floatc                 C  s&   | d}|dvr| |ddS d S )Nzbackground-color)Ntransparentr   r   )fgColorpatternType)r   r   )r.   r   
fill_colorr/   r/   r0   r   i  s   
zCSSToExcelConverter.build_fillr   c                 C  s,   | d}t|tr|ddn|}d|iS )Nznumber-format   §;format_code)r   r   r   replace)r.   r   fcr/   r/   r0   r   p  s   
z'CSSToExcelConverter.build_number_format$dict[str, bool | float | str | None]c                 C  sv   |  |}| |}|r|d nd | || || || |d|v r'dnd d|v p-d | |d| |d	S )Nr   	underlinesinglezline-throughr   )	namefamilysizerT   ra   r   striker   shadow)	_get_font_names_get_decoration_select_font_family_get_font_size_get_is_bold_get_is_italicr   r   _get_shadow)r.   r   
font_names
decorationr/   r/   r0   r   u  s   


zCSSToExcelConverter.build_fontc                 C  r   )Nzfont-weight)r   BOLD_MAP)r.   r   weightr/   r/   r0   r     r   z CSSToExcelConverter._get_is_boldc                 C  r   )Nz
font-style)r   
ITALIC_MAP)r.   r   
font_styler/   r/   r0   r     r   z"CSSToExcelConverter._get_is_italicSequence[str]c                 C  s   | d}|d ur| S dS )Nztext-decorationr/   )r   split)r.   r   r   r/   r/   r0   r     s   
z#CSSToExcelConverter._get_decorationr   c                 C  s   d|v rdS d S )Nr   r   r/   )r.   r   r/   r/   r0   _get_underline  s   z"CSSToExcelConverter._get_underlinec                 C  s    d|v rt td|d S d S )Nztext-shadowz^[^#(]*[1-9])r   researchr   r/   r/   r0   r     s   zCSSToExcelConverter._get_shadowc                 C  s   t d|dd}g }|D ]3}|d d dkr#|dd dd}n|d d dkr6|dd d	d}n| }|rA|| q|S )
Nz(?x)
            (
            "(?:[^"]|\\")+"
            |
            '(?:[^']|\\')+'
            |
            [^'",]+
            )(?=,|\s*$)
        zfont-family rc   "z\"'z\')r   findallr   r   stripappend)r.   r   font_names_tmpr   r   r/   r/   r0   r     s   
	
z#CSSToExcelConverter._get_font_namesfloat | Nonec                 C  s    | d}|d u r|S | |S )Nz	font-size)r   r   )r.   r   r   r/   r/   r0   r     s   

z"CSSToExcelConverter._get_font_sizer   r*   c                 C  s(   d }|D ]}| j |}|r |S q|S r-   )
FAMILY_MAPr   )r.   r   r   r   r/   r/   r0   r     s   z'CSSToExcelConverter._select_font_familyr%   c                 C  s^   |d u rd S |  |r| |S z| j| W S  ty.   tjdt| tt d Y d S w )NzUnhandled color format: r   )	_is_hex_color_convert_hex_to_excelNAMED_COLORSKeyErrorr   r   r   r   r   r.   r%   r/   r/   r0   r     s   

z"CSSToExcelConverter.color_to_excelcolor_stringr   c                 C  s   t |dS )N#)r   
startswith)r.   r   r/   r/   r0   r     s   z!CSSToExcelConverter._is_hex_colorc                 C  sD   | d}| |r|d d |d d  |d d   S | S )Nr   r   rd   rc   )lstrip_is_shorthand_colorupperr.   r   coder/   r/   r0   r     s   

(z)CSSToExcelConverter._convert_hex_to_excelc                 C  s8   | d}t|dkrdS t|dkrdS td| )zcCheck if color code is shorthand.

        #FFF is a shorthand as opposed to full #FFFFFF.
        r      T   FzUnexpected color )r  len
ValueErrorr  r/   r/   r0   r    s   
z'CSSToExcelConverter._is_shorthand_colorr-   )rt   ru   r+   r,   )r{   r|   r+   r}   )r   r   r+   r}   )r   r   r+   r   )r   r   r+   ru   )r   r   r+   r   )r   r   r+   r   )r&   ru   r   ru   r   ru   )r   ru   r+   ru   )r   ru   r+   r   )r   r   r+   r   )r   r   )r   r   r+   r   )r   r   r+   r   )r   r   r+   r   )r   r   r+   ru   )r   r   r+   r   )r   r   r+   r*   )r%   ru   r+   ru   )r   r   r+   r   )r   r   r+   r   ))r2   r3   r4   __doc__r   r   r   r   r   r   r   __annotations__r1   r   rv   r~   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r/   r/   r/   r0   rN   m   s   
 









7














	

rN   c                   @  s   e Zd ZdZdZdZ									dHdIddZedJddZd d! Z	dKd#d$Z
dKd%d&ZdKd'd(ZdKd)d*ZdKd+d,ZdKd-d.ZedLd/d0ZdMd3d4ZdKd5d6Zeed7 d8	9	:	:				dNdOdFdGZdS )PExcelFormattera9  
    Class for formatting a DataFrame to a list of ExcelCells,

    Parameters
    ----------
    df : DataFrame or Styler
    na_rep: na representation
    float_format : str, default None
        Format string for floating point numbers
    cols : sequence, optional
        Columns to write
    header : bool or sequence of str, default True
        Write out column names. If a list of string is given it is
        assumed to be aliases for the column names
    index : bool, default True
        output row names (index)
    index_label : str or sequence, default None
        Column label for index column(s) if desired. If None is given, and
        `header` and `index` are True, then the index names are used. A
        sequence should be given if the DataFrame uses MultiIndex.
    merge_cells : bool, default False
        Format MultiIndex and Hierarchical Rows as merged cells.
    inf_rep : str, default `'inf'`
        representation for np.inf values (which aren't representable in Excel)
        A `'-'` sign will be added in front of -inf.
    style_converter : callable, optional
        This translates Styler styles (CSS) into ExcelWriter styles.
        Defaults to ``CSSToExcelConverter()``.
        It should have signature css_declarations string -> excel style.
        This is only called for body cells.
    i   i @  r   NTFinfna_repr   float_formatru   colsSequence[Hashable] | NoneheaderSequence[Hashable] | boolindexr   index_labelIndexLabel | Nonemerge_cellsinf_repstyle_converterr>   r+   r,   c                 C  s   d| _ || _t|ts!|| _| j  |j}|
d u rt }
|
| _nd | _d | _|| _	|d urWt
t||js<tdt
t||jt
t|krPtd|j|d| _	| j	j| _|| _|| _|| _|| _|| _|	| _d S )Nr   z,passes columns are not ALL present dataframez.Not all names specified in 'columns' are found)columns)
rowcounterr  r   r   styler_computedatarN   r  dfr	  r   intersectionr  r   setreindexr  r  r  r  r  r  )r.   r   r  r  r  r  r  r  r  r  r  r/   r/   r0   r1     s2   

 

zExcelFormatter.__init__ dict[str, dict[str, str | bool]]c                 C  s    ddidddddddddS )	NrT   Tr   r   rP   rO   )r   r   )r   bordersr   r/   r.   r/   r/   r0   header_styleG  s   zExcelFormatter.header_stylec                 C  s   t |rt|r| j}n%t|r2t|r| j}nt|r&d| j }n| jd ur2t	| j| }t
|dd d ur>td|S )N-tzinfoz{Excel does not support datetimes with timezones. Please ensure that datetimes are timezone unaware before writing to Excel.)r   r   isnar  r   isposinf_scalarr  isneginf_scalarr  r   getattrr
  r   r/   r/   r0   _format_valueT  s   


zExcelFormatter._format_valueIterable[ExcelCell]c                 c  s   | j jdkr| jstd| js| jsd S | j }|j| jdd}t|}d}d}| jr=t	| j
jtr=t| j
jd d }| jrt|jD ]\}}t|||| jdV  qEtt||j|jD ]B\}\}}}	||	}
| D ]1\}}d\}}|dkr||| | }}t||| d |
| | jt| jdd ||| j||d	
V  qnq^n+tt| D ]$\}}
d
tt|
}t||| d || jt| jdd ||| jdV  q|| _d S )Nrc   z]Writing to Excel with MultiIndex columns and no index ('index'=False) is not yet implemented.Fsparsifyinclude_namesr   rD   NNctx_columns
r#   r$   r%   r&   r9   r;   r<   r=   r'   r(   .r#   r$   r%   r&   r9   r;   r<   r=   )r  nlevelsr  NotImplementedError_has_aliasesr  _format_multir  r   r   r   r   r	  	enumeratenamesr!   r'  ziplevelscodestakerF   r7   r-  r  r  joinmapr   r  )r.   r  
level_strslevel_lengths	coloffsetlnumr   spansr?  level_codesvaluesispan_valr'   r(   r   r/   r/   r0   _format_header_mif  sx   






z ExcelFormatter._format_header_mic                 c  s    | j s| jrgd}| jrd}t| jjtrt| jjj}| j}| j rGt	t
| j| _t| jt| jkrDtdt| j dt| j d| j}t|D ]\}}t| j|| || jt| jdd d|| jdV  qKd S d S )Nr   rc   zWriting z cols but got z aliasesr4  r7  )r:  r  r  r   r   r   r	  r=  r  r
   r   r
  r<  r7   r  r'  r-  r  r  )r.   rF  colnamescolindexcolnamer/   r/   r0   _format_header_regular  s<   
z%ExcelFormatter._format_header_regularc                   s   t  jtr  }n  }d} jjjrGdd  jjjD dgt j  }t	
dd dd |D rG fd	dt|D }  jd
7  _t||S )Nr/   c                 S  s   g | ]
}|d ur
|ndqS )Nr   r/   rA   xr/   r/   r0   
<listcomp>  s    z1ExcelFormatter._format_header.<locals>.<listcomp>r   c                 S  s   | o|S r-   r/   )rS  yr/   r/   r0   <lambda>  s    z/ExcelFormatter._format_header.<locals>.<lambda>c                 s  s    | ]}|d kV  qdS )r   Nr/   rR  r/   r/   r0   	<genexpr>  s    z0ExcelFormatter._format_header.<locals>.<genexpr>c                 3  s&    | ]\}}t  j|| jV  qd S r-   )r!   r  r'  )rA   rO  r%   r&  r/   r0   rW    s
    
rc   )r   r  r   rM  rQ  r   r  r=  r	  rw   reducer<  r  	itertoolschain)r.   gengen2r#   r/   r&  r0   _format_header  s    


zExcelFormatter._format_headerc                 C  s   t | jjtr|  S |  S r-   )r   r   r  r   _format_hierarchical_rows_format_regular_rowsr&  r/   r/   r0   _format_body  s   zExcelFormatter._format_bodyc                 c  s2   | j s| jr|  jd7  _| jr| jr%t| jtttj	t
fr%| jd }n| jr2t| jtr2| j}n| jjjd }t| jtrF|  jd7  _|rY| jdurYt| jd d|| jV  | jj}t| jjtrj| jj }t|D ]\}}t| j| d|| jt| jdd |d| jdV  qnd}nd}| |E d H  d S )Nrc   r   F	ctx_indexr7  )r:  r  r  r  r  r   r   tuplenpndarrayr   r   r   r=  r  r   r!   r'  r   to_timestampr<  r7   r-  r  r  _generate_body)r.   r  index_valuesidxidxvalrF  r/   r/   r0   r_    s@   

z#ExcelFormatter._format_regular_rowsc                 c  s   | j s| jr|  jd7  _d}| jr| jjj}| jr)t| jtt	t
jtfr)| j}t| jtr9| jr9|  jd7  _tj| rX| jdurXt|D ]\}}t| jd ||| jV  qG| jr| jjjddd}t|}t|| jjj| jjjD ]J\}}}	|j|	|j|jd}
| D ]2\}}d\}}|dkr| j| | d }|}t| j| ||
| | jt| j dd ||| j!||d	
V  q|d7 }qtn-t| jj D ]&}t|D ]\}}t| j| ||| jt| j dd ||| j!d
V  q|d7 }q| "|E d H  d S )Nrc   r   FTr0  )
allow_fill
fill_valuer3  ra  r5  r7  )#r:  r  r  r  r   r=  r  r   r   rb  rc  rd  r   r  r   r  comany_not_noner<  r!   r'  r;  r   r>  r?  r@  rA  _can_hold_na	_na_valuerF   r7   r-  r  r  rf  )r.   gcolidxindex_labelscidxr   rD  rE  rH  r?  rI  rJ  rK  rL  r'   r(   indexcolvalsrh  indexcolvalr/   r/   r0   r^    sz   





z(ExcelFormatter._format_hierarchical_rowsc                 C  s
   t | jS )z1Whether the aliases for column names are present.)r   r  r&  r/   r/   r0   r:  c  s   
zExcelFormatter._has_aliasesrF  r)   c                 c  sp    t t| jD ]-}| jjd d |f }t|D ]\}}t| j| || |d t| j	dd ||| j
dV  qqd S )Nctxr7  )ranger	  r  r   ilocr<  r7   r  r-  r  r  )r.   rF  colidxseriesrK  r%   r/   r/   r0   rf  h  s    
zExcelFormatter._generate_bodyc                 c  s4    t |  |  D ]}| |j|_|V  qd S r-   )rY  rZ  r]  r`  r.  r%   )r.   cellr/   r/   r0   get_formatted_cellsx  s
   z"ExcelFormatter.get_formatted_cellsstorage_options)r|  Sheet1r   writer)FilePath | WriteExcelBuffer | ExcelWriter
sheet_namestartrowstartcolfreeze_panestuple[int, int] | NoneengineStorageOptions | Noneengine_kwargsr8   c	              	   C  s   ddl m}	 | jj\}
}|
| jks|| jkr(td|
 d| d| j d| j |du r.i }|  }t||	r:d}n
|	||||d}d	}z|j	|||||d
 W |rX|
  dS dS |ra|
  w w )ab  
        writer : path-like, file-like, or ExcelWriter object
            File path or existing ExcelWriter
        sheet_name : str, default 'Sheet1'
            Name of sheet which will contain DataFrame
        startrow :
            upper left cell row to dump data frame
        startcol :
            upper left cell column to dump data frame
        freeze_panes : tuple of integer (length 2), default None
            Specifies the one-based bottommost row and rightmost column that
            is to be frozen
        engine : string, default None
            write engine to use if writer is a path - you can also set this
            via the options ``io.excel.xlsx.writer``,
            or ``io.excel.xlsm.writer``.

        {storage_options}

        engine_kwargs: dict, optional
            Arbitrary keyword arguments passed to excel engine.
        r   r   z-This sheet is too large! Your sheet size is: z, z Max sheet size is: NF)r  r|  r  T)r  r  r  )pandas.io.excelr    r   shapemax_rowsmax_colsr
  r{  r   _write_cellsclose)r.   r~  r  r  r  r  r  r|  r  r    num_rowsnum_colsformatted_cells	need_saver/   r/   r0   write}  sF   "
	
zExcelFormatter.write)	r   NNTTNFr  N)r  r   r  ru   r  r  r  r  r  r   r  r  r  r   r  r   r  r>   r+   r,   )r+   r$  )r+   r/  )r+   r   )rF  r)   r+   r/  )r}  r   r   NNNN)r~  r  r  r   r  r)   r  r)   r  r  r  ru   r|  r  r  r8   r+   r,   )r2   r3   r4   r  r  r  r1   propertyr'  r.  rM  rQ  r]  r`  r_  r^  r:  rf  r{  r   r   r  r/   r/   r/   r0   r    sH     -

E



.O

r  )<r  
__future__r   collections.abcr   r   r   r   rw   rY  r   typingr   r   r	   r
   r   numpyrc  pandas._libs.libr   pandas.util._decoratorsr   pandas.util._exceptionsr   pandas.core.dtypesr   pandas.core.dtypes.commonr   r   pandasr   r   r   r   pandas.core.commoncorecommonrl  pandas.core.shared_docsr   pandas.io.formats._color_datar   pandas.io.formats.cssr   r   pandas.io.formats.formatr   pandas.io.formats.printingr   pandas._typingr   r   r   r   r    r!   r7   rN   r  r/   r/   r/   r0   <module>   s>       