
    TAi#~                       S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKrSSK	J
r
  SSKJr  SSKJr  SSKJr  SSKrSSKr\R$                  S:  a  SS	KJr  SS
KJr  OSSKJrJr  SSKJr  SSKJr  SSKJr  SSKJr  SrSr/ SQr/ SQr\\-   r  " S S5      r! " S S\!5      r" " S S5      r# " S S\#5      r$ " S S5      r% " S S 5      r& " S! S"\5      r' " S# S$5      r(g)%z6Contains the core functions to parse tables from PDFs.    )annotationsN)
itemgetter)Any)Iterable)Iterator)      )	TypedDict)Unpack)r
   r      )ImageConversionBackend)build_file_path_in_temp_dir)get_index_closest_point)get_textline_coords   
   leftrightmiddle)topbottomcenterc                  *    \ rS rSrSrS rS rS rSrg)TextAlignment,   a  Represents a list of textlines sharing an alignment on a coordinate.

The alignment can be left/right/middle or top/bottom/center.
(PDF coordinate space)

Parameters
----------
coord : float
    coordinate of the initial text edge. Depending on the alignment
    it could be a vertical or horizontal coordinate.
textline : obj
    the original textline to start the alignment
align : str
    Name of the alignment (e.g. "left", "top", etc)

Attributes
----------
coord : float
    The coordinate aligned averaged out across textlines.  It can be along
    the x or y axis.
textlines : array
    Array of textlines that demonstrate this alignment.
align : str
    Name of the alignment (e.g. "left", "top", etc)
c                ,    Xl         U/U l        X0l        g N)coord	textlinesalignselfr   textliner!   s       H/var/www/html/land-ocr/venv/lib/python3.13/site-packages/camelot/core.py__init__TextAlignment.__init__G   s    
"
    c                    SR                  [        S U R                  S S 5      5      R                  SS5      nSU R                   S[        U R                  5       SU S	3$ )
Nz | c                "    U R                  5       $ r   )get_textxs    r%   <lambda>(TextAlignment.__repr__.<locals>.<lambda>N   s
    !**,r(      
 z<TextEdge coord=z tl=z textlines text='z...'>)joinmapr    replacer   len)r#   text_insides     r%   __repr__TextAlignment.__repr__L   si    jj&r(:;

'$
 	 tzzl$s4>>/B.C D*m52	
r(   c                    U R                   [        U R                  5      -  U-   [        [        U R                  5      S-   5      -  U l         U R                  R	                  U5        g)z<Update new textline to this alignment, adapting its average.r   N)r   r6   r    floatappend)r#   r$   r   s      r%   register_aligned_textline'TextAlignment.register_aligned_textlineU   sT     jj3t~~#66>%!#C
 

 	h'r(   )r!   r   r    N)	__name__
__module____qualname____firstlineno____doc__r&   r8   r=   __static_attributes__ r(   r%   r   r   ,   s    4

(r(   r   c                  <   ^  \ rS rSrSrU 4S jrS rSS jrSrU =r	$ )TextEdge_   a  Defines a text edge coordinates relative to a left-bottom origin.

(PDF coordinate space)
An edge is an alignment bounded over a segment.

Parameters
----------
coord : float
    coordinate of the text edge.  Can be x or y.
y0 : float
    y-coordinate of bottommost point.
y1 : float
    y-coordinate of topmost point.
align : string, optional (default: 'left')
    {'left', 'right', 'middle'}

Attributes
----------
is_valid: bool
    A text edge is valid if it intersects with at least
    TEXTEDGE_REQUIRED_ELEMENTS horizontal text rows.

c                x   > [         TU ]  XU5        UR                  U l        UR                  U l        SU l        g )NF)superr&   y0y1is_valid)r#   r   r$   r!   	__class__s       r%   r&   TextEdge.__init__x   s/    %0++++r(   c                    [        U R                  S5      n[        U R                  S5      n[        U R                  S5      nSU SU SU SU R                   SU R
                   S3$ )Nr0   z<TextEdge x=z y0= y1=z align=z valid=>)roundr   rK   rL   r!   rM   )r#   r-   rK   rL   s       r%   r8   TextEdge.__repr__~   sd    $**a 477A477A1#T"T"WTZZLVWX	
r(   c                    [         R                  " U R                  UR                  US9(       aH  U R                  X!5        UR                  U l        [	        U R
                  5      [        :  a  SU l        ggg)zrUpdate text edge coordinates.

Update the text edge's x and bottom y coordinates and sets
the is_valid attribute.
abs_tolTN)mathiscloserK   r=   r6   r    TEXTEDGE_REQUIRED_ELEMENTSrM   )r#   r-   r$   edge_tols       r%   update_coordsTextEdge.update_coords   s\     <<h?**87kkDG 4>>"%?? $ @ @r(   )rM   rK   rL   2   )
r?   r@   rA   rB   rC   r&   r8   r\   rD   __classcell__rN   s   @r%   rG   rG   _   s    0
% %r(   rG   c                  :    \ rS rSrSrS r\S 5       rS rS r	Sr
g)	TextAlignments   z9Defines a dict of text edges across reference alignments.c                B    0 U l         U H  n/ U R                   U'   M     g r   _text_alignments)r#   alignment_namesalignment_names      r%   r&   TextAlignments.__init__   s$     "-N46D!!.1 .r(   c                    [        XU5      $ r   )r   )r   r$   r!   s      r%   _create_new_text_alignment)TextAlignments._create_new_text_alignment   s    Ue44r(   c                    [         $ r   )NotImplementedr#   	alignmentr   r$   s       r%   _update_alignment TextAlignments._update_alignment   s    r(   c                j   [        U5      nU R                  R                  5        H  u  p4X#   n[        XTS S9nSnUc  SnOFXF   R                  nUS-
  Us=:  a	  US-   :  a  O  OU R                  XF   XQ5        OX:  a  US-   nOUnUc  Mg  U R                  XQU5      n	UR                  Xy5        M     g)z1Update an existing text edge in the current dict.c                    U R                   $ r   )r   r,   s    r%   r.   3TextAlignments._register_textline.<locals>.<lambda>   s    QWWr(   )fnNr   g      ?r   )r   rg   itemsr   r   rr   rl   insert)
r#   r$   coordsalignment_idalignment_arrayr   idx_closest
idx_insertcoord_closestnew_alignments
             r%   _register_textline!TextAlignments._register_textline   s    $X.-1-B-B-H-H-J)L(E 2+<K J"
 / < B B3;<<**'4e #*!,qJ!,J% $ ? ?\!  &&zA9 .Kr(   rf   N)r?   r@   rA   rB   rC   r&   staticmethodrl   rr   r   rD   rE   r(   r%   rc   rc      s)    C7 5 5Br(   rc   c                  r   ^  \ rS rSrSrSU 4S jjrS rS rS rS r	S r
S	 rS
 rS rS rS rS rSrU =r$ )	TextEdges   zDefines a dict text edges on the PDF page.

The dict contains the left, right and middle text edges found on
the PDF page. The dict has three keys based on the alignments,
and each key's value is a list of camelot.core.TextEdge objects.
c                8   > [         TU ]  [        5        Xl        g r   )rJ   r&   HORIZONTAL_ALIGNMENTSr[   )r#   r[   rN   s     r%   r&   TextEdges.__init__   s    ./ r(   c                    [        XU5      $ r   )rG   r"   s       r%   rl   $TextEdges._create_new_text_alignment   s    //r(   c                d    U R                  XU5      nU R                  U   R                  U5        g)z(Add a new text edge to the current dict.N)rl   rg   r<   )r#   r   r$   r!   tes        r%   addTextEdges.add   s-    ,,UeDe$++B/r(   c                <    UR                  X#U R                  5        g r   )r\   r[   rp   s       r%   rr   TextEdges._update_alignment   s    ?r(   c                    U HA  n[        UR                  5       R                  5       5      S:  d  M0  U R                  U5        MC     g)z<Generates the text edges dict based on horizontal text rows.r   N)r6   r+   stripr   )r#   r    tls      r%   generateTextEdges.generate   s6    B2;;=&&()A-''+ r(   c                J   [        S U R                  S    5       5      [        S U R                  S    5       5      [        S U R                  S    5       5      S.n[        UR                  5       [	        S5      S	9S
   n[        [        S U R                  U   5      5      $ )zReturn the list of relevant text edges.

(all share the same alignment)
based on which list intersects horizontal text rows the most.
c              3  p   #    U  H,  oR                   (       d  M  [        UR                  5      v   M.     g 7fr   rM   r6   r    .0r   s     r%   	<genexpr>)TextEdges.get_relevant.<locals>.<genexpr>   s'      ,Ib[[!BLL!!,Is   66r   c              3  r   #    U  H-  nUR                   (       d  M  [        UR                  5      v   M/     g 7fr   r   r   s     r%   r   r      s,      8B;; "BLL!!8   77r   c              3  r   #    U  H-  nUR                   (       d  M  [        UR                  5      v   M/     g 7fr   r   r   s     r%   r   r      s,      9B;; "BLL!!9r   r   r   r   keyr   c                    U R                   $ r   )rM   r   s    r%   r.   (TextEdges.get_relevant.<locals>.<lambda>   s    bkkr(   )sumrg   maxrx   r   listfilter)r#   intersections_sumrelevant_aligns      r%   get_relevantTextEdges.get_relevant   s      ,0,A,A&,I   //8 
  //9 
& .446JqMJ1M)4+@+@+PQ
 	
r(   c                    UR                  S S9  U R                  U5      nU R                  X15        U R                  U5      nU Vs0 s H  oPR	                  XT5      S_M     nnU$ s  snf )a  
Return a dict of interesting table areas on the PDF page.

The table areas are calculated using relevant text edges.

Parameters
----------
textlines : list
    List of text line objects that are relevant for determining table areas.
relevant_textedges : list
    List of relevant text edge objects used to identify table areas.

Returns
-------
dict
    A dictionary with padded table areas as keys and None as values.
c                4    U R                   * U R                  4$ r   )rK   r   r   s    r%   r.   +TextEdges.get_table_areas.<locals>.<lambda>  s    /Ar(   r   N)sort_initialize_table_areas"_extend_table_areas_with_textlines"_calculate_average_textline_height_pad)r#   r    relevant_textedgestable_areasaverage_textline_heightareapadded_table_areass          r%   get_table_areasTextEdges.get_table_areas  s}    & 	$AB223EF//G #'"I"I)"TGR
GRtIId4d:{ 	 
 "!	
s   A'c                    0 nU HM  nU(       d2  SX#R                   UR                  UR                   UR                  4'   M<  U R                  X#5        MO     U$ )a
  
Initialize table areas based on relevant text edges.

Parameters
----------
relevant_textedges : list
    List of relevant text edge objects used to initialize table areas.

Returns
-------
dict
    A dictionary of table areas initialized from relevant text edges.
N)r   rK   rL   _update_table_areas)r#   r   r   r   s       r%   r   !TextEdges._initialize_table_areas#  sM     $BBFXXruubhh>?((9	 % r(   c                   SnU H/  nUR                   US   :  d  M  UR                  US   ::  d  M-  Un  O   Uc1  SXR                  UR                  UR                  UR                   4'   gUR                  U5        US   [	        UR                  US   5      [        US   UR                  5      [        US   UR                   5      4nSX'   g)z
Update table areas by checking for overlaps with new text edges.

Parameters
----------
table_areas : dict
    Current table areas to be updated.
te : object
    The new text edge object to check for overlaps.

Returns
-------
None
Nr   r   r   r0   )rL   rK   r   popminr   )r#   r   r   foundr   updated_areas         r%   r   TextEdges._update_table_areas:  s     DuuQBEET!W$4	   =>BK255"((BEE:;OOE"aBEE58$E!Hbhh'E!Hbee$	L )-K%r(   c                |   U H  nSnU H/  nUR                   US   :  d  M  UR                  US   ::  d  M-  Un  O   Uc  M?  UR                  U5        [        UR                  US   5      [        UR                   US   5      [        US   UR                  5      [        US   UR                  5      4nSX'   M     g)z
Extend table areas based on text lines that overlap vertically.

Parameters
----------
table_areas : dict
    Current table areas to be extended.
textlines : list
    List of text line objects relevant for extending table areas.

Returns
-------
None
Nr   r   r   r0   )rK   rL   r   r   x0r   x1)r#   r   r    r   r   r   r   s          r%   r   ,TextEdges._extend_table_areas_with_textlines\  s     BE#55DG#a(8 E	 $  &uQx(uQx(a"%%(a"%%(	  -1)! r(   c                f    [        S U 5       5      nU(       a  U[        [        U5      5      -  $ S$ )z
Calculate the average height of text lines.

Parameters
----------
textlines : list
    List of text line objects.

Returns
-------
float
    The average height of the text lines.
c              3  R   #    U  H  oR                   UR                  -
  v   M     g 7fr   )rL   rK   )r   r   s     r%   r   ?TextEdges._calculate_average_textline_height.<locals>.<genexpr>  s     !D)B%%"%%-)s   %'r   )r   r;   r6   )r#   r    sum_textline_heights      r%   r   ,TextEdges._calculate_average_textline_height}  s1     "!D)!DD>G"U3y>%::NQNr(   c                h    US   [         -
  nUS   [         -
  nUS   [         -   nUS   US-  -   nX4XV4$ )z
Pad a given area by a constant value.

Parameters
----------
area : tuple
    The area to be padded defined as (x0, y0, x1, y1).
average_row_height : float
    The average height of rows to use for padding.

Returns
-------
tuple
    The padded area.
r   r   r0   r      )TABLE_AREA_PADDING)r#   r   average_row_heightr   rK   r   rL   s          r%   r   TextEdges._pad  sP      !W))!W))!W))!W)A--r(   )r[   r^   )r?   r@   rA   rB   rC   r&   rl   r   rr   r   r   r   r   r   r   r   r   rD   r`   ra   s   @r%   r   r      sN    !00
@,
<"@. -D1BO"   r(   r   c                      \ rS rSrSrS rS r\S 5       r\R                  S 5       r\SS j5       r
\SS j5       r\S	 5       rS
rg)Celli  a^  Defines a cell in a table.

With coordinates relative to a
left-bottom origin. (PDF coordinate space)

Parameters
----------
x1 : float
    x-coordinate of left-bottom point.
y1 : float
    y-coordinate of left-bottom point.
x2 : float
    x-coordinate of right-top point.
y2 : float
    y-coordinate of right-top point.

Attributes
----------
lb : tuple
    Tuple representing left-bottom coordinates.
lt : tuple
    Tuple representing left-top coordinates.
rb : tuple
    Tuple representing right-bottom coordinates.
rt : tuple
    Tuple representing right-top coordinates.
left : bool
    Whether or not cell is bounded on the left.
right : bool
    Whether or not cell is bounded on the right.
top : bool
    Whether or not cell is bounded on the top.
bottom : bool
    Whether or not cell is bounded on the bottom.
text : string
    Text assigned to cell.

c                    Xl         X l        X0l        X@l        X4U l        X4U l        X24U l        X44U l        SU l        SU l	        SU l
        SU l        SU l        g )NFr2   )r   rL   x2y2lbltrbrtr   r   r   r   _textr#   r   rL   r   r   s        r%   r&   Cell.__init__  s]    ((((	

r(   c           	         [        U R                  5      n[        U R                  5      n[        U R                  5      n[        U R                  5      nSU SU SU SU S3	$ )Nz	<Cell x1=rQ   z x2=z y2=rR   )rS   r   rL   r   r   r   s        r%   r8   Cell.__repr__  sW    477^477^477^477^2$d2$d2$d2$a88r(   c                    U R                   $ r   )r   r#   s    r%   text	Cell.text  s    zzr(   c                H    SR                  U R                  U/5      U l        g )Nr2   )r3   r   )r#   ts     r%   r   r     s    WWdjj!_-
r(   c                T    U R                   (       + =(       d    U R                  (       + $ )z'Whether or not cell spans horizontally.)r   r   r   s    r%   hspan
Cell.hspan  s     99}.DJJ.r(   c                T    U R                   (       + =(       d    U R                  (       + $ )z%Whether or not cell spans vertically.)r   r   r   s    r%   vspan
Cell.vspan  s     88|.4;;.r(   c                h    U R                   U R                  -   U R                  -   U R                  -   $ )z1The number of sides on which the cell is bounded.)r   r   r   r   r   s    r%   bound
Cell.bound  s)     xx$++%		1DJJ>>r(   )r   r   r   r   r   r   r   r   r   r   r   rL   r   N)returnbool)r?   r@   rA   rB   rC   r&   r8   propertyr   setterr   r   r   rD   rE   r(   r%   r   r     sz    %N9   
[[. . / / / / ? ?r(   r   c                      \ rS rSrSrS rS rS r\S 5       r	\S 5       r
S rS	 rSS
 jrS rS rS rS rS rS rSS jrS rS rS rS rS rS rS rS rSrg)Tablei  a|  Defines a table with coordinates relative to a left-bottom origin.

(PDF coordinate space)

Parameters
----------
cols : list
    List of tuples representing column x-coordinates in increasing
    order.
rows : list
    List of tuples representing row y-coordinates in decreasing
    order.

Attributes
----------
df : :class:`pandas.DataFrame`
shape : tuple
    Shape of the table.
accuracy : float
    Accuracy with which text was assigned to the cell.
whitespace : float
    Percentage of whitespace in the table.
filename : str
    Path of the original PDF
order : int
    Table number on PDF page.
page : int
    PDF page number.

c                   Xl         X l        U VVs/ s H-  o1 Vs/ s H  n[        US   US   US   US   5      PM     snPM/     snnU l        [        R
                  " 5       U l        SU l        SU l        SU l	        S U l
        S U l        S U l        S U l        S U l        S U l        S U l        S U l        S U l        S U l        g s  snf s  snnf )Nr   r   )r   r   )colsrowsr   cellspd	DataFramedfshapeaccuracy
whitespacefilenameorderpageflavorpdf_size_bboxparseparse_details_image_image_path)r#   r   r   rcs        r%   r&   Table.__init__  s    		LPQDqTBTtAaD!A$!ad3TBDQ
,,.

	

! CQs   	C
$CC
C
c                P    SU R                   R                   SU R                   S3$ )zXReturn a string representation of the class .

Returns
-------
[type]
    [description]
<z shape=rR   )rN   r?   r   r   s    r%   r8   Table.__repr__0  s(     4>>**+74::,a@@r(   c                    U R                   UR                   :X  a  U R                  UR                  :  a  gU R                   UR                   :  a  gg)zReturn True if the two pages are less than the current page .

Parameters
----------
other : [type]
    [description]

Returns
-------
[type]
    [description]
TN)r  r  )r#   others     r%   __lt__Table.__lt__:  s?     99

"zzEKK'99uzz! "r(   c                    / nU R                    H<  nUR                  U Vs/ s H  o3R                  R                  5       PM     sn5        M>     U$ s  snf )z1Returns two-dimensional list of strings in table.)r   r<   r   r   )r#   drowcells       r%   data
Table.dataM  sE     ::CHHC8CDiioo'C89  9s   #A
c                    [        U R                  S5      [        U R                  S5      U R                  U R                  S.nU$ )z`Returns a parsing report.

with % accuracy, % whitespace,
table number on page and page number.
r0   )r   r   r  r  )rS   r   r   r  r  )r#   reports     r%   parsing_reportTable.parsing_reportU  s<     dmmQ/3ZZII	
 r(   c                f   U R                   c  U R                  cg  [        [        R                  R                  U R                  5      S5      U l        [        SS9nUR                  U R                  U R                  5        [        R                  " U R                  5      U l         U R                   $ )zCompute pdf image and cache it.z.pngT)use_fallback)r	  r
  r   ospathbasenamer  r   convertcv2imread)r#   backends     r%   get_pdf_imageTable.get_pdf_imagee  s    ;;'#>GG$$T]]3V$  1dCt/?/?@**T%5%56DK{{r(   c                v    U R                    H(  nU H  nS=Ul        =Ul        =Ul        Ul        M!     M*     U $ )zSet all table edges to True.T)r   r   r   r   r   )r#   r  r  s      r%   set_all_edgesTable.set_all_edgesq  s=    ::CBFF	FDJFDK   r(   c                J    U R                  X5        U R                  X#5        U $ )aq  Set the edges of the joint.

Set a cell's edges to True depending on whether the cell's
coordinates overlap with the line's coordinates within a
tolerance.

Parameters
----------
vertical : list
    List of detected vertical lines.
horizontal : list
    List of detected horizontal lines.
joint_tol : int, optional
    Tolerance for determining proximity, by default 2
)_set_vertical_edges_set_horizontal_edges)r#   vertical
horizontal	joint_tols       r%   	set_edgesTable.set_edgesx  s%      	  5"":9r(   c                n    [        U5       H&  u  pE[        R                  " X%S   US9(       d  M$  Us  $    g )Nr   rV   )	enumeraterX   rY   )r#   rz   r   r2  ir   s         r%   _find_close_pointTable._find_close_point  s2    f%DA||EQ4;; & r(   c                4   U H  nU R                  U R                  US   U5      nUc  M(  U R                  U R                  US   U5      nUc  [        U R                  5      nU R                  U R                  US   U5      nU R	                  XEU5        M     g )Nr   r   r   )r8  r   r6   r   _update_vertical_edges)r#   r0  r2  vstartendr7  s          r%   r.  Table._set_vertical_edges  s    A **499adIFE}((AaD)DC{$))n&&tyy!A$	BA''A6 r(   c                   UcI  [        U R                  5      S-
  nUS:  a*  [        X5       H  nSU R                  U   U   l        M     g g US:X  a*  [        X5       H  nSU R                  U   S   l        M     g [        X5       H4  nSU R                  U   U   l        SU R                  U   US-
     l        M6     g Nr   r   T)r6   r   ranger   r   r   r#   r=  r>  indexjs        r%   r;  Table._update_vertical_edges  s    =		NQ&Ezu*A15DJJqM%(. +  aZ5&(,

1a % ' 5&,0

1e$)15

1eai(. 'r(   c                4   U H  nU R                  U R                  US   U5      nUc  M(  U R                  U R                  US   U5      nUc  [        U R                  5      nU R                  U R                  US   U5      nU R	                  XEU5        M     g )Nr   r0   r   )r8  r   r6   r   _update_horizontal_edges)r#   r1  r2  hr=  r>  r7  s          r%   r/  Table._set_horizontal_edges  s    A **499adIFE}((AaD)DC{$))n&&tyy!A$	BA))%a8 r(   c                   UcI  [        U R                  5      S-
  nUS:  a*  [        X5       H  nSU R                  U   U   l        M     g g US:X  a*  [        X5       H  nSU R                  S   U   l        M     g [        X5       H4  nSU R                  U   U   l        SU R                  US-
     U   l        M6     g rA  )r6   r   rB  r   r   r   rC  s        r%   rH  Table._update_horizontal_edges  s    =		NQ&Ezu*A26DJJu%a(/ +  aZ5&'+

1a $ ' 5&+/

5!!$(26

519%a(/ 'r(   c                  ^ [        U R                  5      n[        U R                  5      mUS:X  d  TS:X  a  U $ [        U R                  5      U:w  d$  [	        U4S jU R                   5       5      (       a  [        S5      e[        U5       H4  nSU R                  U   S   l        SU R                  U   TS-
     l        M6     [        T5       H4  nSU R                  S   U   l	        SU R                  US-
     U   l
        M6     U $ )z Sets table border edges to True.r   c              3  @   >#    U  H  n[        U5      T:g  v   M     g 7fr   r6   )r   r  num_colss     r%   r   #Table.set_border.<locals>.<genexpr>  s      .
,6SCH Js   zQInconsistent cells structure: cells should match the dimensions of rows and cols.Tr   )r6   r   r   r   any
ValueErrorrB  r   r   r   r   )r#   num_rows	row_index	col_indexrP  s       @r%   
set_borderTable.set_border  s    tyy>tyy> q=HMK tzz?h&# .
,0JJ.
 +
 +
 c 
 xI,0DJJy!!$)8<DJJy!(Q,/5 )
 xI+/DJJqM)$(9=DJJx!|$Y/6 ) r(   Nc                    Uc  U $ U H3  nUS:X  a  U R                  5         M  US:X  d  M#  U R                  5         M5     U $ )a  
Copies over text in empty spanning cells.

Parameters
----------
copy_text : list of str, optional (default: None)
    Select one or more of the following strings: {'h', 'v'} to specify
    the direction in which text should be copied over when a cell spans
    multiple rows or columns.

Returns
-------
camelot.core.Table
    The updated table with copied text in spanning cells.
rI  r<  )_copy_horizontal_text_copy_vertical_text)r#   	copy_text	directions      r%   copy_spanning_textTable.copy_spanning_text  sH      K"IC**,c!((*	 # r(   c                   [        [        U R                  5      5       H  n[        [        U R                  U   5      5       H  nU R                  U   U   R                  R	                  5       S:X  d  M3  U R                  U   U   R
                  (       d  MV  U R                  U   U   R                  (       a  My  U R                  U   US-
     R                  U R                  U   U   l        M     M     g)z
Copies text horizontally in empty spanning cells.

This method iterates through the cells and fills empty cells that span
horizontally with the text from the left adjacent cell.

Returns
-------
None
r2   r   N)rB  r6   r   r   r   r   r   r#   r7  rE  s      r%   rZ  Table._copy_horizontal_text   s     s4::'A3tzz!}-.JJqM!$))//1R7

1a(... JJqM!,111,0JJqM!a%,@,E,EDJJqM!$) / (r(   c                   [        [        U R                  5      5       H  n[        [        U R                  U   5      5       H  nU R                  U   U   R                  R	                  5       S:X  d  M3  U R                  U   U   R
                  (       d  MV  U R                  U   U   R                  (       a  My  U R                  US-
     U   R                  U R                  U   U   l        M     M     g)z
Copies text vertically in empty spanning cells.

This method iterates through the cells and fills empty cells that span
vertically with the text from the top adjacent cell.

Returns
-------
None
r2   r   N)rB  r6   r   r   r   r   r   ra  s      r%   r[  Table._copy_vertical_text  s     s4::'A3tzz!}-.JJqM!$))//1R7

1a(... JJqM!,000,0JJq1u,=a,@,E,EDJJqM!$) / (r(   c                n    SSSSS.nUR                  U5        U R                  R                  " U40 UD6  g)zWrite Table(s) to a comma-separated values (csv) file.

For kwargs, check :meth:`pandas.DataFrame.to_csv`.

Parameters
----------
path : str
    Output filepath.

utf-8Fr   )encodingrD  headerquotingN)updater   to_csv)r#   r"  kwargskws       r%   rk  Table.to_csv(  s3     "EUqQ
		&t"r"r(   c                    SS0nUR                  U5        U R                  R                  " S0 UD6n[        US5       nUR	                  U5        SSS5        g! , (       d  f       g= f)zWrite Table(s) to a JSON file.

For kwargs, check :meth:`pandas.DataFrame.to_json`.

Parameters
----------
path : str
    Output filepath.

orientrecordswNrE   )rj  r   to_jsonopenwrite)r#   r"  rl  rm  json_stringfs         r%   rs  Table.to_json7  sP     	"
		&ggoo++$_GGK  __s   A
A'c                    SS0nSU R                    SU R                   3nUR                  U5        [        R                  " U5      nU R
                  R                  " U4SU0UD6  g)zWrite Table(s) to an Excel file.

For kwargs, check :meth:`pandas.DataFrame.to_excel`.

Parameters
----------
path : str
    Output filepath.

rg  rf  page--table-
sheet_nameN)r  r  rj  r   ExcelWriterr   to_excel)r#   r"  rl  rm  r|  writers         r%   r~  Table.to_excelH  s^     '"TYYKwtzzl;

		&%=J="=r(   c                    U R                   R                  " S0 UD6n[        USSS9 nUR                  U5        SSS5        g! , (       d  f       g= f)zWrite Table(s) to an HTML file.

For kwargs, check :meth:`pandas.DataFrame.to_html`.

Parameters
----------
path : str
    Output filepath.

rr  rf  rg  NrE   )r   to_htmlrt  ru  )r#   r"  rl  html_stringrw  s        r%   r  Table.to_htmlY  s?     ggoo//$g.!GGK  /..   A
Ac                    U R                   R                  " S0 UD6n[        USSS9 nUR                  U5        SSS5        g! , (       d  f       g= f)zWrite Table(s) to a Markdown file.

For kwargs, check :meth:`pandas.DataFrame.to_markdown`.

Parameters
----------
path : str
    Output filepath.

rr  rf  r  NrE   )r   to_markdownrt  ru  )r#   r"  rl  	md_stringrw  s        r%   r  Table.to_markdownh  sA     GG''1&1	$g.!GGI /..r  c                   SSS.nUR                  U5        [        R                  " U5      nSU R                   SU R                   3nU R
                  R                  " XT40 UD6  UR                  5         UR                  5         g)zWrite Table(s) to sqlite database.

For kwargs, check :meth:`pandas.DataFrame.to_sql`.

Parameters
----------
path : str
    Output filepath.

r5   F)	if_existsrD  rz  r{  N)	rj  sqlite3connectr  r  r   to_sqlcommitclose)r#   r"  rl  rm  conn
table_names         r%   	to_sqliteTable.to_sqlitew  si     %u5
		&t$TYYKwtzzl;
z.2.

r(   )r  r	  r
  r   r   r   r   r  r  r  r  r  r  r  r   r   r   )r0   r   )r?   r@   rA   rB   rC   r&   r8   r  r   r  r  r(  r+  r3  r8  r.  r;  r/  rH  rW  r^  rZ  r[  rk  rs  r~  r  r  r  rD   rE   r(   r%   r   r     s    > (A&    
(7697:6F(F(#!">"!r(   r   c                  B    \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   S	rg
)_Kwi  z.Helper class to define file related arguments.zos.PathLike[Any] | strr"  strdirnamerootextrE   N)r?   r@   rA   rB   rC   __annotations__rD   rE   r(   r%   r  r    s    8
  L
I	Hr(   r  c                      \ rS rSrSrSS jrS rS rS rSS jr	SS jr
\S	 5       r\SS
 j5       rSSS jjrSS jrSSS jjrSrg)	TableListi  zDefines a list of camelot.core.Table objects.

Each table can be accessed using its index.

Attributes
----------
n : int
    Number of tables in the list.

c                    Xl         g r   _tables)r#   tabless     r%   r&   TableList.__init__  s    (.r(   c                P    SU R                   R                   SU R                   S3$ )Nr  z n=rR   )rN   r?   nr   s    r%   r8   TableList.__repr__  s&    4>>**+3tvvha88r(   c                ,    [        U R                  5      $ r   )r6   r  r   s    r%   __len__TableList.__len__  s    4<<  r(   c                     U R                   U   $ r   r  )r#   idxs     r%   __getitem__TableList.__getitem__  s    ||C  r(   c                ,    [        U R                  5      $ r   )iterr  r   s    r%   __iter__TableList.__iter__  s    DLL!!r(   c                    [        U 5      $ r   )nextr   s    r%   __next__TableList.__next__  s    Dzr(   c                     [        U SU 35      $ )Nto_)getattr)tablerw  s     r%   _format_funcTableList._format_func  s    uA3i((r(   c                    [        U 5      $ )z!The number of tables in the list.rO  r   s    r%   r  TableList.n  s     4yr(   Nc                    US   nUS   nUS   nU R                    H[  nU SUR                   SUR                   U 3n[        R                  R                  X75      nU R                  Xa5      n	U	" U5        M]     g )Nr  r  r  -page-r{  )r  r  r  r!  r"  r3   r  )
r#   rw  rl  r  r  r  r  r  filepath	to_formats
             r%   _write_fileTableList._write_file  sx    #f~Um\\Evejj\cUKHww||G6H))%3Ih	 "r(   c                   US   nUS   nUS   nUS   n[         R                  R                  [         R                  R                  U5      U5      S-   n[        R
                  " USSS9 nU R                   Hq  nU S	UR                   S
UR                   U 3n	[         R                  R                  X95      n
UR                  U
[         R                  R                  U
5      5        Ms     S S S 5        g ! , (       d  f       g = f)Nr"  r  r  r  .ziprr  T
allowZip64r  r{  )r!  r"  r3   r  zipfileZipFiler  r  r  ru  r#  )r#   rl  r"  r  r  r  zipnamezr  r  r  s              r%   _compress_dirTableList._compress_dir  s    f~#f~Um'',,rwwt4d;fD__Wcd;q"V6%**WU[[M#O77<<:"''"2"28"<= & <;;s   +BC66
Dc                   [         R                  R                  U5      n[         R                  R                  U5      n[         R                  R	                  U5      u  pgU(       a  [
        R                  " 5       nXXgS.nUS;   a/  U R                  " SSU0UD6  U(       a  U R                  " S0 UD6  ggUS:X  Ga$  [         R                  R                  XE5      n	[        R                  " U	5      n
U R                   H8  nSUR                   SUR                   3nUR                  R!                  XS9  M:     U
R#                  5         U(       a  [         R                  R                  [         R                  R                  U5      U5      S-   n[$        R&                  " US	S
S9 nUR)                  U	[         R                  R                  U	5      5        SSS5        ggUS:X  a  [         R                  R                  XE5      n	U R                   H  nUR+                  U	5        M     U(       a  [         R                  R                  [         R                  R                  U5      U5      S-   n[$        R&                  " US	S
S9 nUR)                  U	[         R                  R                  U	5      5        SSS5        ggg! , (       d  f       g= f! , (       d  f       g= f)zExport the list of tables to specified file format.

Parameters
----------
path : str
    Output filepath.
f : str
    File format. Can be csv, excel, html, json, markdown or sqlite.
compress : bool
    Whether or not to add files to a ZIP archive.

)r"  r  r  r  )csvhtmljsonmarkdownrw  excelrz  r{  )r|  r  rr  Tr  NsqliterE   )r!  r"  r  r#  splitexttempfilemkdtempr  r  r3   r   r}  r  r  r  r   r~  r  r  r  ru  r  )r#   r"  rw  compressr  r#  r  r  rl  r  r  r  r|  r  r  s                  r%   exportTableList.export  s    ''//$'77##D)GG$$X.	&&(G#R33+q+F+"",V, '\ww||G6H^^H-F$UZZL}E
!!&!@ & LLN'',,rwwt'<dCfL__WcdCqGGHbgg&6&6x&@A DC  (]ww||G6H) &'',,rwwt'<dCfL__WcdCqGGHbgg&6&6x&@A DC 	  DC DCs   '0K0K
K
K!r  )r  zIterable[Table]r   None)r   zIterator[Table])r   r   )r   intr   )rl  zUnpack[_Kw]r   r  )r  F)r"  r  )r?   r@   rA   rB   rC   r&   r8   r  r  r  r  r   r  r   r  r  r  r  rD   rE   r(   r%   r  r    sc    	/9!!" ) )   
>+B +Br(   r  ))rC   
__future__r   rX   r!  r  sysr  r  operatorr   typingr   r   r   r%  pandasr   version_infor
   r   typing_extensionsbackendsr   utilsr   r   r   rZ   r   r   VERTICAL_ALIGNMENTSALL_ALIGNMENTSr   rG   rc   r   r   r   r  r  rE   r(   r%   <module>r     s    < "  	  
       
  w 3 , . * &
    4 1 &)<<0( 0(f3%} 3%l/B /Bd\  \ ~S? S?lL L^) hB hBr(   