U
    rÎ·h¸3  ã                   @   s¶  d 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 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 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, ddl-m.Z. ddl/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d"l@mAZAmBZBmCZCmDZD eEfZFG d#d$„ d$eGƒZHd%S )&zAWorkbook is the top-level container for all document information.é    )Úcopy)Ú
deprecated)Ú	Worksheet)ÚReadOnlyWorksheet)ÚWriteOnlyWorksheet)ÚWorksheetCopy)Úquote_sheetname)ÚIndexedList)ÚWINDOWS_EPOCHÚ	MAC_EPOCH)ÚReadOnlyWorkbookException)Úsave_workbook)Ú
StyleArray)Ú
NamedStyle)ÚDifferentialStyleList)Ú	Alignment)ÚDEFAULT_BORDER)ÚDEFAULT_EMPTY_FILLÚDEFAULT_GRAY_FILL)ÚDEFAULT_FONT)Ú
Protection)ÚCOLOR_INDEX)ÚNamedStyleList)ÚTableStyleList)Ú
Chartsheeté   )ÚDefinedNameÚDefinedNameDict)ÚDocumentProperties)ÚCustomPropertyList)ÚRelationshipList)Ú_WorkbookChild)ÚDocumentSecurity)ÚCalcProperties)ÚBookView)ÚXLSMÚXLSXÚXLTMÚXLTXc                   @   s°  e Zd ZdZdZdZdZdZdQdd„Zdd„ Z	e
dd	„ ƒZejd
d	„ ƒZe
dd„ ƒZe
dd„ ƒZe
dd„ ƒZe
dd„ ƒZe
dd„ ƒZejdd„ ƒZdRdd„ZdSdd„ZdTdd„Zdd„ Zed ƒd!d"„ ƒZdUd#d$„Zed%ƒd&d'„ ƒZd(d)„ Zd*d+„ Zed,ƒd-d.„ ƒZd/d0„ Zd1d2„ Zd3d4„ Zed5ƒd6d7„ ƒZ e
d8d9„ ƒZ!e
d:d;„ ƒZ"e
d<d=„ ƒZ#ed>ƒdVd?d@„ƒZ$dAdB„ Z%e
dCdD„ ƒZ&e
dEdF„ ƒZ'dGdH„ Z(e
dIdJ„ ƒZ)dKdL„ Z*dMdN„ Z+dOdP„ Z,dS )WÚWorkbookz>Workbook is the container for all other parts of the document.Fz/xl/workbook.xmlc                 C   s¬   g | _ g | _d| _tƒ | _g | _tƒ | _tƒ | _	t
ƒ | _|| _tƒ | _|  ¡  d | _d | _d| _d | _t| _d| _|| _| jsŽ| j  t| ƒ¡ tƒ | _tƒ | _tƒ g| _ d S )Nr   Fzutf-8)!Ú_sheetsZ_pivotsÚ_active_sheet_indexr   Údefined_namesZ_external_linksr   Ú
propertiesr   Zcustom_doc_propsr"   ÚsecurityÚ_Workbook__write_onlyr	   Zshared_stringsÚ_setup_stylesZloaded_themeÚvba_archiveZis_templateZ	code_namer
   ÚepochÚencodingÚ	iso_datesÚ
write_onlyÚappendr   r    Zrelsr#   Zcalculationr$   Úviews)Úselfr5   r4   © r9   ú>/tmp/pip-unpacked-wheel-49e_hfpc/openpyxl/workbook/workbook.pyÚ__init__;   s.    zWorkbook.__init__c                 C   sÄ   t ƒ | _| j t¡ t tƒ gƒ| _t ƒ | _| j t¡ t ƒ | _| j t	¡ | j t
¡ t ƒ | _i | _i | _t tƒ gƒ| _t| _t tƒ gƒ| _tƒ | _|  tttƒttƒdd�¡ tƒ | _tƒ | _dS )zBootstrap stylesr   )ÚfontZborderZ	builtinIdN)r	   Z_fontsÚaddr   r   Z_alignmentsZ_bordersr   Z_fillsr   r   Z_number_formatsZ_date_formatsZ_timedelta_formatsr   Z_protectionsr   Z_colorsr   Z_cell_stylesr   Ú_named_stylesÚadd_named_styler   r   r   Z_table_stylesr   Z_differential_styles©r8   r9   r9   r:   r0   \   s$    zWorkbook._setup_stylesc                 C   s   | j tkrtS tS ©N)Ú_epochr
   r   r@   r9   r9   r:   r2   y   s    
zWorkbook.epochc                 C   s   |t tfkrtdƒ‚|| _d S )Nz%The epoch must be either 1900 or 1904)r
   r   Ú
ValueErrorrB   )r8   Úvaluer9   r9   r:   r2   €   s    c                 C   s   | j S rA   )Ú
_read_onlyr@   r9   r9   r:   Ú	read_only‡   s    zWorkbook.read_onlyc                 C   s   | j S rA   )Ú
_data_onlyr@   r9   r9   r:   Ú	data_only‹   s    zWorkbook.data_onlyc                 C   s   | j S rA   )r/   r@   r9   r9   r:   r5   �   s    zWorkbook.write_onlyc                 C   s   | j S rA   )r2   r@   r9   r9   r:   Úexcel_base_date”   s    zWorkbook.excel_base_datec                 C   s(   z| j | j W S  tk
r"   Y nX dS )zoGet the currently active sheet or None

        :type: :class:`openpyxl.worksheet.worksheet.Worksheet`
        N)r*   r+   Ú
IndexErrorr@   r9   r9   r:   Úactive˜   s    zWorkbook.activec                 C   sd   t |ttfƒstdƒ‚t |tƒr*|| _dS || jkr<tdƒ‚|jdkrNtdƒ‚| j |¡}|| _dS )zSet the active sheetz?Value must be either a worksheet, chartsheet or numerical indexNz Worksheet is not in the workbookZvisiblez&Only visible sheets can be made active)	Ú
isinstancer!   ÚINTEGER_TYPESÚ	TypeErrorr+   r*   rC   Zsheet_stateÚindex)r8   rD   Úidxr9   r9   r:   rK   £   s    


Nc                 C   s@   | j rtdƒ‚| jr"t| |d�}nt| |d�}| j||d� |S )zåCreate a worksheet (at an optional index).

        :param title: optional title of the sheet
        :type title: str
        :param index: optional position at which the sheet will be inserted
        :type index: int

        ú/Cannot create new sheet in a read-only workbook©ÚparentÚtitle)ÚsheetrO   )rF   r   r5   r   r   Ú
_add_sheet)r8   rT   rO   Znew_wsr9   r9   r:   Úcreate_sheet¸   s    	zWorkbook.create_sheetc                 C   sR   t |tttfƒstdƒ‚|j| kr*tdƒ‚|dkr@| j |¡ n| j 	||¡ dS )z(Add an worksheet (at an optional index).zCannot be added to a workbookz0You cannot add worksheets from another workbook.N)
rL   r   r   r   rN   rS   rC   r*   r6   Úinsert)r8   rU   rO   r9   r9   r:   rV   Í   s    
zWorkbook._add_sheetr   c                 C   s@   t |tƒs| | }| j |¡}| j|= || }| j ||¡ dS )z+
        Move a sheet or sheetname
        N)rL   r   r*   rO   rX   )r8   rU   ÚoffsetrP   Únew_posr9   r9   r:   Ú
move_sheetÜ   s    
zWorkbook.move_sheetc                 C   s   | j  |¡}| j  |¡ dS ©z&Remove `worksheet` from this workbook.N)r*   rO   Úremove)r8   Ú	worksheetrP   r9   r9   r:   r]   è   s    zWorkbook.removez-Use wb.remove(worksheet) or del wb[sheetname]c                 C   s   |   |¡ dS r\   ©r]   ©r8   r^   r9   r9   r:   Úremove_sheetî   s    zWorkbook.remove_sheetc                 C   s*   | j rtdƒ‚t| |d�}|  ||¡ |S )NrQ   rR   )rF   r   r   rV   )r8   rT   rO   Úcsr9   r9   r:   Úcreate_chartsheetô   s
    zWorkbook.create_chartsheetzUse wb[sheetname]c                 C   s   | | S )ú�Returns a worksheet by its name.

        :param name: the name of the worksheet to look for
        :type name: string

        r9   )r8   Únamer9   r9   r:   Úget_sheet_by_nameý   s    zWorkbook.get_sheet_by_namec                 C   s
   || j kS rA   ©Ú
sheetnames)r8   Úkeyr9   r9   r:   Ú__contains__  s    zWorkbook.__contains__c                 C   s   | j  |¡S )z Return the index of a worksheet.)Ú
worksheetsrO   r`   r9   r9   r:   rO     s    zWorkbook.indexzUse wb.index(worksheet)c                 C   s
   |   |¡S )z"Return the index of the worksheet.)rO   r`   r9   r9   r:   Ú	get_index  s    zWorkbook.get_indexc                 C   s6   | j | j D ]}|j|kr|  S qtd |¡ƒ‚dS )rd   zWorksheet {0} does not exist.N)rk   ÚchartsheetsrT   ÚKeyErrorÚformat©r8   ri   rU   r9   r9   r:   Ú__getitem__  s    

zWorkbook.__getitem__c                 C   s   | | }|   |¡ d S rA   r_   rp   r9   r9   r:   Ú__delitem__!  s    zWorkbook.__delitem__c                 C   s
   t | jƒS rA   )Úiterrk   r@   r9   r9   r:   Ú__iter__%  s    zWorkbook.__iter__zUse wb.sheetnamesc                 C   s   | j S rA   rg   r@   r9   r9   r:   Úget_sheet_names)  s    zWorkbook.get_sheet_namesc                 C   s   dd„ | j D ƒS )zrA list of sheets in this workbook

        :type: list of :class:`openpyxl.worksheet.worksheet.Worksheet`
        c                 S   s    g | ]}t |tttfƒr|‘qS r9   )rL   r   r   r   ©Ú.0Úsr9   r9   r:   Ú
<listcomp>3  s      z'Workbook.worksheets.<locals>.<listcomp>©r*   r@   r9   r9   r:   rk   -  s    zWorkbook.worksheetsc                 C   s   dd„ | j D ƒS )zzA list of Chartsheets in this workbook

        :type: list of :class:`openpyxl.chartsheet.chartsheet.Chartsheet`
        c                 S   s   g | ]}t |tƒr|‘qS r9   )rL   r   rv   r9   r9   r:   ry   ;  s     
 z(Workbook.chartsheets.<locals>.<listcomp>rz   r@   r9   r9   r:   rm   5  s    zWorkbook.chartsheetsc                 C   s   dd„ | j D ƒS )zœReturns the list of the names of worksheets in this workbook.

        Names are returned in the worksheets order.

        :type: list of strings

        c                 S   s   g | ]
}|j ‘qS r9   ©rT   rv   r9   r9   r:   ry   F  s     z'Workbook.sheetnames.<locals>.<listcomp>rz   r@   r9   r9   r:   rh   =  s    	zWorkbook.sheetnameszcAssign scoped named ranges directly to worksheets or global ones to the workbook. Deprecated in 3.1c                 C   s<   t |d�}|dk	r(d t|jƒ|¡|_n||_|| j|< dS )z1Create a new named_range on a worksheet

        ©re   Nz{0}!{1})r   ro   r   rT   rD   r,   )r8   re   r^   rD   ZscopeZdefnr9   r9   r:   Úcreate_named_rangeI  s
    
zWorkbook.create_named_rangec                 C   s   | j  |¡ | | ¡ dS )z#
        Add a named style
        N)r>   r6   Úbind)r8   Ústyler9   r9   r:   r?   W  s    zWorkbook.add_named_stylec                 C   s   | j jS )z-
        List available named styles
        )r>   Únamesr@   r9   r9   r:   Únamed_styles_  s    zWorkbook.named_stylesc                 C   s&   | j r
tpt}| jr"| j rtp t}|S )zß
        The mime type is determined by whether a workbook is a template or
        not and whether it contains macros or not. Excel requires the file
        extension to match but openpyxl does not enforce this.

        )Útemplater(   r&   r1   r'   r%   )r8   Úctr9   r9   r:   Ú	mime_typeg  s    zWorkbook.mime_typec                 C   s0   | j rtdƒ‚| jr"| js"|  ¡  t| |ƒ dS )a�  Save the current workbook under the given `filename`.
        Use this function instead of using an `ExcelWriter`.

        .. warning::
            When creating your workbook using `write_only` set to True,
            you will only be able to call this function once. Subsequent attempts to
            modify or save the file will raise an :class:`openpyxl.shared.exc.WorkbookAlreadySaved` exception.
        zWorkbook is read-onlyN)rF   rN   r5   rk   rW   r   )r8   Úfilenamer9   r9   r:   Úsaveu  s
    	zWorkbook.savec                 C   s   dd„ | j D ƒS )z&
        List of named styles
        c                 S   s   g | ]
}|j ‘qS r9   r|   rv   r9   r9   r:   ry   Š  s     z(Workbook.style_names.<locals>.<listcomp>)r>   r@   r9   r9   r:   Ústyle_names…  s    zWorkbook.style_namesc                 C   sD   | j s| jrtdƒ‚d |j¡}| j|d�}t||d�}| ¡  |S )aS  Copy an existing worksheet in the current workbook

        .. warning::
            This function cannot copy worksheets between workbooks.
            worksheets can only be copied within the workbook that they belong

        :param from_worksheet: the worksheet to be copied from
        :return: copy of the initial worksheet
        z6Cannot copy worksheets in read-only or write-only modez{0} Copyr{   )Zsource_worksheetZtarget_worksheet)r/   rE   rC   ro   rT   rW   r   Úcopy_worksheet)r8   Zfrom_worksheetZ	new_titleZto_worksheetÚcpr9   r9   r:   rˆ   �  s    
zWorkbook.copy_worksheetc                 C   s   t | dƒr| j ¡  dS )z[
        Close workbook file if open. Only affects read-only and write-only modes.
        Ú_archiveN)ÚhasattrrŠ   Úcloser@   r9   r9   r:   rŒ   ¡  s    
zWorkbook.closec                 C   sF   |  ¡ }| jD ]$}|jD ]}||  ¡ kr  dS qq|| jkrBdS dS )z‡
        Check for duplicate name in defined name list and table list of each worksheet.
        Names are not case sensitive.
        TN)Úlowerrk   Ztablesr,   )r8   re   rU   Útr9   r9   r:   Ú_duplicate_name©  s    


zWorkbook._duplicate_name)FF)NN)N)r   )NN)NNN)-Ú__name__Ú
__module__Ú__qualname__Ú__doc__rE   rG   r‚   Úpathr;   r0   Úpropertyr2   ÚsetterrF   rH   r5   rI   rK   rW   rV   r[   r]   r   ra   rc   rf   rj   rO   rl   rq   rr   rt   ru   rk   rm   rh   r}   r?   r�   r„   r†   r‡   rˆ   rŒ   r�   r9   r9   r9   r:   r)   3   s|     þ
!













	
	







r)   N)Ir“   r   Zopenpyxl.compatr   Zopenpyxl.worksheet.worksheetr   Zopenpyxl.worksheet._read_onlyr   Zopenpyxl.worksheet._write_onlyr   Zopenpyxl.worksheet.copierr   Zopenpyxl.utilsr   Zopenpyxl.utils.indexed_listr	   Zopenpyxl.utils.datetimer
   r   Zopenpyxl.utils.exceptionsr   Zopenpyxl.writer.excelr   Zopenpyxl.styles.cell_styler   Zopenpyxl.styles.named_stylesr   Zopenpyxl.styles.differentialr   Zopenpyxl.styles.alignmentr   Zopenpyxl.styles.bordersr   Zopenpyxl.styles.fillsr   r   Zopenpyxl.styles.fontsr   Zopenpyxl.styles.protectionr   Zopenpyxl.styles.colorsr   r   Zopenpyxl.styles.tabler   Zopenpyxl.chartsheetr   Zdefined_namer   r   Zopenpyxl.packaging.corer   Zopenpyxl.packaging.customr   Zopenpyxl.packaging.relationshipr    Úchildr!   Z
protectionr"   r-   r#   r7   r$   Zopenpyxl.xml.constantsr%   r&   r'   r(   ÚintrM   Úobjectr)   r9   r9   r9   r:   Ú<module>   sD   