B
    P\q)                 @   s  d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	 d dl
Zd dlZd dlmZmZ d dlmZ ydd	lmZ W n ek
r   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"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- d dl.m/Z/m0Z0 d dl1m2Z2m3Z3m4Z4 d dl5m6Z6 d dl7m8Z8 d dl9m:Z: d dl;m<Z< d dl=m>Z> d dl?m@Z@ ddlAmBZB dZCdd ZDdd  ZEG d!d" d"ZFd
ed
d#fd$d%ZGdS )&    )absolute_import)ZipFileZIP_DEFLATED
BadZipfile)exc_info)BytesION)unicodefile)TableDefinition   )KEEP_VBAF)InvalidFileException)ARC_SHARED_STRINGSARC_COREARC_CONTENT_TYPESARC_WORKBOOK	ARC_THEMECOMMENTS_NSSHARED_STRINGSEXTERNAL_LINKXLTMXLTXXLSMXLSX)CommentSheet   )read_string_table)WorkbookParser)apply_stylesheet)DocumentProperties)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 r:   4lib/python3.7/site-packages/openpyxl/reader/excel.py_validate_archiveE   s    	

r<   c             C   sn   t tttg}x|D ]}| |}|r|S qW tdd | jD }|t|@ }|rbtdt |	 S t
dd S )Nc             s   s   | ]}|j V  qd S )N)ContentType).0pr:   r:   r;   	<genexpr>n   s    z&_find_workbook_part.<locals>.<genexpr>/z$File contains no valid workbook part)r   r   r   r   findsetZDefaultr!   r   popIOError)packageZworkbook_typesctpartdefaultsZworkbook_typer:   r:   r;   _find_workbook_partf   s    

rJ   c               @   sb   e Zd ZdZde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S )ExcelReaderzQ
    Read an Excel package and dispatch the contents to the relevant modules
    FTc             C   s8   t || _| j | _|| _|| _|| _|| _g | _d S )N)	r<   r9   Znamelistvalid_files	read_onlykeep_vba	data_only
keep_linksshared_strings)selffnrM   rN   rO   rP   r:   r:   r;   __init__|   s    
zExcelReader.__init__c             C   s$   | j t}t|}t|| _d S )N)r9   r,   r   r*   r    	from_treerF   )rR   srcrootr:   r:   r;   read_manifest   s    zExcelReader.read_manifestc          	   C   sH   | j t}|d k	rD|jdd  }| j|}t|| _W d Q R X d S )Nr   )rF   rB   r   PartNamer9   openr   rQ   )rR   rG   Zstrings_pathrV   r:   r:   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k|_| jrtt dt|_x$| jD ]}|j|| j| q~W | jr| j|_|| _d S )Nr   )rP   a)rJ   rF   r   r9   rY   rP   parserparsewb_sheetsrO   Z
_data_onlyrM   Z
_read_onlyr=   r   r   templaterN   r   r   r   vba_archiverL   Zwritestrr,   Z_archive)rR   Zwb_partr_   namer:   r:   r;   read_workbook   s    

zExcelReader.read_workbookc             C   s,   t | jkr(t| jt }t|| j_d S )N)	r   rL   r*   r9   r,   r   rU   r_   Z
properties)rR   rV   r:   r:   r;   read_properties   s    
zExcelReader.read_propertiesc             C   s   t | jkr| jt | j_d S )N)r   rL   r9   r,   r_   Zloaded_theme)rR   r:   r:   r;   
read_theme   s    
zExcelReader.read_themec          	   C   s   |j }t|}g }|| jkr(t| j|}| j|d}| }W d Q R X t|}t	|}	| j
|	_|j|	_| j
|	 |tj}
x6|
D ].}t| j|j \}}x|D ]}|	| qW qW d S )Nr/   )targetr$   rL   r#   r9   rZ   r,   r*   r'   rU   r_   Z_parentrc   titleZ
_add_sheetrB   r)   	_rel_typer+   	add_chart)rR   sheetrelZ
sheet_path	rels_pathrelsrV   xmlZnodeZcsdrawingschartsimagescr:   r:   r;   read_chartsheet   s"    



zExcelReader.read_chartsheetc             C   sL  xD| j  D ]4\}}|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| qn<| j	|j}| j|j}||_t||| j| j}|  xN|tD ]@}| j	|j}	tt|	}
x|
jD ]\}}||| _qW qW | jjr>|j r>||j  j|_ x8|j!D ].}| j	|}	t|	}t"|}|#| qFW |t$j%}x\|D ]T}t&| j	|j\}}x|D ]}|'||j( qW x|D ]}|)||j( qW qW |t*j+}xL|D ]D}|j,}| j	|}	t|	}t*|}| j j-|j. |_/|0| qW |j1|_2qW d S )NZ
chartsheet)3r]   Zfind_sheetsrg   rL   ZTypert   r$   r"   r#   r9   rM   r%   r_   rc   rQ   r`   appendrZ   Zcreate_sheetZ_relsr&   rO   Zbind_allrB   r   r,   r   rU   r*   Zcommentscommentrb   Zlegacy_drawingZtablesr(   Z	add_tabler)   ri   r+   rj   ZanchorZ	add_imager
   Zrel_typeZTargetZpivot_cachesZcacheIdcacheZ	add_pivotstateZsheet_state)rR   rk   rl   rm   rn   ZwsZfhZ	ws_parserr/   rV   Zcomment_sheetrefrv   tro   tablerp   rq   rr   rs   ZimZ	pivot_relZ
pivot_pathZtreeZpivotr:   r:   r;   read_worksheets   s^    








zExcelReader.read_worksheetsc             C   s\   |    |   |   |   |   t| j| j |   | j	
  | jsX| j  d S )N)rX   r[   rd   re   rf   r   r9   r_   r|   r]   Zassign_namesrM   close)rR   r:   r:   r;   r,     s    
zExcelReader.readN)__name__
__module____qualname____doc__r   rT   rX   r[   rd   re   rf   rt   r|   r,   r:   r:   r:   r;   rK   v   s   
>rK   Tc             C   s   t | ||||}|  |jS )a#  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: preseve vba content (this does NOT mean you can use it)
    :type keep_vba: bool

    :param guess_types: guess cell content type and do not read it from the file
    :type guess_types: 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

    :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.

    )rK   r,   r_   )r7   rM   rN   rO   rP   readerr:   r:   r;   load_workbook  s    r   )HZ
__future__r   Zzipfiler   r   r   sysr   ior   os.pathr1   warningsZopenpyxl.compatr   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   r   Zopenpyxl.comments.comment_sheetr   Zstringsr   Zworkbookr   Zopenpyxl.styles.stylesheetr   Zopenpyxl.packaging.corer   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*   rp   r+   r5   r<   rJ   rK   r   r:   r:   r:   r;   <module>   sD   
8! #