U
    rÎ·hþ/  ã                   @   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k
rd   dZY nX 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k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   ú9/tmp/pip-unpacked-wheel-49e_hfpc/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   s     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    

rG   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ÚfnrL   rM   rN   rO   rP   r7   r7   r8   Ú__init__y   s    
zExcelReader.__init__c                 C   s$   | j  t¡}t|ƒ}t |¡| _d S rI   )r6   r)   r   r'   r   Ú	from_treerC   )rR   ÚsrcÚrootr7   r7   r8   Úread_manifest…   s    zExcelReader.read_manifestc              	   C   sV   | j  t¡}t}| jrt}|d k	rR|jdd … }| j |¡�}||ƒ| _	W 5 Q R X d S )Nr   )
rC   r?   r   r   rP   r   ÚPartNamer6   ÚopenrQ   )rR   rD   ÚreaderZstrings_pathrV   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k|_| jr˜ttƒ dtƒ|_| jD ]}|j || j |¡¡ q|| jr¦| j|_|| _d S )Nr   )rO   Úa)rG   rC   r   r6   rY   rO   ÚparserÚparseÚwbÚ_sheetsrN   Z
_data_onlyrL   Z
_read_onlyr:   r   r   ÚtemplaterM   r   r   r   Úvba_archiverK   Úwritestrr)   Z_archive)rR   Zwb_partr`   Únamer7   r7   r8   Úread_workbook–   s    


zExcelReader.read_workbookc                 C   s,   t | jkr(t| j t ¡ƒ}t |¡| j_d S rI   )	r	   rK   r'   r6   r)   r   rU   r`   Z
properties©rR   rV   r7   r7   r8   Úread_properties­   s    
zExcelReader.read_propertiesc                 C   s,   t | jkr(t| j t ¡ƒ}t |¡| j_d S rI   )	r
   rK   r'   r6   r)   r   rU   r`   Zcustom_doc_propsrg   r7   r7   r8   Úread_custom³   s    
zExcelReader.read_customc                 C   s   t | jkr| j t ¡| j_d S rI   )r   rK   r6   r)   r`   Zloaded_theme)rR   r7   r7   r8   Ú
read_theme¹   s    
zExcelReader.read_themec              	   C   s¸   |j }t|ƒ}g }|| jkr(t| j|ƒ}| j |d¡�}| ¡ }W 5 Q R X t|ƒ}t 	|¡}	| j
|	_|j|	_| j
 |	¡ | tj¡}
|
D ]*}t| j|j ƒ\}}|D ]}|	 |¡ q¢qˆd S )Nr,   )Útargetr!   rK   r    r6   rZ   r)   r'   r$   rU   r`   Ú_parentre   ÚtitleZ
_add_sheetr?   r&   Ú	_rel_typer(   Ú	add_chart)rR   ÚsheetÚrelZ
sheet_pathÚ	rels_pathÚrelsrV   ÚxmlÚnodeÚcsÚdrawingsÚchartsÚimagesÚcr7   r7   r8   Úread_chartsheet¾   s"    

zExcelReader.read_chartsheetc                 C   sŽ  d}| j  ¡ D �]x\}}|j| jkr&qd|jkr>|  ||¡ qt|jƒ}tƒ }|| jkrdt| j	|ƒ}| j
ršt| j|j|j| jƒ}|j|_| jj |¡ qn@| j	 |j¡}| j |j¡}||_t||| j| j| jƒ}| ¡  | t¡D ]ˆ}	| j	 |	j¡}
t t|
ƒ¡}|j D ]`\}}z||| _!W nF t"k
�rf   || }t#|t$ƒ�rbt% &| '|j(|j)¡¡ Y �q
Y nX �q
qä| jj*�r�|j+�r�||j+ j|_+nd |_+|j,D ].}| j	 |¡}
t|
ƒ}t- |¡}| .|¡ �qœ| t/j0¡}|D ]L}t1| j	|jƒ\}}|D ]}| 2||j3¡ �qö|D ]}| 4||j3¡ �q�qÜ| t5j6¡}|D ]D}	|	j7}| j	 |¡}
t|
ƒ}t5 |¡}| j j8|j9 |_:| ;|¡ �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.Z
chartsheet)<r^   Zfind_sheetsrk   rK   ÚTyper{   r!   r   r    r6   rL   r"   r`   re   rQ   ÚstateZsheet_statera   ÚappendrZ   Zcreate_sheetZ_relsr#   rN   rP   Zbind_allr?   r   r)   r   rU   r'   ÚcommentsÚcommentÚAttributeErrorÚ
isinstancer   ÚwarningsÚwarnÚformatrm   Z
coordinaterc   Zlegacy_drawingZtablesr%   Z	add_tabler&   rn   r(   ro   ÚanchorZ	add_imager   Zrel_typeZTargetZpivot_cachesZcacheIdÚcacheZ	add_pivot)rR   Zcomment_warningrp   rq   rr   rs   ÚwsÚfhZ	ws_parserr,   rV   Zcomment_sheetÚrefr€   rz   Útrt   Útablerw   rx   ry   ZimZ	pivot_relZ
pivot_pathÚtreeZpivotr7   r7   r8   Úread_worksheetsÔ   sp    





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k
rÆ } z td
|› d| jj› d�ƒ|‚W 5 d }~X Y nX 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.)rX   r\   rf   rh   ri   rj   r   r6   r`   rŽ   r^   Zassign_namesrL   ÚcloseÚ
ValueErrorr4   )rR   ÚactionÚer7   r7   r8   r)     s6    
ÿüzExcelReader.readN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rT   rX   r\   rf   rh   ri   rj   r{   rŽ   r)   r7   r7   r7   r8   rH   s   s     ÿ
HrH   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.

    )rH   r)   r`   )r4   rL   rM   rN   rO   rP   r[   r7   r7   r8   Úload_workbook:  s      ÿr—   )Er–   Úzipfiler   r   Úior   Úos.pathr.   rƒ   Zopenpyxl.pivot.tabler   Ú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'   rw   r(   r2   r9   rG   rH   r—   r7   r7   r7   r8   Ú<module>   sJ   
4! H  ÿ