a
    GBì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yb   dZY n0 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   sn   t | dƒ}|s`tj | ¡d  ¡ }|tvr`|dkr8d}n |dkrFd}n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   )ÚfilenameZis_file_likeZfile_formatÚmsgÚarchive© r7   úT/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/openpyxl/reader/excel.pyÚ_validate_archiveB   s     	
ÿý
r9   c                 C   sj   t tttg}|D ]}|  |¡}|r|  S qdd„ | jD ƒ}|t|ƒ@ }|r^tdt | 	¡ ƒS t
dƒ‚d S )Nc                 S   s   h | ]
}|j ’qS r7   )ÚContentType)Ú.0Úpr7   r7   r8   Ú	<setcomp>k   ó    z&_find_workbook_part.<locals>.<setcomp>ú/z$File contains no valid workbook part)r   r   r   r   ÚfindZDefaultÚsetr   r   ÚpopÚIOError)ÚpackageZworkbook_typesÚctÚpartÚdefaultsZworkbook_typer7   r7   r8   Ú_find_workbook_partc   s    

rH   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)
r9   r6   ÚnamelistÚvalid_filesÚ	read_onlyÚkeep_vbaÚ	data_onlyÚ
keep_linksÚ	rich_textÚshared_strings)ÚselfÚfnrM   rN   rO   rP   rQ   r7   r7   r8   Ú__init__y   s    
zExcelReader.__init__c                 C   s$   | j  t¡}t|ƒ}t |¡| _d S rJ   )r6   r)   r   r'   r   Ú	from_treerD   )rS   ÚsrcÚrootr7   r7   r8   Úread_manifest…   s    zExcelReader.read_manifestc                 C   sj   | j  t¡}t}| jrt}|d urf|jdd … }| j |¡�}||ƒ| _	W d   ƒ n1 s\0    Y  d S )Nr   )
rD   r@   r   r   rQ   r   ÚPartNamer6   ÚopenrR   )rS   rE   ÚreaderZstrings_pathrW   r7   r7   r8   Ú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r˜ttƒ dtƒ|_| jD ]}|j || j |¡¡ q|| jr¦| j|_|| _d S )Nr   )rP   Úa)rH   rD   r   r6   rZ   rP   ÚparserÚparseÚwbÚ_sheetsrO   Z
_data_onlyrM   Z
_read_onlyr:   r   r   ÚtemplaterN   r   r   r   Úvba_archiverL   Úwritestrr)   Z_archive)rS   Zwb_partra   Únamer7   r7   r8   Úread_workbook–   s    


zExcelReader.read_workbookc                 C   s,   t | jv r(t| j t ¡ƒ}t |¡| j_d S rJ   )	r	   rL   r'   r6   r)   r   rV   ra   Ú
properties©rS   rW   r7   r7   r8   Úread_properties­   s    
zExcelReader.read_propertiesc                 C   s,   t | jv r(t| j t ¡ƒ}t |¡| j_d S rJ   )	r
   rL   r'   r6   r)   r   rV   ra   Zcustom_doc_propsri   r7   r7   r8   Úread_custom³   s    
zExcelReader.read_customc                 C   s   t | jv r| j t ¡| j_d S rJ   )r   rL   r6   r)   ra   Zloaded_theme)rS   r7   r7   r8   Ú
read_theme¹   s    
zExcelReader.read_themec                 C   sÌ   |j }t|ƒ}g }|| jv r(t| j|ƒ}| j |d¡�}| ¡ }W d   ƒ n1 sT0    Y  t|ƒ}t 	|¡}	| j
|	_|j|	_| j
 |	¡ | tj¡}
|
D ]*}t| j|j ƒ\}}|D ]}|	 |¡ q¶qœd S )Nr,   )Útargetr!   rL   r    r6   r[   r)   r'   r$   rV   ra   Z_parentrf   ÚtitleZ
_add_sheetr@   r&   Ú	_rel_typer(   Ú	add_chart)rS   ÚsheetÚrelZ
sheet_pathÚ	rels_pathÚrelsrW   ÚxmlÚnodeÚcsÚdrawingsÚchartsÚimagesÚcr7   r7   r8   Úread_chartsheet¾   s"    
&
zExcelReader.read_chartsheetc                 C   sš  d}| j  ¡ D �]„\}}|j| jvr&qd|jv r>|  ||¡ qt|jƒ}tƒ }|| jv rdt| j	|ƒ}| j
ršt| j|j|j| jƒ}|j|_| jj |¡ qnH| j	 |j¡}| j |j¡}||_t||| j| j| jƒ}| ¡  | ¡  | t¡D ]†}	| j	 |	j¡}
t t |
ƒ¡}|j!D ]^\}}z||| _"W nD t#�yl   || }t$|t%ƒ�rht& '| (|j)|j*¡¡ Y �qY n0 �qqì| jj+�r˜|j,�r˜| -|j,¡j|_,nd |_,|j.D ].}| j	 |¡}
t |
ƒ}t/ |¡}| 0|¡ �q¤| t1j2¡}|D ]L}t3| j	|jƒ\}}|D ]}| 4||j5¡ �qþ|D ]}| 6||j5¡ �q�qä| t7j8¡}| j j9}|D ]@}	|	j:}| j	 |¡}
t |
ƒ}t7 |¡}||j; |_<| =|¡ �qJ|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.Z
chartsheet)>r_   Zfind_sheetsrm   rL   ÚTyper|   r!   r   r    r6   rM   r"   ra   rf   rR   ÚstateZsheet_staterb   Úappendr[   Zcreate_sheetZ_relsr#   rO   rQ   Zbind_allÚcloser@   r   r)   r   rV   r'   ÚcommentsÚcommentÚAttributeErrorÚ
isinstancer   ÚwarningsÚwarnÚformatrn   Z
coordinaterd   Zlegacy_drawingÚgetZtablesr%   Z	add_tabler&   ro   r(   rp   ÚanchorZ	add_imager   Zrel_typeÚpivot_cachesZTargetZcacheIdÚcacheZ	add_pivot)rS   Zcomment_warningrq   rr   rs   rt   ÚwsÚfhZ	ws_parserr,   rW   Zcomment_sheetÚrefr‚   r{   Útru   Útablerx   ry   rz   ZimZ	pivot_relrŠ   Z
pivot_pathÚtreeZpivotr7   r7   r8   Úread_worksheetsÔ   st    





zExcelReader.read_worksheetsc              
   C   sÎ   d}z„|   ¡  d}|  ¡  d}|  ¡  d}|  ¡  d}|  ¡  d}|  ¡  d}t| j| jƒ d}|  	¡  d	}| j
 ¡  | js†| j ¡  W n@ tyÈ } z(td
|› d| jj› d�ƒ|‚W Y d }~n
d }~0 0 d S )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.)rY   r]   rg   rj   rk   rl   r   r6   ra   r’   r_   Zassign_namesrM   r€   Ú
ValueErrorr4   )rS   ÚactionÚer7   r7   r8   r)     s6    
ÿüzExcelReader.readN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rU   rY   r]   rg   rj   rk   rl   r|   r’   r)   r7   r7   r7   r8   rI   s   s   ÿ
JrI   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.

    )rI   r)   ra   )r4   rM   rN   rO   rP   rQ   r\   r7   r7   r8   Úload_workbook<  s
    ÿrš   )Er™   Úzipfiler   r   Úior   Úos.pathr.   r…   Zopenpyxl.pivot.tabler   Ztestsr   ÚImportErrorZopenpyxl.utils.exceptionsr   Zopenpyxl.xml.constantsr	   r
   r   r   r   r   r   r   r   r   r   Zopenpyxl.cellr   Zopenpyxl.comments.comment_sheetr   Ústringsr   r   Zworkbookr   Zopenpyxl.styles.stylesheetr   Zopenpyxl.packaging.corer   Zopenpyxl.packaging.customr   Zopenpyxl.packaging.manifestr   r   Zopenpyxl.packaging.relationshipr   r    r!   Zopenpyxl.worksheet._read_onlyr"   Zopenpyxl.worksheet._readerr#   Zopenpyxl.chartsheetr$   Zopenpyxl.worksheet.tabler%   Z$openpyxl.drawing.spreadsheet_drawingr&   Zopenpyxl.xml.functionsr'   rx   r(   r2   r9   rH   rI   rš   r7   r7   r7   r8   Ú<module>   sF   
4! Jÿ