
    khA                        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Zd+defdZd Zd,defdZ d Z!d-defdZ"d.defdZ#de$fdZ%de$fdZ&d Z'd Z( G d de      Z) ede)       d/d!Z*d"efd#Z+d$efd%Z,d$ede$fd&Z-d$efd'Z.d0d$ed(e/fd)Z0y # e$ r ddlmZmZmZ Y w xY w)1z0docx 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_valuec           	          | j                   j                  d      d   }|j                  t        d      t	        |             |j                  t        d      t	        t        d|z                     y)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cols       P/var/www/teggl/fontify/venv/lib/python3.12/site-packages/pdf2docx/common/docx.pyset_equal_columnsr!      sR     //



+A
.CGGBwKS"GGByM3s2e8}-.    
width_listc           
      P   | j                   j                  d      d   }t        |      dk(  rt        |      dk7  r|j                          y|j                          |j	                  t        d      t        t        |                   |j	                  t        d      d       |D ]~  }t        d      }|j	                  t        d	      t        t        d
|z                     |j	                  t        d      t        t        d
|z                     |j                  |        y)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es         r    set_columnsr-   $   s     ??  ,Q/D :t9a< 	JJLHHR[#c*o./HHR%   	biSAY(	bmSRX/0A	r"   c                 v    | j                   }|j                         j                  |       dx| _        | _         y)zDelete a paragraph.

    Reference:    
        https://github.com/python-openxml/python-docx/issues/33#issuecomment-77661907
    N)_element	getparentremove_p)	paragraphps     r    delete_paragraphr5   H   s2     	AKKM(,,IL9%r"   line_spacingc                    | j                   }||_        t        d      |_        t        d      |_        t        d      |_        t        d      |_        d|_        dj                  t        d            }| j                  j                         j                  dt        |             dj                  t        d            }| j                  j                         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_formatr6   r   space_beforespace_afterleft_indentright_indentwidow_controlformatr   r2   get_or_add_pPrinsertr   )r4   r6   pfxmls       r    reset_paragraph_formatrC   S   s     
		B"BOeBOUBNUBNeBOB +
1
1'#,
?CDD  IcN3
*
1
1'#,
?CDD  IcN3Ir"   c                     t        d      }t        d      }t        d      }|j                  |       |j                  |       | j                  j                  |       y)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)   r2   )r4   pPrrPrvs       r    set_hidden_propertyrI   t   sH     g
C
g
CJAJJqMJJsODDKKr"   scalec           
          | j                   j                         j                  dt        dj	                  t        d      d|z                     y)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   r>   r   )p_runrJ   s     r    set_char_scalingrQ      s@     
HH$$Q)00s5yIJLr"   r   c           
          | j                   j                         j                  dt        dj	                  t        d      d|z                     y)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   NrM   )rP   r   s     r    set_char_spacingrS      s@     
HH$$Q/66ws|RXNOQr"   srgbc                 <   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                  _	        yt        |      dd j                  d	      }d
j                  t        d      |      }| j                  j                         j!                  dt#        |             y)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      N   z4<w:shd {} w:val="clear" w:color="auto" w:fill="{}"/>r+   r   )r   r   REDBRIGHT_GREENBLUEYELLOWPINK	TURQUOISEfonthighlight_colorhexzfillr>   r   rN   rO   r@   r   )rP   rT   	color_mapcrB   s        r    set_char_shadingrd      s     	'N..'N77'N//'N11'N//'N44I y%.t_

" IabM"ELLWUX\[\]!((IcN;r"   c                     t        |      dd j                  d      }dj                  t        d      |      }| j                  j                         j                  dt        |             y)zSet underline and color.
    
    Args:
        p_run (docx.text.run.Run): Proxy object wrapping <w:r> element.
        srgb (int): Color value.
    rV   NrW   z%<w:u {} w:val="single" w:color="{}"/>r+   r   )r`   ra   r>   r   rN   rO   r@   r   )rP   rT   rc   rB   s       r    set_char_underlinerf      sU     	D	!"AA
2
9
9'#,
JC	HH$$Q	#7r"   c                 &   | j                   }|j                  |t        j                  d      }t	        d      }|j                  t        d      |       |j                  t        d      d       t	        d      }t	        d      }t	        d	      }|j                  t        d
      d       |j                  |       |j                  |       ||_        |j                  |       | j                         }	|	j                  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)is_externalzw:hyperlinkzr:idz	w:history1zw:rrE   zw:rStylew:val	Hyperlink)part	relate_tor   	HYPERLINKr   r   r   r)   textadd_runrN   )
r3   urlro   rl   r_id	hyperlinknew_runrG   rStylers
             r    add_hyperlinkrw      s    " >>D>>#0::>MD M*IMM"V*d%MM"[/3' % G g
C $F
JJr'{K( JJvNN3GLW 	ADDKK	Hr"   c                     | j                         }	 |j                  |t        |      t        |             d| j
                  _        y# t        $ r t	        d       Y yw xY w)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.N      ?)rp   add_picturer   r   printr8   r6   )r4   image_path_or_streamry   rz   	docx_spans        r    	add_imager     s_     		I2"U)BvJW '+A# " #$s   &A
 
A! A!c                   p    e Zd ZdZ ed      Z ed      Z ed      Zed        Z	ed        Z
ed        Zy)	
_CT_AnchorzA
    ``<w:anchor>`` element, container for a floating image.
    z	wp:extentzwp:docPrz	a:graphicc                 J   t        | j                  ||            }||j                  _        ||j                  _        ||j
                  _        d|z  |j
                  _        d|j                  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)clsr   r   shape_idpicpos_xpos_yanchors           r    newz_CT_Anchor.new  s     3??5%89"(83F 	""& 	""..s3r"   c                     d}t        j                  |||||      }	| j                  ||||	||      }
|
j                  j                  j	                  |	       |
S )z}
        Return a new `wp:anchor` element containing the `pic:pic` element
        specified by the argument values.
        r   )r	   r   r   r   r   )r   r   rIdfilenamer   r   r   r   pic_idr   r   s              r    new_pic_anchorz_CT_Anchor.new_pic_anchor/  sT     nnVXsB;R3u=""..s3r"   c                 N    dt        dddd      t        |      t        |      fz  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>wpar   rv   )r   r   )r   r   r   s      r    r   z_CT_Anchor._anchor_xml;  s1    &  'tS%=s5z3u:W'X	
r"   N)__name__
__module____qualname____doc__r   r   r   r   classmethodr   r   r    r"   r    r   r     sa     ;'F*%EK(G   	 	 
 
r"   r   z	wp:anchorNc                 p   | j                         }|j                  j                  |      \  }}|j                  t	        |      d      \  }}	|j                  j
                  |j                  }}
t        j                  |
||||	t	        |      t	        |            }|j                  j                  |       y)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)rp   rl   get_or_add_imagescaled_dimensionsr   next_idr   r   r   rN   add_drawing)r4   r~   ry   r   r   runr   imager   r   r   r   r   s                r    add_float_imager   W  s     ))+C**+?@JC$$RY5FB))5>>hH&&xhB5	SUV[S\]FFFvr"   indentc                    | j                   j                  d      }|rbt        d      }|j                  t	        d      t        d|z               |j                  t	        d      d       |d   j                  |       yy)	zIndent 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   tbl_prr,   s       r    indent_tabler   m  sh     ^^!!),F
#	biRY(	blE"q		 r"   cellc           	      ~   | j                   }|j                         }t        d      }dD ]  }||v st        dj                  |            }|j	                  t        d      t        |j                  |                   |j	                  t        d      d       |j                  |        |j                  |       y)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   r>   r   r   r   getr)   )r   kwargstctcPrtcMarmnodes          r    set_cell_marginsr   |  s      
BD	"E. ;v}}Q/0DHHRYFJJqM 23HHR\5)LL 	KKr"   c           	          t        |      dd j                  d      }| j                  j                         j	                  t        dj                  t        d      |                   y)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.
    rV   NrW   z<w:shd {} w:fill="{}"/>r+   )r`   ra   r   r   r)   r   r>   r   )r   rT   rc   s      r    set_cell_shadingr     sT     	D	!"AAHH%%i0J0Q0QRYZ]R^`a0b&cdr"   c           	         | j                   }|j                         }|j                  d      }|t        d      }|j	                  |       dD ]  }|j                  |      }|sdj                  |      }|j                  t        |            }|t        |      }|j	                  |       dD ]=  }	|	|v s|j                  t        dj                  |	            t        ||	                ?  y)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   insideHinsideVr   )szvalcolorr   shadow)r   r   first_child_found_inr   r)   r   r>   findr   r   r   )
r   r   r   r   	tcBordersedge	edge_datatagelementkeys
             r    set_cell_borderr     s    * 
BD ))-8I.	I H MJJt$	--%C  nnRW-G%c*  ) A M)#KK6==#5 6IcN8KLMMr"   	directionc                     | j                   }|j                         }t        d      }|j                  t	        d      |       |j                  |       y)zSet 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:textDirectionrj   N)r   r   r   r   r   r)   )r   r   r   r   textDirections        r    set_vertical_cell_directionr     sF     
BD 12Mbk9-KKr"   )rV   r   )r   )g?)r{   )g        )NN)btLr)1r   docx.sharedr   	docx.oxmlr   r   r   ImportErrordocx.oxml.parserdocx.oxml.nsr   r   docx.oxml.shaper	   docx.oxml.xmlchemyr
   r   docx.enum.textr   docx.image.exceptionsr   
docx.tabler   docx.opc.constantsr   sharer   r!   listr-   r5   floatrC   rI   rQ   rS   r   rd   rf   rw   r   r   r   r   r   r   r   r   r   r   r"   r    <module>r      s!   6 NFF % & = ) 8  0 
/"D "H-5 B,
L% 
L
Q% 
Q< <<	83 	8.h+(<
 <
| [* -,u % <e% ec e-M -M`U c c  NMMNs   
B< <CC