o
    ÕjrM  ã                   @   sÖ  d Z ddlmZ zddlmZmZmZ W n ey'   ddlmZmZmZ Y nw 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lmZ dFdd„ZdGdefdd„Zdd„ Z dHde!fdd„Z"dd„ Z#dIde!fdd„Z$dJd!e!fd"d#„Z%d$e&fd%d&„Z'd$e&fd'd(„Z(d)d*„ Z)d+Z*d,d-„ Z+d.d/„ Z,d0d1„ Z-G d2d3„ d3eƒZ.ed4e.ƒ dKd6d7„Z/d8e!fd9d:„Z0d;efd<d=„Z1d;ed$e&fd>d?„Z2d;efd@dA„Z3dLd;edCe4fdDdE„Z5d5S )Mz0docx 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)Úetreeé   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    úU/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/pdf2docx/common/docx.pyÚset_equal_columns   s    r"   Ú
width_listc                 C   s¼   | j  d¡d }t|ƒdkrt|ƒdkr| ¡  dS | ¡  | tdƒtt|ƒƒ¡ | tdƒd¡ |D ]'}tdƒ}| td	ƒttd
| ƒƒ¡ | tdƒttd
| ƒƒ¡ | 	|¡ q4d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   Nr   zw:equalWidthÚ0zw:colúw:wr   r   )
r   r   ÚlenÚclearr   r   r   r   r   Úappend)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_paragraphI   s   r2   çÍÌÌÌÌÌð?Ú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_formatr4   r   Zspace_beforeZspace_afterZleft_indentZright_indentZwidow_controlÚformatr   r/   Zget_or_add_pPrÚinsertr   )r1   r4   ÚpfÚxmlr    r    r!   Úreset_paragraph_formatT   s   



r:   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   r(   r/   )r1   ZpPrÚrPrÚvr    r    r!   Úset_hidden_propertyu   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_rPrr7   r   r6   r   )Úp_runr@   r    r    r!   Úset_char_scaling‹   ó   	ÿrG   ç        r   c              	   C   rA   )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   NrC   )rF   r   r    r    r!   Úset_char_spacing˜   rH   rJ   Ú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Úzfillr6   r   rD   rE   r7   r   )rF   rK   Z	color_mapÚcr9   r    r    r!   Úset_char_shading¥   s   





úrQ   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   NrL   z%<w:u {} w:val="single" w:color="{}"/>r*   r   )rN   rO   r6   r   rD   rE   r7   r   )rF   rK   rP   r9   r    r    r!   Úset_char_underlineÃ   s   rR   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:rr;   zw:rStyleúw:valZ	Hyperlink)ÚpartZ	relate_tor   Z	HYPERLINKr   r   r   r(   ÚtextÚadd_runrD   )
r0   ÚurlrV   rU   Zr_idZ	hyperlinkZnew_runr<   ZrStyleÚrr    r    r!   Úadd_hyperlinkÏ   s    


rZ   iœ1  c                 C   s   t | t ƒS )N)r   Ú
EMU_PER_PT)Zvalue_ptr    r    r!   Ú	pt_to_emu   s   r\   c                 C   sh  t |ƒ}t |ƒ}t |ƒ}t |ƒ}	|  ¡ }
|
 |¡ |
j d¡d }| d¡d }| d¡d }| dt|ƒ¡ | dt|	ƒ¡ | d¡d }| dt|ƒ¡ | dt|	ƒ¡ | d¡d }| d	¡d }| d
¡d }tj|dd}tj|dd}tj|dd}tj|dd}dt	ddddƒ› d|› d|› d|› d|› d|› d|› d}t
|ƒ}| |¡ | |¡ dS )a)  
    Insert floating image at absolute position using points (pt).

    :param paragraph: target paragraph
    :param image_path: image path
    :param x_pt: horizontal position in pt
    :param y_pt: vertical position in pt
    :param width_pt: width in pt
    :param height_pt: height in pt
    z.//w:drawingr   z.//wp:inlinez./wp:extentÚcxÚcyz.//a:extz
./wp:docPrz./wp:cNvGraphicFramePrz./a:graphicÚunicode)Úencodingz
    <wp:anchor ÚwpÚaÚpicrY   zù
        simplePos="0"
        relativeHeight="0"
        behindDoc="0"
        locked="0"
        layoutInCell="1"
        allowOverlap="1">

        <wp:simplePos x="0" y="0"/>

        <wp:positionH relativeFrom="page">
            <wp:posOffset>zn</wp:posOffset>
        </wp:positionH>

        <wp:positionV relativeFrom="page">
            <wp:posOffset>z1</wp:posOffset>
        </wp:positionV>

        z"

        <wp:wrapNone/>

        z	
        z

    </wp:anchor>
    N)r\   rW   Úadd_picturerD   r   r   r   r   Ztostringr   r   r.   r(   )r0   Z
image_pathZx_ptZy_ptZwidth_ptZ	height_ptZx_emuZy_emuZ	width_emuZ
height_emuÚrunZdrawingÚinlineÚextentZa_extÚdocPrZcNvGraphicFramePrÚgraphicZ
extent_xmlZ	docPr_xmlZcNvGraphicFramePr_xmlZgraphic_xmlZ
anchor_xmlÚanchorr    r    r!   Úadd_floating_picture_pt  sN   
ÿôðíéèç
rk   c                 C   sN   |   ¡ }z|j|t|ƒt|ƒd W n ty    tdƒ Y dS w d| j_dS )u“  Add a floating image to a paragraph at a specific position.

    The image is inserted as a floating picture (not inline) using ``add_floating_picture_pt``,
    anchored to the given paragraph and positioned by ``x_pos`` and ``y_pos`` in Pt.

    Args:
        p (Paragraph): ``python-docx`` paragraph instance.
        image_path_or_stream (str | bytes): Image file path or inâ€‘memory image stream.
        x_pos (float): Horizontal position of the image in Pt, relative to the page.
        y_pos (float): Vertical position of the image in Pt, relative to the page.
        width (float): Image width in Pt.
        height (float): Image height in Pt.
    )ÚwidthÚheightzUnrecognized Image.Nr?   )rW   rd   r   r   Úprintr5   r4   )r1   Úimage_path_or_streamZx_posZy_posrl   rm   Z	docx_spanr    r    r!   Ú	add_image_  s   
þrp   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_xmlrg   r]   r^   rh   ÚidÚnameri   ÚgraphicDataÚuriÚ_insert_pic)Úclsr]   r^   Úshape_idrc   Úpos_xÚpos_yrj   r    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   r|   ri   ru   rw   )rx   ry   ÚrIdÚfilenamer]   r^   rz   r{   Zpic_idrc   rj   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>ra   rb   rc   rY   )r   r   )rx   rz   r{   r    r    r!   rr   §  s   íÿz_CT_Anchor._anchor_xmlN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   rg   rh   ri   Úclassmethodr|   r   rr   r    r    r    r!   rq   ‚  s    

rq   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)rW   rU   Zget_or_add_imageZscaled_dimensionsr   Znext_idr~   rq   r   rD   Zadd_drawing)r1   ro   rl   rz   r{   re   r}   Úimager]   r^   ry   r~   rj   r    r    r!   Úadd_float_imageÃ  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_tableÙ  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   r6   r   r   r   Úgetr(   )rŒ   ÚkwargsÚtcÚtcPrZtcMarÚmÚnoder    r    r!   Úset_cell_marginsè  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   NrL   z<w:shd {} w:fill="{}"/>r*   )rN   rO   r’   r“   r(   r   r6   r   )rŒ   rK   rP   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”   r6   Ú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:textDirectionrT   N)r’   r“   r   r   r   r(   )rŒ   r¦   r–   r—   ZtextDirectionr    r    r!   Úset_vertical_cell_directionD  s
   	r§   )r   r   )r   )r3   )r?   )rI   )NN)r¥   )6rƒ   Zdocx.sharedr   Z	docx.oxmlr   r   r   ÚImportErrorZdocx.oxml.parserZ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   Zsharer   Zlxmlr   r"   Úlistr,   r2   Úfloatr:   r>   rG   rJ   r   rQ   rR   rZ   r[   r\   rk   rp   rq   r†   r‹   rš   r›   r¤   r   r§   r    r    r    r!   Ú<module>   sL    þ
$!0[#
>
0