o
    gE0                     @   s  d Z ddlmZmZ ddlmZ ddlZddlZddl	m
Z
 zddlmZ W n ey1   dZY nw dd	lmZ dd
lmZmZmZmZmZmZmZmZmZ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$ 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/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z? dZ@dd ZAdd  ZBG d!d" d"ZCdedd#dfd$d%ZDdS )&zRead an xlsx file into Python    )ZipFileZIP_DEFLATED)BytesION)TableDefinition   )KEEP_VBAF)InvalidFileException)ARC_CORE
ARC_CUSTOMARC_CONTENT_TYPESARC_WORKBOOK	ARC_THEMECOMMENTS_NSSHARED_STRINGSXLTMXLTXXLSMXLSX)
MergedCell)CommentSheet   )read_string_tableread_rich_text)WorkbookParser)apply_stylesheet)DocumentProperties)CustomPropertyList)ManifestOverride)RelationshipListget_dependentsget_rels_path)ReadOnlyWorksheet)WorksheetReader)
Chartsheet)Table)SpreadsheetDrawing)
fromstring)find_images)z.xlsxz.xlsmz.xltxz.xltmc                 C   sz   t | d}|s6tj| d  }|tvr6|dkrd}t||dkr)d}t|d|dtf }t|t| d	}|S )
a  
    Does a first check whether filename is a string or a file-like
    object. If it is a string representing a filename, a check is done
    for supported formats by checking the given file-extension. If the
    file-extension is not in SUPPORTED_FORMATS an InvalidFileException
    will raised. Otherwise the filename (resp. file-like object) will
    forwarded to zipfile.ZipFile returning a ZipFile-Instance.
    readz.xlszopenpyxl does not support the old .xls file format, please use xlrd to read this file, or convert it to the more recent .xlsx file format.z.xlsbz|openpyxl does not support binary format .xlsb, please convert this file to .xlsx format if you want to open it with openpyxlzropenpyxl does not support %s file format, please check you can open it with Excel first. Supported formats are: %s,r)	hasattrospathsplitextlowerSUPPORTED_FORMATSjoinr   r   )filenameis_file_likefile_formatmsgarchive r9   O/var/www/html/api-tag/env/lib/python3.10/site-packages/openpyxl/reader/excel.py_validate_archiveB   s$   
		
r;   c                 C   sf   t tttg}|D ]}| |}|r|  S qdd | jD }|t|@ }|r/tdt |	 S t
d)Nc                 S   s   h | ]}|j qS r9   )ContentType).0pr9   r9   r:   	<setcomp>k   s    z&_find_workbook_part.<locals>.<setcomp>/z$File contains no valid workbook part)r   r   r   r   findDefaultsetr   r   popIOError)packageworkbook_typesctpartdefaultsworkbook_typer9   r9   r:   _find_workbook_partc   s   
rL   c                   @   sl   e Zd ZdZdedddfddZdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )ExcelReaderzQ
    Read an Excel package and dispatch the contents to the relevant modules
    FTc                 C   s>   t || _| j | _|| _|| _|| _|| _|| _g | _	d S N)
r;   r8   namelistvalid_files	read_onlykeep_vba	data_only
keep_links	rich_textshared_strings)selffnrQ   rR   rS   rT   rU   r9   r9   r:   __init__y   s   

zExcelReader.__init__c                 C   s$   | j t}t|}t|| _d S rN   )r8   r)   r   r'   r   	from_treerF   )rW   srcrootr9   r9   r:   read_manifest   s   zExcelReader.read_manifestc                 C   sp   | j t}t}| jrt}|d ur6|jdd  }| j|}||| _	W d    d S 1 s/w   Y  d S d S )Nr   )
rF   rA   r   r   rU   r   PartNamer8   openrV   )rW   rH   readerstrings_pathr[   r9   r9   r:   read_strings   s   "zExcelReader.read_stringsc                 C   s   t | j}t| j|jdd  | jd| _| j  | jj}g |_	| j
|_| j|_|jttfv |_| jrLtt dt|_| jD ]}|j|| j| q>| jrS| j|_|| _d S )Nr   )rT   a)rL   rF   r   r8   r^   rT   parserparsewb_sheetsrS   
_data_onlyrQ   
_read_onlyr<   r   r   templaterR   r   r   r   vba_archiverP   writestrr)   _archive)rW   wb_partrf   namer9   r9   r:   read_workbook   s   



zExcelReader.read_workbookc                 C   0   t | jv rt| jt }t|| j_d S d S rN   )	r	   rP   r'   r8   r)   r   rZ   rf   
propertiesrW   r[   r9   r9   r:   read_properties      
zExcelReader.read_propertiesc                 C   rq   rN   )	r
   rP   r'   r8   r)   r   rZ   rf   custom_doc_propsrs   r9   r9   r:   read_custom   ru   zExcelReader.read_customc                 C   s"   t | jv r| jt | j_d S d S rN   )r   rP   r8   r)   rf   loaded_theme)rW   r9   r9   r:   
read_theme   s   
zExcelReader.read_themec                 C   s   |j }t|}g }|| jv rt| j|}| j|d}| }W d    n1 s*w   Y  t|}t	|}	| j
|	_|j|	_| j
|	 |tj}
|
D ]}t| j|j \}}|D ]}|	| q[qNd S )Nr,   )targetr!   rP   r    r8   r_   r)   r'   r$   rZ   rf   _parentro   title
_add_sheetrA   r&   	_rel_typer(   	add_chart)rW   sheetrel
sheet_path	rels_pathrelsr[   xmlnodecsdrawingschartsimagescr9   r9   r:   read_chartsheet   s(   


zExcelReader.read_chartsheetc                 C   s  d}| j  D ]7\}}|j| jvrqd|jv r| || qt|j}t }|| jv r2t| j	|}| j
rLt| j|j|j| j}|j|_| jj| q| j	|j}| j|j}||_t||| j| j| j}|  |  |tD ]>}	| j	|	j}
tt |
}|j!D ]*\}}z||| _"W q t#y   || }t$|t%rt&'|(|j)|j* Y qY qw qu| jj+r|j,r|-|j,j|_,nd |_,|j.D ]}| j	|}
t |
}t/|}|0| q|t1j2}|D ]$}t3| j	|j\}}|D ]	}|4||j5 q|D ]
}|6||j5 qq|t7j8}| j j9}|D ] }	|	j:}| j	|}
t |
}t7|}||j; |_<|=| q|j|_qd S )Nz~Cell '{0}':{1} is part of a merged range but has a comment which will be removed because merged cells cannot contain any data.
chartsheet)>rd   find_sheetsrz   rP   Typer   r!   r   r    r8   rQ   r"   rf   ro   rV   statesheet_staterg   appendr_   create_sheet_relsr#   rS   rU   bind_allcloserA   r   r)   r   rZ   r'   commentscommentAttributeError
isinstancer   warningswarnformatr|   
coordinaterk   legacy_drawinggettablesr%   	add_tabler&   r~   r(   r   anchor	add_imager   rel_typepivot_cachesTargetcacheIdcache	add_pivot)rW   comment_warningr   r   r   r   wsfh	ws_parserr,   r[   comment_sheetrefr   r   tr   tabler   r   r   im	pivot_relr   
pivot_pathtreepivotr9   r9   r:   read_worksheets   s~   








zExcelReader.read_worksheetsc              
   C   s   d}zF|    d}|   d}|   d}|   d}|   d}|   d}t| j| j d}| 	  d	}| j
  | jsF| j  W d S W d S  tyb } ztd
| d| jj d|d }~ww )Nzread manifestzread stringszread workbookzread propertieszread custom propertiesz
read themezread stylesheetzread worksheetszassign namesz#Unable to read workbook: could not z from z~.
This is most probably because the workbook source files contain some invalid XML.
Please see the exception for more details.)r]   rb   rp   rt   rw   ry   r   r8   rf   r   rd   assign_namesrQ   r   
ValueErrorr4   )rW   actioner9   r9   r:   r)     s<   
zExcelReader.readN)__name__
__module____qualname____doc__r   rY   r]   rb   rp   rt   rw   ry   r   r   r)   r9   r9   r9   r:   rM   s   s    
JrM   Tc                 C   s    t | |||||}|  |jS )aE  Open the given filename and return the workbook

    :param filename: the path to open or a file-like object
    :type filename: string or a file-like object open in binary mode c.f., :class:`zipfile.ZipFile`

    :param read_only: optimised for reading, content cannot be edited
    :type read_only: bool

    :param keep_vba: preserve vba content (this does NOT mean you can use it)
    :type keep_vba: bool

    :param data_only: controls whether cells with formulae have either the formula (default) or the value stored the last time Excel read the sheet
    :type data_only: bool

    :param keep_links: whether links to external workbooks should be preserved. The default is True
    :type keep_links: bool

    :param rich_text: if set to True openpyxl will preserve any rich text formatting in cells. The default is False
    :type rich_text: bool

    :rtype: :class:`openpyxl.workbook.Workbook`

    .. note::

        When using lazy load, all worksheets will be :class:`openpyxl.worksheet.iter_worksheet.IterableWorksheet`
        and the returned workbook will be read-only.

    )rM   r)   rf   )r4   rQ   rR   rS   rT   rU   r`   r9   r9   r:   load_workbook<  s
   r   )Er   zipfiler   r   ior   os.pathr.   r   openpyxl.pivot.tabler   testsr   ImportErroropenpyxl.utils.exceptionsr   openpyxl.xml.constantsr	   r
   r   r   r   r   r   r   r   r   r   openpyxl.cellr   openpyxl.comments.comment_sheetr   stringsr   r   workbookr   openpyxl.styles.stylesheetr   openpyxl.packaging.corer   openpyxl.packaging.customr   openpyxl.packaging.manifestr   r   openpyxl.packaging.relationshipr   r    r!   openpyxl.worksheet._read_onlyr"   openpyxl.worksheet._readerr#   openpyxl.chartsheetr$   openpyxl.worksheet.tabler%   $openpyxl.drawing.spreadsheet_drawingr&   openpyxl.xml.functionsr'   r   r(   r2   r;   rL   rM   r   r9   r9   r9   r:   <module>   sJ   4! J