o
    gj                     @   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   S/var/www/html/api-tag/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   $	