U
    \$d/                     @   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.
    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   )filenameZis_file_likeZfile_formatmsgarchive r7   9/tmp/pip-unpacked-wheel-dtxbr_v9/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rt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 ]}|	| qqd 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
rt| 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  