o
    	'fP                     @  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                   @  sX   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Z	dS )	CT_HdrFtrzN`w:hdr` and `w:ftr`, the root element for header and footer part respectively.zCallable[[], CT_P]add_pz
List[CT_P]p_lstzList[CT_Tbl]tbl_lstzCallable[[CT_Tbl], CT_Tbl]_insert_tblzw:p 
successorszw:tblreturnList[CT_P | CT_Tbl]c                 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"   I/home/ubuntu/flask/venv/lib/python3.10/site-packages/docx/oxml/section.pyinner_content_elements)   s   
z CT_HdrFtr.inner_content_elementsN)r%   r&   )
__name__
__module____qualname____doc____annotations__r   p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   r6   r0   r   r8   r"   r"   r"   r*   r4   3   s   
 
r4   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   r;   r0   r   r<   r=   r>   r?   r@   rA   r"   r"   r"   r*   r9   @   s.   
 r9   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:wr:   wzw:hhzw:orient)defaultr   orientN)r,   r-   r.   r/   r   r   rC   r0   rD   r   PORTRAITrF   r"   r"   r"   r*   rB   Z   s   
 rB   c                   @  s  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$< [dmd+d,Zdmd-d.Zednd0d1Zejdod4d1Zdpd5d6Zednd7d8Zejdod9d8Zdqd;d<Zdqd=d>Zednd?d@ZejdodAd@ZedndBdCZejdodDdCZdrdFdGZedndHdIZejdodJdIZedsdLdMZejdtdOdMZedndPdQZejdudRdQZedndSdTZejdudUdTZedvdWdXZdwdYdZZdxd[d\Z ednd]d^Z!e!jdud_d^Z!edydadbZ"e"jdzdddbZ"ed{dfdgZ#e#jd|didgZ#edndjdkZ$e$jdudldkZ$dS )}	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:endnotePrr5   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:footerReferencer5      NzCT_SectType | NonetyperQ      zCT_PageSz | NonepgSzrR      zCT_PageMar | NonepgMarrS      zCT_OnOff | NonetitlePgr6   r   r8   r7   r%   r4   c                 C     |   }||_||_|S )zxReturn newly added CT_HdrFtrRef element of `type_` with `rId`.

        The element tag is `w:footerReference`.
        )rM   r6   r8   )r)   r6   r8   footerReferencer"   r"   r*   add_footerReference      zCT_SectPr.add_footerReferencec                 C  r\   )zxReturn newly added CT_HdrFtrRef element of `type_` with `rId`.

        The element tag is `w:headerReference`.
        )rN   r6   r8   )r)   r6   r8   headerReferencer"   r"   r*   add_headerReference   r_   zCT_SectPr.add_headerReferencer:   c                 C     | 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)rY   r=   r)   rY   r"   r"   r*   bottom_margin      zCT_SectPr.bottom_marginvalueint | Length | Nonec                 C  2   |   }|d u st|tr||_d S t||_d S N)rI   
isinstancer   r=   r)   rf   rY   r"   r"   r*   rd      
   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   attribclear)r)   cloned_sectPrr"   r"   r*   clone   s   
zCT_SectPr.clonec                 C  rb   )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)rY   r@   rc   r"   r"   r*   r@      s   zCT_SectPr.footerc                 C  rh   ri   )rI   rj   r   r@   rk   r"   r"   r*   r@      rl   CT_HdrFtrRef | Nonec                 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)   r6   path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   rr   len)r)   r6   matching_headerReferencesr"   r"   r*   get_headerReference   s   zCT_SectPr.get_headerReferencec                 C  rb   )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)rY   rA   rc   r"   r"   r*   rA         zCT_SectPr.gutterc                 C  rh   ri   )rI   rj   r   rA   rk   r"   r"   r*   rA      rl   c                 C  rb   )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)rY   r?   rc   r"   r"   r*   r?     s   zCT_SectPr.headerc                 C  rh   ri   )rI   rj   r   r?   rk   r"   r"   r*   r?     rl   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  rb   )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)rY   r>   rc   r"   r"   r*   left_margin  ry   zCT_SectPr.left_marginc                 C  rh   ri   )rI   rj   r   r>   rk   r"   r"   r*   r~   '  rl   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)rW   r   rG   rF   r)   rW   r"   r"   r*   orientation.  s   zCT_SectPr.orientationWD_ORIENTATION | Nonec                 C  s"   |   }|r||_d S tj|_d S ri   )rJ   r   rG   rF   r)   rf   rW   r"   r"   r*   r   :  s   c                 C  rb   )ziValue in EMU of the `h` attribute of the `w:pgSz` child element.

        |None| if not present.
        N)rW   rD   r   r"   r"   r*   page_height?  re   zCT_SectPr.page_heightc                 C     |   }||_d S ri   )rJ   rD   r   r"   r"   r*   r   J     
c                 C  rb   )zoValue in EMU of the ``w`` attribute of the ``<w:pgSz>`` child element.

        |None| if not present.
        N)rW   rC   r   r"   r"   r*   
page_widthO  re   zCT_SectPr.page_widthc                 C  r   ri   )rJ   rC   r   r"   r"   r*   r   Z  r   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'   rv   )r)   preceding_sectPrsr"   r"   r*   preceding_sectPr_  s   
zCT_SectPr.preceding_sectPrc                 C  .   |  |}|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)ru   
ValueErrorr8   remove)r)   r6   r]   r8   r"   r"   r*   remove_footerReferencef     

z CT_SectPr.remove_footerReferencec                 C  r   )zCReturn rId of w:headerReference child of `type_` after removing it.Nz!CT_SectPr has no header reference)rx   r   r8   r   )r)   r6   r`   r8   r"   r"   r*   remove_headerReferencep  r   z CT_SectPr.remove_headerReferencec                 C  rb   )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)rY   r<   rc   r"   r"   r*   right_marginz  ry   zCT_SectPr.right_marginc                 C  r   ri   )rI   r<   rk   r"   r"   r*   r     r   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)rU   valr   NEW_PAGE)r)   rU   r"   r"   r*   
start_type  s   zCT_SectPr.start_typeWD_SECTION_START | Nonec                 C  s0   |d u s	|t ju r|   d S |  }||_d S ri   )r   r   rP   rL   r   )r)   rf   rU   r"   r"   r*   r     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)r[   r   )r)   r[   r"   r"   r*   titlePg_val  s   zCT_SectPr.titlePg_valbool | Nonec                 C  s"   |dv r
|    d S d|  _d S )N)NFT)rO   rK   r   )r)   rf   r"   r"   r*   r     s   c                 C  rb   )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)rY   r;   rc   r"   r"   r*   
top_margin  ry   zCT_SectPr.top_marginc                 C  r   ri   )rI   r;   rk   r"   r"   r*   r     r   )r6   r   r8   r7   r%   r4   )r%   r:   )rf   rg   )r%   rH   )r6   r   r%   rq   )r%   rz   )r%   r   )rf   r   )rf   r:   )r%   r   )r6   r   r%   r7   )r6   r   )r%   r   )rf   r   )r%   r   )rf   r   )%r,   r-   r.   r/   r0   _tag_seqr   r`   r]   r   rU   rW   rY   r[   r^   ra   r3   rd   setterrp   r@   ru   rx   rA   r?   r}   r~   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r*   rH   j   s   
 








		
	





			rH   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:valr   r   N)r,   r-   r.   r/   r   r   r   r0   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	ZedddZ	dddZ
dddZed ddZd!ddZed"ddZdS )#r{   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_xpathsectPrrH   c                 C  s
   || _ d S ri   )_sectPr)r)   r   r"   r"   r*   __init__  s   
z"_SectBlockElementIterator.__init__r%   Iterator[BlockElement]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*   r|     s   z2_SectBlockElementIterator.iter_sect_block_elementsc                 c  sZ    | j | j}}||}|dkrdn| ||d  }| ||d D ]}|V  q%d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   sectPrs
sectPr_idxn_blks_to_skipelementr"   r"   r*   r     s   
z3_SectBlockElementIterator._iter_sect_block_elements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_sectionr7   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)   p_sect_term_blockbody_sect_term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_section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   rH   )r   rH   r%   r   )r%   r   )r   rH   r%   r   )r%   r7   )r   rH   r%   r   )r%   r   )r,   r-   r.   r/   r   r0   r   r   classmethodr|   r   r   r   r   r   r   r"   r"   r"   r*   r{     s   
 



r{   N)1r/   
__future__r   copyr   typingr   r   r   r   r   lxmlr	   typing_extensionsr
   docx.enum.sectionr   r   r   docx.oxml.nsr   docx.oxml.sharedr   docx.oxml.simpletypesr   r   r   docx.oxml.tabler   docx.oxml.text.paragraphr   docx.oxml.xmlchemyr   r   r   r   r   docx.sharedr   r   r   r0   r   r4   r9   rB   rH   r   r{   r"   r"   r"   r*   <module>   s0      R