o
    gAq                     @  s8  d Z ddlmZ ddlmZ ddlZddlZddlmZm	Z	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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#Z# ddl$m%  m&Z' ddl(m)Z) ddl*m+Z+m,Z, ddl-m.Z. ddl/m0Z0 G dd dZ1G dd dZ2G dd dZ3dS )zC
Utilities for conversion to writer-agnostic Excel representation.
    )annotations)reduceN)CallableHashableIterableMappingSequencecast)is_list_like)
IndexLabelStorageOptions)doc)missing)is_float	is_scalar)	DataFrameIndex
MultiIndexPeriodIndex)generic)CSS4_COLORS)CSSResolver
CSSWarning)get_level_lengths)pprint_thingc                   @  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"   c                 C  s(   || _ || _|| _|| _|| _|| _d S Nr   )selfr   r   r   r    r!   r"    r'   U/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/pandas/io/formats/excel.py__init__8   s   	
zExcelCell.__init__)NNN)r   r#   r   r#   r!   r$   r"   r$   )__name__
__module____qualname__
__fields__	__slots__r)   r'   r'   r'   r(   r   4   s    r   c                   @  sv  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e
d< d^d_ddZe Zd`ddZdaddZdbd d!Zdcd"d#Zddd%d&Zded(d)Zdfd,d-Zdgd/d0Zdhd2d3Zdid5d6Zdjd7d8Zdkd:d;Zdld=d>Zddd?d@ZdddAdBZdmdDdEZdndGdHZdddIdJZdmdKdLZ dodNdOZ!dpdQdRZ"dqdTdUZ#drdXdYZ$dsdZd[Z%drd\d]Z&dS )t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)r0   ztext-topmiddlebaseliner2   ztext-bottomTF)boldbolder600700800900normallighter100200300400500)r;   italicoblique            )serifz
sans-serifcursivefantasyzdict[str, str] | None	inheritedN
str | Nonec                 C  s"   |d ur|  || _d S d | _d S r%   )compute_cssrK   )r&   rK   r'   r'   r(   r)      s   
zCSSToExcelConverter.__init__declarations_strstrreturndict[str, dict[str, str]]c                 C  s   |  || j}| |S )ax  
        Convert CSS declarations to ExcelWriter style.

        Parameters
        ----------
        declarations_str : str
            List of CSS declarations.
            e.g. "font-weight: bold; background: blue"

        Returns
        -------
        xlstyle : dict
            A style as interpreted by ExcelWriter when found in
            ExcelCell.style.
        )rM   rK   build_xlstyle)r&   rN   
propertiesr'   r'   r(   __call__   s   
zCSSToExcelConverter.__call__propsMapping[str, str]c                   sH   |  || || || || |d}d fdd  | |S )	N)	alignmentborderfillfontnumber_formatddict[str, str]rP   Nonec                   sF   t |  D ]\}}|du r| |= qt|tr  | |s | |= qdS )z4Remove key where value is None, through nested dictsN)listitems
isinstancedict)r\   kvremove_noner'   r(   rf      s   
z6CSSToExcelConverter.build_xlstyle.<locals>.remove_none)r\   r]   rP   r^   )build_alignmentbuild_border
build_fill
build_fontbuild_number_format)r&   rU   outr'   re   r(   rR      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&   rU   r'   r'   r(   rg      s   z#CSSToExcelConverter.build_alignmentc                 C     | d}|r| j |S d S )Nzvertical-align)rq   VERTICAL_MAP)r&   rU   vertical_alignr'   r'   r(   rr         
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)rq   boolrt   r'   r'   r(   rs      s   z%CSSToExcelConverter._get_is_wrap_text dict[str, dict[str, str | None]]c                   s    fdddD S )Nc                   sP   i | ]$}|  d | d d | d d | ddqS )zborder-z-stylez-widthz-color)r    color)_border_stylerq   color_to_excel).0siderU   r&   r'   r(   
<dictcomp>   s    z4CSSToExcelConverter.build_border.<locals>.<dictcomp>r0   rightr2   leftr'   rt   r'   r   r(   rh      s   z CSSToExcelConverter.build_borderr    widthc                 C  s   |d u r
|d u r
d S |dks|dkrd S |  |}|d u rd S |dv r%|S |dkr+dS |dkr7|dv r5dS dS |dkrC|dv rAdS d	S d S )
Nnonehidden)Ngrooveridgeinsetoutsetsoliddoubledotted)hairthinmediumDashDotDotdashedmediumDashed)_get_width_name)r&   r    r   
width_namer'   r'   r(   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'   r(   r     s   
z#CSSToExcelConverter._get_width_namefloatc                 C  s   |d u rd}|  |S )N2pt)_pt_to_float)r&   r   r'   r'   r(   r     s   
z#CSSToExcelConverter._width_to_float	pt_stringc                 C  s   | dsJ t|dS )Npt)endswithr   rstrip)r&   r   r'   r'   r(   r     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)rq   r   )r&   rU   
fill_colorr'   r'   r(   ri     s   
zCSSToExcelConverter.build_filldict[str, str | None]c                 C  s   d| diS )Nformat_codeznumber-format)rq   rt   r'   r'   r(   rk         z'CSSToExcelConverter.build_number_format*dict[str, bool | int | 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sizer5   rB   r   striker~   shadow)	_get_font_names_get_decoration_select_font_family_get_font_size_get_is_bold_get_is_italicr   rq   _get_shadow)r&   rU   
font_names
decorationr'   r'   r(   rj   #  s   


zCSSToExcelConverter.build_fontc                 C  ru   )Nzfont-weight)rq   BOLD_MAP)r&   rU   weightr'   r'   r(   r   ;  rx   z CSSToExcelConverter._get_is_boldc                 C  ru   )Nz
font-style)rq   
ITALIC_MAP)r&   rU   
font_styler'   r'   r(   r   A  rx   z"CSSToExcelConverter._get_is_italicSequence[str]c                 C  s   | d}|d ur| S dS )Nztext-decorationr'   )rq   split)r&   rU   r   r'   r'   r(   r   G  s   
z#CSSToExcelConverter._get_decorationr   c                 C  s   d|v rdS d S )Nr   r   r'   )r&   r   r'   r'   r(   _get_underlineN  s   z"CSSToExcelConverter._get_underlinec                 C  s    d|v rt td|d S d S )Nztext-shadowz^[^#(]*[1-9])r|   researchrt   r'   r'   r(   r   S  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 rD   "z\"'z\')r   findallrq   replacestripappend)r&   rU   font_names_tmpr   r   r'   r'   r(   r   X  s   
	
z#CSSToExcelConverter._get_font_namesfloat | Nonec                 C  s    | d}|d u r|S | |S )Nz	font-size)rq   r   )r&   rU   r   r'   r'   r(   r   r  s   

z"CSSToExcelConverter._get_font_sizer$   c                 C  s(   d }|D ]}| j |}|r |S q|S r%   )
FAMILY_MAPrq   )r&   r   r   r   r'   r'   r(   r   x  s   z'CSSToExcelConverter._select_font_familyr   c                 C  sX   |d u rd S |  |r| |S z| j| W S  ty+   tdt| t Y d S w )NzUnhandled color format: )_is_hex_color_convert_hex_to_excelNAMED_COLORSKeyErrorwarningswarnreprr   r&   r   r'   r'   r(   r     s   

z"CSSToExcelConverter.color_to_excelcolor_stringr|   c                 C  s   t |dS )N#)r|   
startswith)r&   r   r'   r'   r(   r     r   z!CSSToExcelConverter._is_hex_colorc                 C  sD   | d}| |r|d d |d d  |d d   S | S )Nr   r   rE   rD   )lstrip_is_shorthand_colorupperr&   r   coder'   r'   r(   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'   r(   r     s   
z'CSSToExcelConverter._is_shorthand_colorr%   )rK   rL   )rN   rO   rP   rQ   )rU   rV   rP   rQ   )rU   rV   rP   rm   )rU   rV   rP   rL   )rU   rV   rP   ry   )rU   rV   rP   r}   )r    rL   r   rL   )r   rL   rP   rL   )r   rL   rP   r   )r   rO   rP   r   )rU   rV   )rU   rV   rP   r   )rU   rV   rP   r   )rU   rV   rP   r   )r   r   rP   rL   )rU   rV   rP   r   )rP   r$   )r   rL   rP   rL   )r   rO   rP   r|   )r   rO   rP   rO   )'r*   r+   r,   __doc__r   r   rv   r   r   r   __annotations__r)   r   rM   rT   rR   rg   rr   rs   rh   r   r   r   r   ri   rk   rj   r   r   r   r   r   r   r   r   r   r   r   r   r'   r'   r'   r(   r/   I   sz   
 







'














	

r/   c                   @  s   e Zd ZdZdZdZ									d<d=ddZedd Zdd Z	d>d!d"Z
d>d#d$Zd>d%d&Zd>d'd(Zd>d)d*Zd>d+d,Zed?d-d.Zd@d1d2Zd>d3d4Zeejd5 d6	7	8	8			dAdBd:d;ZdS )C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_reprO   float_formatrL   colsSequence[Hashable] | NoneheaderSequence[Hashable] | boolindexr|   index_labelIndexLabel | Nonemerge_cellsinf_repstyle_converterCallable | Nonec                 C  s   d| _ || _t|ts|| _|j}|
d u rt }
|
| _nd | _|| _|d urOt	t
||js4tdt	t
||jt	t|krHt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   ra   r   stylerdatar/   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'   r(   r)     s.   
 

zExcelFormatter.__init__c                 C  s    ddidddddddddS )	Nr5   Tr   r   r1   r0   )rn   ro   )rZ   bordersrW   r'   r&   r'   r'   r(   header_style  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'   r(   _format_value  s   


zExcelFormatter._format_valuerP   Iterable[ExcelCell]c              
   c  sz   | j jdkr| jstd| js| jsd S | j }|j| jd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  qFtt||j|jD ]7\}\}}}	||	}
| D ]&\}}|dk}t||| d |
| | j|r|nd |r|| | nd dV  qoq_n tt| D ]\}}
dtt|
}t||| d || jV  q|| _d S )	NrD   z]Writing to Excel with MultiIndex columns and no index ('index'=False) is not yet implemented.Fsparsifyadjoinnamesr   r   r   r   r    r   .)r   nlevelsr   NotImplementedError_has_aliasesr   formatr   r   ra   r   r   r   	enumerater  r   r   ziplevelscodestaker`   joinmapr   r   )r&   r   
level_strslevel_lengths	coloffsetlnumr   spansr  level_codesvaluesispan_valspans_multiple_cellsrd   r'   r'   r(   _format_header_mi  sZ   





z ExcelFormatter._format_header_mic                 c  s    | j s| jr]d}| jrd}t| jjtrt| jjd }| j}| j rHtt	| j| _t| jt| jkrEt
dt| j dt| j d| j}t|D ]\}}t| j|| || jV  qLd S d S )Nr   rD   zWriting z cols but got z aliases)r  r   r   ra   r   r   r   r   r	   r   r   r  r   r   r   )r&   r  colnamescolindexcolnamer'   r'   r(   _format_header_regularM  s.   z%ExcelFormatter._format_header_regularc                   s   t  jtr  }n  }d} jjjrFdd  jjjD dgt j  }t	dd t
dd |rF fdd	t|D }  jd
7  _t||S )Nr'   c                 S  s   g | ]
}|d ur
|ndqS Nr   r'   )r   xr'   r'   r(   
<listcomp>n  s    z1ExcelFormatter._format_header.<locals>.<listcomp>r   c                 S  s   | o|S r%   r'   )r)  yr'   r'   r(   <lambda>q      z/ExcelFormatter._format_header.<locals>.<lambda>c                 S  s   | dkS r(  r'   )r)  r'   r'   r(   r,  q  r-  c                 3  s&    | ]\}}t  j|| jV  qd S r%   )r   r   r   )r   r%  r   r   r'   r(   	<genexpr>t  s
    
z0ExcelFormatter._format_header.<locals>.<genexpr>rD   )ra   r   r   r#  r'  r   r   r  r   r   r  r  r   	itertoolschain)r&   gengen2r   r'   r   r(   _format_headerf  s    


zExcelFormatter._format_headerc                 C  s   t | jjtr|  S |  S r%   )ra   r   r   r   _format_hierarchical_rows_format_regular_rowsr   r'   r'   r(   _format_body{  s   zExcelFormatter._format_bodyc                 c  s   | 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V  qnd}nd}| |E d H  d S )NrD   r   F)r  r   r   r   r   ra   r_   tuplenpndarrayr   rO   r   r  r   r   r   r   r   to_timestampr  _generate_body)r&   r   index_valuesidxidxvalr  r'   r'   r(   r5    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d}t|}t|| jjj| jjjD ]@\}}}	|j|	|j|jd}
| D ](\}}|dk}t| j| ||
| | j|r| j| | d nd |r|nd dV  q|d7 }qun#t| jj D ]}t|D ]\}}t| j| ||| jdV  q|d7 }q| |E d H  d S )	NrD   r   FTr  )
allow_fill
fill_valuer   r  )r  r   r   r   r   r  r   ra   r_   r7  r8  r9  r   r   r   r   comany_not_noner  r   r   r  r   r  r  r  r  _can_hold_na	_na_valuer`   r;  )r&   gcolidxindex_labelscidxr   r  r  r  r  r  r  r   r!  r"  indexcolvalsr=  indexcolvalr'   r'   r(   r4    sh   



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

z(ExcelFormatter._format_hierarchical_rowsc                 C  s
   t | jS )z1Whether the aliases for column names are present.)r
   r   r   r'   r'   r(   r    s   
zExcelFormatter._has_aliasesr  r#   c           	      c  s    | j d u r	d }n
| j  j}|sd }d }tt| jD ]9}| jjd d |f }t|D ](\}}|d urGd	dd |||f D }| 
|}t| j| || ||V  q,qd S )N;c                 s  s$    | ]\}}|d  t | V  qdS ):N)rO   )r   ard   r'   r'   r(   r.    s   " z0ExcelFormatter._generate_body.<locals>.<genexpr>)r   _computectxranger   r   r   ilocr  r  r   r   r   )	r&   r  stylesxlstylecolidxseriesr   r   cssr'   r'   r(   r;    s    

zExcelFormatter._generate_bodyc                 c  s4    t |  |  D ]}| |j|_|V  qd S r%   )r/  r0  r3  r6  r  r   )r&   cellr'   r'   r(   get_formatted_cells  s
   z"ExcelFormatter.get_formatted_cellsstorage_options)rX  Sheet1r   r   c              	   C  s   ddl m} | jj\}	}
|	| jks|
| jkr(td|	 d|
 d| j d| j |  }t||r4d}n	||||d}d}z|j	|||||d	 W |rQ|
  d
S d
S |rZ|
  w w )a#  
        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``, ``io.excel.xls.writer``,
            and ``io.excel.xlsm.writer``.

            .. deprecated:: 1.2.0

                As the `xlwt <https://pypi.org/project/xlwt/>`__ package is no longer
                maintained, the ``xlwt`` engine will be removed in a future
                version of pandas.

        {storage_options}

            .. versionadded:: 1.2.0
        r   )ExcelWriterz-This sheet is too large! Your sheet size is: z, z Max sheet size is: F)enginerX  T)startrowstartcolfreeze_panesN)pandas.io.excelrZ  r   shapemax_rowsmax_colsr   rW  ra   write_cellsclose)r&   writer
sheet_namer\  r]  r^  r[  rX  rZ  num_rowsnum_colsformatted_cells	need_saver'   r'   r(   write  s<   &
	
zExcelFormatter.write)	r   NNTTNFr   N)r   rO   r   rL   r   r   r   r   r   r|   r   r   r   r|   r   rO   r   r   )rP   r  )rP   r|   )r  r#   rP   r  )rY  r   r   NNN)rX  r   )r*   r+   r,   r   ra  rb  r)   propertyr   r  r#  r'  r3  r6  r5  r4  r  r;  rW  r   r   _shared_docsrk  r'   r'   r'   r(   r     sF     ,


8



&J

r   )4r   
__future__r   	functoolsr   r/  r   typingr   r   r   r   r   r	   r   numpyr8  pandas._libs.libr
   pandas._typingr   r   pandas.util._decoratorsr   pandas.core.dtypesr   pandas.core.dtypes.commonr   r   pandasr   r   r   r   pandas.corer   pandas.core.commoncorecommonrA  pandas.io.formats._color_datar   pandas.io.formats.cssr   r   pandas.io.formats.formatr   pandas.io.formats.printingr   r   r/   r   r'   r'   r'   r(   <module>   s2       _