o
    ðäjj  ã                   @   s¶   d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
 ddlmZmZ ddlmZmZ ddlmZmZ 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„ ZG dd„ dƒZdS )z2Write the workbook global settings to the archive.é    )Úquote_sheetname)ÚARC_APPÚARC_COREÚ
ARC_CUSTOMÚARC_WORKBOOKÚ
PKG_REL_NSÚCUSTOMUI_NSÚARC_ROOT_RELS)ÚtostringÚ
fromstring)ÚRelationshipÚRelationshipList)ÚDefinedNameÚDefinedNameList)ÚExternalReference)Ú
ChildSheetÚWorkbookPackageÚ
PivotCache)ÚWorkbookProperties)ÚCALENDAR_MAC_1904c                 C   s`   dd„ t | jƒD ƒ}|stdƒ‚| j}| j}|r|jdkr|S ||d… D ]}|| _|  S dS )z~
    Return the index of the active sheet.
    If the sheet set to active is hidden return the next visible sheet or None
    c                 S   s   g | ]\}}|j d kr|‘qS )Úvisible)Úsheet_state)Ú.0ÚidxÚsheet© r   ú]/var/www/ticemtrilhas/avaliacao_env/lib/python3.10/site-packages/openpyxl/workbook/_writer.pyÚ
<listcomp>!   s    z$get_active_sheet.<locals>.<listcomp>z"At least one sheet must be visibler   N)Ú	enumerateÚ_sheetsÚ
IndexErrorÚ_active_sheet_indexÚactiver   )ÚwbÚvisible_sheetsr   r   r   r   r   Úget_active_sheet   s   r%   c                   @   s\   e Z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d„ ZdS )ÚWorkbookWriterc                 C   s.   || _ tƒ | _tƒ | _|j| j_|j| j_d S )N)	r#   r   Úrelsr   ÚpackageÚsecurityÚworkbookProtectionÚcalculationÚcalcPr)Úselfr#   r   r   r   Ú__init__3   s
   
zWorkbookWriter.__init__c                 C   s:   t ƒ }| jjd ur| jj|_| jjtkrd|_|| j_d S )NT)	r   r#   Ú	code_nameÚcodeNameÚexcel_base_dater   Údate1904r(   Ú
workbookPr)r-   Úpropsr   r   r   Úwrite_properties;   s   
zWorkbookWriter.write_propertiesc                 C   s†   t | jjdƒD ]9\}}t|j|d |¡d�}t|j|jd�}| j	 
|¡ |jdks9t| jjƒdkr5tdƒ‚|j|_| jj 
|¡ qd S )Né   zrId{0})ÚnameÚsheetIdÚid©ÚtypeÚTargetr   z1The only worksheet of a workbook cannot be hidden)r   r#   r   r   ÚtitleÚformatr   Ú	_rel_typeÚpathr'   Úappendr   ÚlenÚ
ValueErrorÚstater(   Úsheets)r-   r   r   Ú
sheet_nodeÚrelr   r   r   Úwrite_worksheetsE   s   
÷zWorkbookWriter.write_worksheetsc                 C   sX   | j jD ]%}t| j jƒd }t|j|jd�}| j |¡ t|j	d�}| j
j |¡ qd S )Nr6   r:   )r9   )r#   Ú_external_linksrB   r'   r   r?   r@   rA   r   r9   r(   ÚexternalReferences)r-   ÚlinkÚrIdrG   Úextr   r   r   Ú
write_refsR   s   úzWorkbookWriter.write_refsc                 C   sä   t | jj ¡ ƒ}t| jjƒD ]Z\}}t|jƒ}|jr,|j ¡ }|D ]}||_q!| 	|¡ |j
rDtd|dd�}|› d|j
› �|_| |¡ |jrVtd|d�}|j|_| |¡ |jrhtd|d�}|j|_| |¡ qt|d�| j_d S )	NÚ_FilterDatabaseT)r7   ÚlocalSheetIdÚhiddenú!ÚPrint_Titles)r7   rP   Ú
Print_Area)ÚdefinedName)Úlistr#   Údefined_namesÚvaluesr   Ú
worksheetsr   r=   rP   ÚextendÚauto_filterr   ÚvaluerA   Úprint_titlesÚ
print_arear   r(   ÚdefinedNames)r-   rW   r   r   ÚquotedÚnamesÚnr7   r   r   r   Úwrite_names\   s,   





€zWorkbookWriter.write_namesc                 C   sp   t ƒ }| jjD ].}|j|vr5| |j¡ t|jd�}| jj 	|¡ t
|jj|jjd�}| j 	|¡ |j|_qd S )N)ÚcacheId©ÚTyper<   )Úsetr#   Ú_pivotsÚcacheÚaddr   rd   r(   ÚpivotCachesrA   r   Úrel_typer@   r'   r9   )r-   Úpivot_cachesÚpivotÚcrG   r   r   r   Úwrite_pivots{   s   
€ùzWorkbookWriter.write_pivotsc                 C   s0   t | jƒ}| jjr|| jjd _| jj| j_d S )Nr   )r%   r#   ÚviewsÚ	activeTabr(   Ú	bookViews)r-   r"   r   r   r   Úwrite_viewsˆ   s   
zWorkbookWriter.write_viewsc                 C   s>   |   ¡  |  ¡  |  ¡  |  ¡  |  ¡  |  ¡  t| j ¡ ƒS )zWrite the core workbook xml.)	r5   rH   rc   rp   rt   rN   r
   r(   Úto_tree)r-   r   r   r   Úwrite�   s   zWorkbookWriter.writec                 C   sd   t ddd�}| j |¡ t ddd�}| j |¡ | jjr+t ddd�}d|_| j |¡ t| j ¡ ƒS )	z%Write the workbook relationships xml.Ústylesz
styles.xmlr:   Úthemeztheme/theme1.xmlÚ zvbaProject.binzAhttp://schemas.microsoft.com/office/2006/relationships/vbaProject)r   r'   rA   r#   Úvba_archiverf   r
   ru   )r-   rw   rx   Úvbar   r   r   Ú
write_relsœ   s   zWorkbookWriter.write_relsc                 C   sÂ   t ƒ }tdtd�}| |¡ tt› d�td�}| |¡ tdtd�}| |¡ t| jj	ƒdkr:tdt
d�}| |¡ | jjdur[t| jj t¡ƒ}t  |¡}| t¡D ]}| |¡ qSt| ¡ ƒS )	zWrite the package relationshipsÚofficeDocumentr:   z/metadata/core-propertiesre   zextended-propertiesr6   zcustom-propertiesN)r   r   r   rA   r   r   r   rB   r#   Úcustom_doc_propsr   rz   r   Úreadr	   Ú	from_treeÚfindr   r
   ru   )r-   r'   rG   ÚxmlÚ	root_relsr   r   r   Úwrite_root_rels­   s    




zWorkbookWriter.write_root_relsN)Ú__name__Ú
__module__Ú__qualname__r.   r5   rH   rN   rc   rp   rt   rv   r|   r„   r   r   r   r   r&   1   s    

r&   N) Ú__doc__Úopenpyxl.utilsr   Úopenpyxl.xml.constantsr   r   r   r   r   r   r	   Úopenpyxl.xml.functionsr
   r   Úopenpyxl.packaging.relationshipr   r   Úopenpyxl.workbook.defined_namer   r   Ú$openpyxl.workbook.external_referencer   Úopenpyxl.packaging.workbookr   r   r   Úopenpyxl.workbook.propertiesr   Úopenpyxl.utils.datetimer   r%   r&   r   r   r   r   Ú<module>   s   $	