a
    b*                     @   s  d 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 zddlmZ W n eyr   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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+ ddl,m-Z-m.Z. ddl/m0Z0m1Z1m2Z2 ddl3m4Z4 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ZAdd ZBdd  ZCG d!d" d"ZDd	ed	d#fd$d%ZEdS )&zRead an xlsx file into Python    )ZipFileZIP_DEFLATED
BadZipfile)exc_info)BytesION)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)
MergedCell)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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.
    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 r8   e/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/openpyxl/reader/excel.py_validate_archiveC   s     	

r:   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 r8   )ContentType).0pr8   r8   r9   	<setcomp>l       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_typer8   r8   r9   _find_workbook_partd   s    

rI   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:   r7   namelistvalid_files	read_onlykeep_vba	data_only
keep_linksshared_strings)selffnrN   rO   rP   rQ   r8   r8   r9   __init__z   s    
zExcelReader.__init__c                 C   s$   | j t}t|}t|| _d S rK   )r7   r*   r   r(   r   	from_treerE   )rS   srcrootr8   r8   r9   read_manifest   s    zExcelReader.read_manifestc                 C   s\   | j t}|d urX|jdd  }| j|}t|| _W d    n1 sN0    Y  d S )Nr   )rE   rA   r   PartNamer7   openr   rR   )rS   rF   Zstrings_pathrW   r8   r8   r9   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rtt dt|_| jD ]}|j|| j| q|| jr| j|_|| _d S )Nr   )rQ   a)rI   rE   r   r7   rZ   rQ   parserparsewb_sheetsrP   Z
_data_onlyrN   Z
_read_onlyr;   r   r   templaterO   r   r   r   vba_archiverM   writestrr*   Z_archive)rS   Zwb_partr`   namer8   r8   r9   read_workbook   s    


zExcelReader.read_workbookc                 C   s,   t | jv r(t| jt }t|| j_d S rK   )	r   rM   r(   r7   r*   r   rV   r`   Z
properties)rS   rW   r8   r8   r9   read_properties   s    
zExcelReader.read_propertiesc                 C   s   t | jv r| jt | j_d S rK   )r   rM   r7   r*   r`   Zloaded_themerS   r8   r8   r9   
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 ]}|	| qqd S )Nr-   )targetr"   rM   r!   r7   r[   r*   r(   r%   rV   r`   _parentre   titleZ
_add_sheetrA   r'   	_rel_typer)   	add_chart)rS   sheetrelZ
sheet_path	rels_pathrelsrW   xmlnodecsdrawingschartsimagescr8   r8   r9   read_chartsheet   s"    
&
zExcelReader.read_chartsheetc                 C   s  d}| j  D ]r\}}|j| jvr&qd|jv r>| || qt|j}t }|| jv rdt| j	|}| j
rt| j|j|j| j}|j|_| jj| qn<| j	|j}| j|j}||_t||| j| j}|  |tD ]}	| j	|	j}
tt|
}|jD ]^\}}z||| _ W nD t!y`   || }t"|t#r\t$%|&|j'|j( Y qY n0 qq| jj)r|j*r||j* j|_*nd |_*|j+D ].}| j	|}
t|
}t,|}|-| q|t.j/}|D ]L}t0| j	|j\}}|D ]}|1||j2 q|D ]}|3||j2 q
q|t4j5}|D ]D}	|	j6}| j	|}
t|
}t4|}| j j7|j8 |_9|:| q4|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_sheetsrj   rM   Typerz   r"   r    r!   r7   rN   r#   r`   re   rR   stateZsheet_statera   appendr[   Zcreate_sheetZ_relsr$   rP   Zbind_allrA   r   r*   r   rV   r(   commentscommentAttributeError
isinstancer   warningswarnformatrl   Z
coordinaterc   Zlegacy_drawingZtablesr&   Z	add_tabler'   rm   r)   rn   anchorZ	add_imager   Zrel_typeZTargetZpivot_cachesZcacheIdcacheZ	add_pivot)rS   Zcomment_warningro   rp   rq   rr   wsfhZ	ws_parserr-   rW   Zcomment_sheetrefr   ry   trs   tablerv   rw   rx   ZimZ	pivot_relZ
pivot_pathtreeZpivotr8   r8   r9   read_worksheets   sp    





zExcelReader.read_worksheetsc                 C   s\   |    |   |   |   |   t| j| j |   | j	
  | jsX| j  d S rK   )rY   r\   rf   rg   ri   r   r7   r`   r   r^   Zassign_namesrN   closerh   r8   r8   r9   r*     s    
zExcelReader.readN)__name__
__module____qualname____doc__r	   rU   rY   r\   rf   rg   ri   rz   r   r*   r8   r8   r8   r9   rJ   t   s   
HrJ   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 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.

    )rJ   r*   r`   )r5   rN   rO   rP   rQ   readerr8   r8   r9   load_workbook   s
    r   )Fr   zipfiler   r   r   sysr   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   r   Zopenpyxl.cellr   Zopenpyxl.comments.comment_sheetr   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(   rv   r)   r3   r:   rI   rJ   r   r8   r8   r8   r9   <module>   sF   
8! -