o
    eÜ?  ã                   @   sŠ  d Z ddlmZ ddlmZmZmZ ddlmZ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d„ZdAdefdd„Zdd„ ZdBdefdd„Zdd„ ZdCdefdd„Z dDd efd!d"„Z!d#e"fd$d%„Z#d#e"fd&d'„Z$d(d)„ Z%d*d+„ Z&G d,d-„ d-eƒZ'ed.e'ƒ dEd0d1„Z(d2efd3d4„Z)d5efd6d7„Z*d5ed#e"fd8d9„Z+d5efd:d;„Z,dFd5ed=e-fd>d?„Z.d/S )Gz1docx operation methods based on ``python-docx``.
é    )ÚPt)ÚOxmlElementÚ	parse_xmlÚregister_element_cls)ÚqnÚnsdecls)Ú
CT_Picture)ÚBaseOxmlElementÚOneAndOnlyOne)ÚWD_COLOR_INDEX)ÚUnrecognizedImageError)Ú_Cell)ÚRELATIONSHIP_TYPEé   )Ú	rgb_valueé   c                 C   sD   | j  d¡d }| tdƒt|ƒ¡ | tdƒttd| ƒƒ¡ dS )a  Set section column count and space. All the columns have same width.

    Args:
        section : ``python-docx`` Section instance.
        num (int): Column count. Defaults to 2.
        space (int, optional): Space between adjacent columns. Unit: Pt. Defaults to 0.
    ú./w:colsr   úw:numúw:spaceé   N)Ú_sectPrÚxpathÚsetr   ÚstrÚint)ÚsectionÚnumÚspaceÚcol© r   úDD:\Projects\ConvertPro\env\Lib\site-packages\pdf2docx/common/docx.pyÚset_equal_columns   s    r!   Ú
width_listc                 C   sœ   | j  d¡d }| tdƒtt|ƒƒ¡ | tdƒd¡ |ddd… D ](}tdƒ}| td	ƒttd
| ƒƒ¡ | tdƒttd
| ƒƒ¡ | d|¡ q#dS )aŸ  Set section column count and space.

    Args:
        section : ``python-docx`` Section instance.
        width_list (list|tuple): Width of each column.
        space (int, optional): Space between adjacent columns. Unit: Pt. Defaults to 0.
    
    Scheme::

        <w:cols w:num="2" w:space="0" w:equalWidth="0">
            <w:col w:w="2600" w:space="0"/>
            <w:col w:w="7632"/>
        </w:cols>
    r   r   r   zw:equalWidthÚ0Néÿÿÿÿzw:colúw:wr   r   )	r   r   r   r   r   Úlenr   r   Úinsert)r   r"   r   ÚcolsÚwÚer   r   r    Úset_columns"   s   ür+   c                 C   s$   | j }| ¡  |¡ d | _| _ dS )z‚Delete a paragraph.

    Reference:    
        https://github.com/python-openxml/python-docx/issues/33#issuecomment-77661907
    N)Ú_elementZ	getparentÚremoveÚ_p)Ú	paragraphÚpr   r   r    Údelete_paragraph>   s   r1   çÍÌÌÌÌÌð?Úline_spacingc                 C   s†   | j }||_tdƒ|_tdƒ|_tdƒ|_tdƒ|_d|_d t	dƒ¡}| j
 ¡  dt|ƒ¡ d t	dƒ¡}| j
 ¡  dt|ƒ¡ |S )a¤  Reset paragraph format, especially line spacing.

    Two kinds of line spacing, corresponding to the setting in MS Office Word:

    * line_spacing=1.05: single or multiple
    * line_spacing=Pt(1): exactly
    
    Args:
        p (Paragraph): ``python-docx`` paragraph instance.
        line_spacing (float, optional): Line spacing. Defaults to 1.05.
    
    Returns:
        paragraph_format: Paragraph format.
    r   Tz<w:autoSpaceDE {} w:val="0"/>r)   z<w:autoSpaceDN {} w:val="0"/>)Úparagraph_formatr3   r   Zspace_beforeZspace_afterZleft_indentZright_indentZwidow_controlÚformatr   r.   Zget_or_add_pPrr'   r   )r0   r3   ÚpfÚxmlr   r   r    Úreset_paragraph_formatI   s   



r8   c                 C   s<   t dƒ}t dƒ}t dƒ}| |¡ | |¡ | j |¡ dS )a  Hide paragraph. This method just sets the paragraph property, while the added text must
    be hided explicitly.

        r = p.add_run()
        r.text = "Hidden"
        r.font.hidden = True

    Args:
        p (Paragraph): python-docx created paragraph.
    zw:pPrúw:rPrzw:vanishN)r   Úappendr.   )r0   ZpPrÚrPrÚvr   r   r    Úset_hidden_propertyj   s   

r=   ç      ð?Úscalec              	   C   ó*   | j  ¡  dtd tdƒd| ¡ƒ¡ dS )a  Set character spacing: scaling. 
    
    Manual operation in MS Word: Font | Advanced | Character Spacing | Scaling.
    
    Args:
        p_run (docx.text.run.Run): Proxy object wrapping <w:r> element.
        scale (float, optional): scaling factor. Defaults to 1.0.
    r   z<w:w {} w:val="{}"/>r)   éd   N©Ú_rÚget_or_add_rPrr'   r   r5   r   )Úp_runr?   r   r   r    Úset_char_scaling€   ó   	ÿrF   ç        r   c              	   C   r@   )a1  Set character spacing. 
    
    Manual operation in MS Word: Font | Advanced | Character Spacing | Spacing.
    
    Args:
        p_run (docx.text.run.Run): Proxy object wrapping <w:r> element.
        space (float, optional): Spacing value in Pt. Expand if positive else condense. Defaults to 0.0.
    r   z<w:spacing {} w:val="{}"/>r)   r   NrB   )rE   r   r   r   r    Úset_char_spacing   rG   rI   Úsrgbc                 C   s˜   t dƒtjt dƒtjt dƒtjt dƒtjt dƒtjt dƒtji}||v r,|| | j_	dS t
|ƒdd…  d	¡}d
 tdƒ|¡}| j ¡  dt|ƒ¡ dS )a  Set character shading color, in case the color is out of highlight color scope.
    
    Reference: 
        http://officeopenxml.com/WPtextShading.php
    
    Args:
        p_run (docx.text.run.Run): Proxy object wrapping <w:r> element.
        srgb (int): Color value.
    )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   r   Né   z4<w:shd {} w:val="clear" w:color="auto" w:fill="{}"/>r)   r   )r   r   ZREDZBRIGHT_GREENZBLUEZYELLOWZPINKZ	TURQUOISEZfontÚhighlight_colorÚhexÚzfillr5   r   rC   rD   r'   r   )rE   rJ   Z	color_mapÚcr7   r   r   r    Úset_char_shadingš   s   





úrP   c                 C   s@   t |ƒdd…  d¡}d tdƒ|¡}| j ¡  dt|ƒ¡ dS )z•Set underline and color.
    
    Args:
        p_run (docx.text.run.Run): Proxy object wrapping <w:r> element.
        srgb (int): Color value.
    r   NrK   z%<w:u {} w:val="single" w:color="{}"/>r)   r   )rM   rN   r5   r   rC   rD   r'   r   )rE   rJ   rO   r7   r   r   r    Úset_char_underline¸   s   rQ   c           
      C   s¤   | j }|j|tjdd}tdƒ}| tdƒ|¡ | tdƒd¡ tdƒ}tdƒ}td	ƒ}| td
ƒd¡ | |¡ | |¡ ||_| |¡ |  	¡ }	|	j
 |¡ |	S )a“  Create a hyperlink within a paragraph object.

    Reference:

        https://github.com/python-openxml/python-docx/issues/74#issuecomment-215678765

    Args:
        paragraph (Paragraph): ``python-docx`` paragraph adding the hyperlink to.
        url (str): The required url.
        text (str): The text displayed for the url.

    Returns: 
        Run: A Run object containing the hyperlink.
    T)Zis_externalzw:hyperlinkzr:idz	w:historyÚ1zw:rr9   zw:rStyleúw:valZ	Hyperlink)ÚpartZ	relate_tor   Z	HYPERLINKr   r   r   r:   ÚtextÚadd_runrC   )
r/   ÚurlrU   rT   Zr_idZ	hyperlinkZnew_runr;   ZrStyleÚrr   r   r    Úadd_hyperlinkÄ   s    


rY   c                 C   sN   |   ¡ }z|j|t|ƒt|ƒd W n ty    tdƒ Y dS w d| j_dS )zþ Add image to paragraph.
    
    Args:
        p (Paragraph): ``python-docx`` paragraph instance.
        image_path_or_stream (str, bytes): Image path or stream.
        width (float): Image width in Pt.
        height (float): Image height in Pt.
    )ÚwidthÚheightzUnrecognized Image.Nr>   )rV   Zadd_picturer   r   Úprintr4   r3   )r0   Úimage_path_or_streamrZ   r[   Z	docx_spanr   r   r    Ú	add_imageø   s   	þr^   c                   @   sL   e Zd ZdZedƒZedƒZedƒZedd„ ƒZ	edd„ ƒZ
ed	d
„ ƒZdS )Ú
_CT_AnchorzA
    ``<w:anchor>`` element, container for a floating image.
    z	wp:extentzwp:docPrz	a:graphicc                 C   sP   t |  ||¡ƒ}||j_||j_||j_d| |j_d|jj	_
|jj	 |¡ |S )zn
        Return a new ``<wp:anchor>`` element populated with the values passed
        as parameters.
        z
Picture %dz8http://schemas.openxmlformats.org/drawingml/2006/picture)r   Ú_anchor_xmlÚextentÚcxÚcyÚdocPrÚidÚnameÚgraphicÚgraphicDataÚuriÚ_insert_pic)Úclsrb   rc   Úshape_idÚpicÚpos_xÚpos_yÚanchorr   r   r    Únew  s   ÿz_CT_Anchor.newc                 C   s<   d}t  |||||¡}	|  ||||	||¡}
|
jj |	¡ |
S )z}
        Return a new `wp:anchor` element containing the `pic:pic` element
        specified by the argument values.
        r   )r   rq   rg   rh   rj   )rk   rl   ÚrIdÚfilenamerb   rc   rn   ro   Zpic_idrm   rp   r   r   r    Únew_pic_anchor%  s
   z_CT_Anchor.new_pic_anchorc                 C   s    dt ddddƒt|ƒt|ƒf S )NaŽ  <wp:anchor distT="0" distB="0" distL="0" distR="0" simplePos="0" relativeHeight="0" 
           behindDoc="1" locked="0" layoutInCell="1" allowOverlap="1" 
           %s>
  <wp:simplePos x="0" y="0"/>
  <wp:positionH relativeFrom="page">
    <wp:posOffset>%d</wp:posOffset>
  </wp:positionH>
  <wp:positionV relativeFrom="page">
    <wp:posOffset>%d</wp:posOffset>
  </wp:positionV>
  <wp:extent cx="914400" cy="914400"/>
  <wp:wrapNone/>
  <wp:docPr id="666" name="unnamed"/>
  <wp:cNvGraphicFramePr>
    <a:graphicFrameLocks noChangeAspect="1"/>
  </wp:cNvGraphicFramePr>
  <a:graphic>
    <a:graphicData uri="URI not set"/>
  </a:graphic>
</wp:anchor>ZwpÚarm   rX   )r   r   )rk   rn   ro   r   r   r    r`   1  s   íÿz_CT_Anchor._anchor_xmlN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   ra   rd   rg   Úclassmethodrq   rt   r`   r   r   r   r    r_     s    

r_   z	wp:anchorNc              
   C   sj   |   ¡ }|j |¡\}}| t|ƒd¡\}}	|jj|j}
}t |
||||	t|ƒt|ƒ¡}|j	 
|¡ dS )aÊ  Add float image behind text.
    
    Args:
        p (Paragraph): ``python-docx`` Paragraph object this picture belongs to.
        image_path_or_stream (str, bytes): Image path or stream.
        width (float): Displaying width of picture, in unit Pt.
        pos_x (float): X-position (English Metric Units) to the top-left point of page valid region
        pos_y (float): Y-position (English Metric Units) to the top-left point of page valid region
    N)rV   rT   Zget_or_add_imageZscaled_dimensionsr   Znext_idrs   r_   rt   rC   Zadd_drawing)r0   r]   rZ   rn   ro   Úrunrr   Úimagerb   rc   rl   rs   rp   r   r   r    Úadd_float_imageM  s   
r}   Úindentc                 C   sV   | j  d¡}|r)tdƒ}| tdƒtd| ƒ¡ | tdƒd¡ |d  |¡ dS dS )	z™Indent a table.
    
    Args:
        table (Table): ``python-docx`` Table object.
        indent (float): Indent value, the basic unit is 1/20 pt.
    zw:tblPrzw:tblIndr%   r   úw:typeÚdxar   N)r,   r   r   r   r   r   r:   )Útabler~   Ztbl_prr*   r   r   r    Úindent_tablec  s   ür‚   Úcellc                 K   sx   | j }| ¡ }tdƒ}dD ]'}||v r4td |¡ƒ}| tdƒt| |¡ƒ¡ | tdƒd¡ | |¡ q| |¡ dS )aë  Set cell margins. Provided values are in twentieths of a point (1/1440 of an inch).
    
    Reference: 

        * https://blog.csdn.net/weixin_44312186/article/details/104944773
        * http://officeopenxml.com/WPtableCellMargins.php
    
    Args:
        cell (_Cell): ``python-docx`` Cell instance you want to modify.
        kwargs (dict): Dict with keys: top, bottom, start, end.
        
    Usage::
    
        set_cell_margins(cell, top=50, start=50, bottom=50, end=50)    
    zw:tcMar)ÚtopÚstartÚbottomÚendúw:{}r%   r   r€   N)	Ú_tcÚget_or_add_tcPrr   r5   r   r   r   Úgetr:   )rƒ   ÚkwargsÚtcÚtcPrZtcMarÚmÚnoder   r   r    Úset_cell_marginsr  s   
€r‘   c                 C   s:   t |ƒdd…  d¡}| j ¡  td tdƒ|¡ƒ¡ dS )a  Set cell background-color.

    Reference:
        https://stackoverflow.com/questions/26752856/python-docx-set-table-cell-background-and-text-color
    
    Args:
        cell (_Cell): ``python-docx`` Cell instance you want to modify
        srgb (int): RGB color value.
    r   NrK   z<w:shd {} w:fill="{}"/>r)   )rM   rN   r‰   rŠ   r:   r   r5   r   )rƒ   rJ   rO   r   r   r    Úset_cell_shading  s   
$r’   c           
      K   s°   | j }| ¡ }| d¡}|du rtdƒ}| |¡ dD ]:}| |¡}|rUd |¡}| t|ƒ¡}|du r=t|ƒ}| |¡ dD ]}	|	|v rT| 	td |	¡ƒt
||	 ƒ¡ q?qdS )a²  Set cell`s border.
    
    Reference:
        * https://stackoverflow.com/questions/33069697/how-to-setup-cell-borders-with-python-docx
        * https://blog.csdn.net/weixin_44312186/article/details/104944110

    Args:
        cell (_Cell): ``python-docx`` Cell instance you want to modify.
        kwargs (dict): Dict with keys: top, bottom, start, end.

    Usage::
    
        set_cell_border(
            cell,
            top={"sz": 12, "val": "single", "color": "#FF0000", "space": "0"},
            bottom={"sz": 12, "color": "#00FF00", "val": "single"},
            start={"sz": 24, "val": "dashed", "shadow": "true"},
            end={"sz": 12, "val": "dashed"},
        )
    zw:tcBordersN)r…   r„   r‡   r†   ZinsideHZinsideVrˆ   )ÚszÚvalÚcolorr   Zshadow)r‰   rŠ   Zfirst_child_found_inr   r:   r‹   r5   Úfindr   r   r   )
rƒ   rŒ   r   rŽ   Z	tcBordersÚedgeZ	edge_dataÚtagÚelementÚkeyr   r   r    Úset_cell_borderž  s&   




€òr›   ÚbtLrÚ	directionc                 C   s4   | j }| ¡ }tdƒ}| tdƒ|¡ | |¡ dS )zòSet vertical text direction for cell.

    Reference:
        https://stackoverflow.com/questions/47738013/how-to-rotate-text-in-table-cells
    
    Args:
        direction (str): Either "tbRl" (top to bottom) or "btLr" (bottom to top).
    zw:textDirectionrS   N)r‰   rŠ   r   r   r   r:   )rƒ   r   r   rŽ   ZtextDirectionr   r   r    Úset_vertical_cell_directionÎ  s
   	rž   )r   r   )r   )r2   )r>   )rH   )NN)rœ   )/ry   Zdocx.sharedr   Z	docx.oxmlr   r   r   Zdocx.oxml.nsr   r   Zdocx.oxml.shaper   Zdocx.oxml.xmlchemyr	   r
   Zdocx.enum.textr   Zdocx.image.exceptionsr   Z
docx.tabler   Zdocx.opc.constantsr   Úsharer   r!   Úlistr+   r1   Úfloatr8   r=   rF   rI   r   rP   rQ   rY   r^   r_   r}   r‚   r‘   r’   r›   r   rž   r   r   r   r    Ú<module>   s<   
!4
>
0