o
    ðäjE0  ã                   @   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.
    Úreadéÿÿÿÿz.xlszŠopenpyxl 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   úY/var/www/ticemtrilhas/avaliacao_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%ƒr°t& '| (|j)|j*¡¡ Y qˆY qˆw 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ÿ