a
    ]jO                     @  sT  U d Z ddlmZ ddlmZ ddl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mZmZ ddl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lm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z' dZ(de)d< G dd de Z*G dd de Z+G dd de Z,G dd de Z-G dd de Z.G dd de Z/G dd dZ0d S )!z'Section-related custom element classes.    )annotations)deepcopy)CallableIteratorListSequencecast)etree)	TypeAlias)WD_HEADER_FOOTERWD_ORIENTATIONWD_SECTION_START)nsmap)CT_OnOff)ST_SignedTwipsMeasureST_TwipsMeasure	XsdString)CT_Tbl)CT_P)BaseOxmlElementOptionalAttributeRequiredAttribute
ZeroOrMore	ZeroOrOne)LengthlazypropertyzCT_P | CT_Tblr
   BlockElementc                   @  s\   e Zd ZU dZded< ded< ded< ded	< ed
ddZedddZeddddZ	dS )	CT_HdrFtrzN`w:hdr` and `w:ftr`, the root element for header and footer part respectively.zCallable[[], CT_P]Zadd_pz
List[CT_P]Zp_lstzList[CT_Tbl]Ztbl_lstzCallable[[CT_Tbl], CT_Tbl]Z_insert_tblzw:p 
successorszw:tblzList[CT_P | CT_Tbl]returnc                 C  s
   |  dS )zGenerate all `w:p` and `w:tbl` elements in this header or footer.

        Elements appear in document order. Elements shaded by nesting in a `w:ins` or
        other "wrapper" element will not be included.
        z./w:p | ./w:tbl)xpathselfr   r   M/var/www/html/assistant/venv/lib/python3.9/site-packages/docx/oxml/section.pyinner_content_elements)   s    z CT_HdrFtr.inner_content_elementsN)
__name__
__module____qualname____doc____annotations__r   pZtblpropertyr'   r   r   r   r&   r      s   
r   c                   @  s6   e Zd ZU dZedeZded< edeZ	ded< dS )	CT_HdrFtrRefz5`w:headerReference` and `w:footerReference` elements.w:typer   type_zr:idstrrIdN)
r(   r)   r*   r+   r   r   r1   r,   r   r3   r   r   r   r&   r/   3   s
   
r/   c                   @  s   e Zd ZU dZedeZded< edeZ	ded< edeZ
ded< ed	eZded
< edeZded< edeZded< edeZded< dS )
CT_PageMarz-``<w:pgMar>`` element, defining page margins.zw:topLength | Nonetopzw:rightrightzw:bottombottomzw:leftleftzw:headerheaderzw:footerfooterzw:guttergutterN)r(   r)   r*   r+   r   r   r6   r,   r   r7   r8   r9   r:   r;   r<   r   r   r   r&   r4   <   s,   
r4   c                   @  sN   e Zd ZU dZedeZded< edeZded< ede	e	j
dZd	ed
< dS )	CT_PageSzz?``<w:pgSz>`` element, defining page dimensions and orientation.zw:wr5   wzw:hhzw:orient)defaultr   orientN)r(   r)   r*   r+   r   r   r>   r,   r?   r   PORTRAITrA   r   r   r   r&   r=   V   s   
r=   c                   @  s8  e Zd ZU dZded< ded< ded< ded	< d
ed< d
ed< ded< ded< dZededZededZe	dedd dZ
ded< e	dedd dZded< e	dedd dZded < e	d!ed"d dZd#ed$< [d%d&d'd(d)d*Zd%d&d'd(d+d,Zed-d.d/d0Zejd1d2d3d0Zd d.d4d5Zed-d.d6d7Zejd1d2d8d7Zd%d9d:d;d<Zd%d9d:d=d>Zed-d.d?d@Zejd1d2dAd@Zed-d.dBdCZejd1d2dDdCZdEd.dFdGZed-d.dHdIZejd1d2dJdIZedKd.dLdMZejdNd2dOdMZed-d.dPdQZejd-d2dRdQZed-d.dSdTZejd-d2dUdTZedVd.dWdXZd%d&d:dYdZZd%d[d\d]Z ed-d.d^d_Z!e!jd-d2d`d_Z!edad.dbdcZ"e"jddd2dedcZ"edfd.dgdhZ#e#jdid2djdhZ#ed-d.dkdlZ$e$jd-d2dmdlZ$dS )n	CT_SectPrzA`w:sectPr` element, the container element for section properties.zCallable[[], CT_PageMar]get_or_add_pgMarzCallable[[], CT_PageSz]get_or_add_pgSzzCallable[[], CT_OnOff]get_or_add_titlePgzCallable[[], CT_SectType]get_or_add_typezCallable[[], CT_HdrFtrRef]_add_footerReference_add_headerReferencezCallable[[], None]_remove_titlePg_remove_type)zw:footnotePrzw:endnotePrr0   w:pgSzw:pgMarz
w:paperSrczw:pgBorderszw:lnNumTypezw:pgNumTypezw:colsz
w:formProtzw:vAlignzw:noEndnote	w:titlePgzw:textDirectionzw:bidizw:rtlGutterz	w:docGridzw:printerSettingszw:sectPrChangezw:headerReferencer   zw:footerReferencer0      NzCT_SectType | NonetyperL      zCT_PageSz | NonepgSzrM      zCT_PageMar | NonepgMarrN      zCT_OnOff | NonetitlePgr   r2   r/   )r1   r3   r"   c                 C  s   |   }||_||_|S )zxReturn newly added CT_HdrFtrRef element of `type_` with `rId`.

        The element tag is `w:footerReference`.
        )rH   r1   r3   )r%   r1   r3   footerReferencer   r   r&   add_footerReference   s    zCT_SectPr.add_footerReferencec                 C  s   |   }||_||_|S )zxReturn newly added CT_HdrFtrRef element of `type_` with `rId`.

        The element tag is `w:headerReference`.
        )rI   r1   r3   )r%   r1   r3   headerReferencer   r   r&   add_headerReference   s    zCT_SectPr.add_headerReferencer5   r!   c                 C  s   | j }|du rdS |jS )zValue of the `w:bottom` attr of `<w:pgMar>` child element, as |Length|.

        |None| when either the element or the attribute is not present.
        N)rT   r8   r%   rT   r   r   r&   bottom_margin   s    zCT_SectPr.bottom_marginzint | Length | None)valuec                 C  s,   |   }|d u st|tr|nt||_d S N)rD   
isinstancer   r8   r%   r]   rT   r   r   r&   r\      s    c                 C  s   t | }|j  |S )zReturn an exact duplicate of this ``<w:sectPr>`` element tree suitable for
        use in adding a section break.

        All rsid* attributes are removed from the root ``<w:sectPr>`` element.
        )r   Zattribclear)r%   Zcloned_sectPrr   r   r&   clone   s    
zCT_SectPr.clonec                 C  s   | j }|du rdS |jS )a  Distance from bottom edge of page to bottom edge of the footer.

        This is the value of the `w:footer` attribute in the `w:pgMar` child element,
        as a |Length| object, or |None| if either the element or the attribute is not
        present.
        N)rT   r;   r[   r   r   r&   r;      s    zCT_SectPr.footerc                 C  s,   |   }|d u st|tr|nt||_d S r^   )rD   r_   r   r;   r`   r   r   r&   r;      s    zCT_HdrFtrRef | None)r1   r"   c                 C  s(   dt | }| |}|s dS |d S )zAReturn footerReference element of `type_` or None if not present.z!./w:footerReference[@w:type='%s']Nr   )r   to_xmlr#   )r%   r1   pathZfooterReferencesr   r   r&   get_footerReference   s
    
zCT_SectPr.get_footerReferencec                 C  s,   |  dt| }t|dkr$dS |d S )zAReturn headerReference element of `type_` or None if not present.z!./w:headerReference[@w:type='%s']r   N)r#   r   rc   len)r%   r1   Zmatching_headerReferencesr   r   r&   get_headerReference   s    zCT_SectPr.get_headerReferencec                 C  s   | j }|du rdS |jS )zThe value of the ``w:gutter`` attribute in the ``<w:pgMar>`` child element,
        as a |Length| object, or |None| if either the element or the attribute is not
        present.N)rT   r<   r[   r   r   r&   r<      s    zCT_SectPr.gutterc                 C  s,   |   }|d u st|tr|nt||_d S r^   )rD   r_   r   r<   r`   r   r   r&   r<      s    c                 C  s   | j }|du rdS |jS )zDistance from top edge of page to top edge of header.

        This value comes from the `w:header` attribute on the `w:pgMar` child element.
        |None| if either the element or the attribute is not present.
        N)rT   r:   r[   r   r   r&   r:      s    zCT_SectPr.headerc                 C  s,   |   }|d u st|tr|nt||_d S r^   )rD   r_   r   r:   r`   r   r   r&   r:     s    zIterator[CT_P | CT_Tbl]c                 C  s
   t | S )zGenerate all `w:p` and `w:tbl` elements in this section.

        Elements appear in document order. Elements shaded by nesting in a `w:ins` or
        other "wrapper" element will not be included.
        )_SectBlockElementIteratoriter_sect_block_elementsr$   r   r   r&   iter_inner_content  s    zCT_SectPr.iter_inner_contentc                 C  s   | j }|du rdS |jS )zThe value of the ``w:left`` attribute in the ``<w:pgMar>`` child element, as
        a |Length| object, or |None| if either the element or the attribute is not
        present.N)rT   r9   r[   r   r   r&   left_margin  s    zCT_SectPr.left_marginc                 C  s,   |   }|d u st|tr|nt||_d S r^   )rD   r_   r   r9   r`   r   r   r&   rk     s    r   c                 C  s   | j }|du rtjS |jS )z`WD_ORIENTATION` member indicating page-orientation for this section.

        This is the value of the `orient` attribute on the `w:pgSz` child, or
        `WD_ORIENTATION.PORTRAIT` if not present.
        N)rR   r   rB   rA   r%   rR   r   r   r&   orientation  s    zCT_SectPr.orientationzWD_ORIENTATION | Nonec                 C  s   |   }|r|ntj|_d S r^   )rE   r   rB   rA   r%   r]   rR   r   r   r&   rm   *  s    c                 C  s   | j }|du rdS |jS )ziValue in EMU of the `h` attribute of the `w:pgSz` child element.

        |None| if not present.
        N)rR   r?   rl   r   r   r&   page_height/  s    zCT_SectPr.page_heightc                 C  s   |   }||_d S r^   )rE   r?   rn   r   r   r&   ro   :  s    c                 C  s   | j }|du rdS |jS )zoValue in EMU of the ``w`` attribute of the ``<w:pgSz>`` child element.

        |None| if not present.
        N)rR   r>   rl   r   r   r&   
page_width?  s    zCT_SectPr.page_widthc                 C  s   |   }||_d S r^   )rE   r>   rn   r   r   r&   rp   J  s    zCT_SectPr | Nonec                 C  s"   |  d}t|dkr|d S dS )zCSectPr immediately preceding this one or None if this is the first.z./preceding::w:sectPr[1]r   N)r#   rf   )r%   Zpreceding_sectPrsr   r   r&   preceding_sectPrO  s    
zCT_SectPr.preceding_sectPrc                 C  s.   |  |}|du rtd|j}| | |S )zCReturn rId of w:footerReference child of `type_` after removing it.Nz!CT_SectPr has no footer reference)re   
ValueErrorr3   remove)r%   r1   rW   r3   r   r   r&   remove_footerReferenceV  s    

z CT_SectPr.remove_footerReference)r1   c                 C  s.   |  |}|du rtd|j}| | |S )zCReturn rId of w:headerReference child of `type_` after removing it.Nz!CT_SectPr has no header reference)rg   rr   r3   rs   )r%   r1   rY   r3   r   r   r&   remove_headerReference`  s    

z CT_SectPr.remove_headerReferencec                 C  s   | j }|du rdS |jS )zThe value of the ``w:right`` attribute in the ``<w:pgMar>`` child element, as
        a |Length| object, or |None| if either the element or the attribute is not
        present.N)rT   r7   r[   r   r   r&   right_marginj  s    zCT_SectPr.right_marginc                 C  s   |   }||_d S r^   )rD   r7   r`   r   r   r&   rv   t  s    r   c                 C  s$   | j }|du s|jdu rtjS |jS )zThe member of the ``WD_SECTION_START`` enumeration corresponding to the value
        of the ``val`` attribute of the ``<w:type>`` child element, or
        ``WD_SECTION_START.NEW_PAGE`` if not present.N)rP   valr   NEW_PAGE)r%   rP   r   r   r&   
start_typey  s    zCT_SectPr.start_typeWD_SECTION_START | Nonec                 C  s0   |d u s|t ju r|   d S |  }||_d S r^   )r   rx   rK   rG   rw   )r%   r]   rP   r   r   r&   ry     s
    boolc                 C  s   | j }|du rdS |jS )zEValue of `w:titlePg/@val` or |False| if `./w:titlePg` is not present.NF)rV   rw   )r%   rV   r   r   r&   titlePg_val  s    zCT_SectPr.titlePg_valzbool | Nonec                 C  s    |dv r|    n
d|  _d S )N)NFT)rJ   rF   rw   )r%   r]   r   r   r&   r|     s    
c                 C  s   | j }|du rdS |jS )zThe value of the ``w:top`` attribute in the ``<w:pgMar>`` child element, as a
        |Length| object, or |None| if either the element or the attribute is not
        present.N)rT   r6   r[   r   r   r&   
top_margin  s    zCT_SectPr.top_marginc                 C  s   |   }||_d S r^   )rD   r6   r`   r   r   r&   r}     s    )%r(   r)   r*   r+   r,   Z_tag_seqr   rY   rW   r   rP   rR   rT   rV   rX   rZ   r.   r\   setterrb   r;   re   rg   r<   r:   rj   rk   rm   ro   rp   rq   rt   ru   rv   ry   r|   r}   r   r   r   r&   rC   d   s   




			



			rC   c                   @  s$   e Zd ZU dZedeZded< dS )CT_SectTypez:``<w:sectType>`` element, defining the section start type.zw:valrz   rw   N)r(   r)   r*   r+   r   r   rw   r,   r   r   r   r&   r     s   
r   c                   @  s   e Zd ZU dZdZded< dZded< dddd	Zedd
dddZ	d
dddZ
dddddZeddddZdddddZeddddZdS )rh   zGenerates the block-item XML elements in a section.

    A block-item element is a `CT_P` (paragraph) or a `CT_Tbl` (table).
    Nzetree.XPath | None_compiled_blocks_xpath_compiled_count_xpathrC   )sectPrc                 C  s
   || _ d S r^   )_sectPr)r%   r   r   r   r&   __init__  s    z"_SectBlockElementIterator.__init__zIterator[BlockElement])r   r"   c                 C  s   | |  S )zIGenerate each CT_P or CT_Tbl element within extents governed by `sectPr`.)_iter_sect_block_elements)clsr   r   r   r&   ri     s    z2_SectBlockElementIterator.iter_sect_block_elementsr!   c                 c  sX   | j | j }}||}|dkr$dn| ||d  }| ||d D ]
}|V  qHdS )z0Generate each CT_P or CT_Tbl element in section.r      N)r   _sectPrsindex%_count_of_blocks_in_and_above_section_blocks_in_and_above_section)r%   r   ZsectPrsZ
sectPr_idxZn_blks_to_skipelementr   r   r&   r     s    
z3_SectBlockElementIterator._iter_sect_block_elementszSequence[BlockElement]c                 C  s6   | j du rtj| jtdd| _ | j }ttt ||S )FAll ps and tbls in section defined by `sectPr` and all prior sections.NF
namespacesregexp)r   r	   XPath"_blocks_in_and_above_section_xpathr   r   r   r   r%   r   r#   r   r   r&   r     s    
z6_SectBlockElementIterator._blocks_in_and_above_sectionr2   c              	   C  s,   d}d}d}| d| d| d| d| 	S )zHXPath expr for ps and tbls in context of a sectPr and all prior sectPrs.z./parent::w:pPr/parent::w:pzself::w:sectPr[parent::w:body]z-preceding-sibling::*[self::w:p | self::w:tbl]z | /r   )r%   Zp_sect_term_blockZbody_sect_termZpred_ps_and_tblsr   r   r&   r     s    
z<_SectBlockElementIterator._blocks_in_and_above_section_xpathintc                 C  s>   | j du r&tjd| j dtdd| _ | j }ttt||S )r   Nzcount()Fr   )r   r	   r   r   r   r   r   floatr   r   r   r&   r     s    
z?_SectBlockElementIterator._count_of_blocks_in_and_above_sectionzSequence[CT_SectPr]c                 C  s   | j dS )z5All w:sectPr elements in document, in document-order.zC/w:document/w:body/w:p/w:pPr/w:sectPr | /w:document/w:body/w:sectPr)r   r#   r$   r   r   r&   r     s    z"_SectBlockElementIterator._sectPrs)r(   r)   r*   r+   r   r,   r   r   classmethodri   r   r   r   r   r   r   r   r   r   r&   rh     s   
rh   N)1r+   
__future__r   copyr   typingr   r   r   r   r   Zlxmlr	   Ztyping_extensionsr
   Zdocx.enum.sectionr   r   r   Zdocx.oxml.nsr   Zdocx.oxml.sharedr   Zdocx.oxml.simpletypesr   r   r   Zdocx.oxml.tabler   Zdocx.oxml.text.paragraphr   Zdocx.oxml.xmlchemyr   r   r   r   r   Zdocx.sharedr   r   r   r,   r   r/   r4   r=   rC   r   rh   r   r   r   r&   <module>   s.   	  H