
    i#~                       d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlmZ ddlmZ ddlmZ ddlZddlZej$                  dk\  rdd	lmZ dd
lmZ n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ZdZg dZg dZeez   Z  G d d      Z! G d de!      Z" G d d      Z# G d de#      Z$ G d d      Z% G d d       Z& G d! d"e      Z' G d# d$      Z(y)%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                  "    e Zd ZdZd Zd Zd Zy)TextAlignmenta  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                0    || _         |g| _        || _        y N)coord	textlinesalignselfr   textliner    s       ?/home/agent/.local/lib/python3.12/site-packages/camelot/core.py__init__zTextAlignment.__init__G   s    
"
    c                    dj                  t        d | j                  d d             j                  dd      }d| j                   dt        | j                         d| d	S )
Nz | c                "    | j                         S r   )get_textxs    r$   <lambda>z(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__zTextAlignment.__repr__L   si    jj&r(:;

'$
 	 tzzl$s4>>/B.C D*m52	
r&   c                    | j                   t        | j                        z  |z   t        t        | j                        dz         z  | _         | j                  j	                  |       y)z<Update new textline to this alignment, adapting its average.r   N)r   r3   r   floatappend)r"   r#   r   s      r$   register_aligned_textlinez'TextAlignment.register_aligned_textlineU   sT     jj3t~~#66>%!#C
 

 	h'r&   N)__name__
__module____qualname____doc__r%   r5   r9    r&   r$   r   r   ,   s    4

(r&   r   c                  0     e Zd ZdZ fdZd ZddZ xZS )TextEdgeaf  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                z    t         |   |||       |j                  | _        |j                  | _        d| _        y )NF)superr%   y0y1is_valid)r"   r   r#   r    	__class__s       r$   r%   zTextEdge.__init__x   s1    %0++++r&   c                    t        | j                  d      }t        | j                  d      }t        | j                  d      }d| d| d| d| j                   d| j
                   dS )Nr-   z<TextEdge x=z y0= y1=z align=z valid=>)roundr   rC   rD   r    rE   )r"   r+   rC   rD   s       r$   r5   zTextEdge.__repr__~   sd    $**a 477A477A1#T"T"WTZZLVWX	
r&   c                    t        j                  | j                  |j                  |      rH| j                  ||       |j                  | _        t	        | j
                        t        kD  rd| _        yyy)zUpdate text edge coordinates.

        Update the text edge's x and bottom y coordinates and sets
        the is_valid attribute.
        abs_tolTN)mathiscloserC   r9   r3   r   TEXTEDGE_REQUIRED_ELEMENTSrE   )r"   r+   r#   edge_tols       r$   update_coordszTextEdge.update_coords   s^     <<h?**8Q7kkDG 4>>"%?? $ @ @r&   2   )r:   r;   r<   r=   r%   r5   rR   __classcell__rF   s   @r$   r@   r@   _   s    0
%r&   r@   c                  2    e Zd ZdZd Zed        Zd Zd Zy)TextAlignmentsz9Defines a dict of text edges across reference alignments.c                >    i | _         |D ]  }g | j                   |<    y r   )_text_alignments)r"   alignment_namesalignment_names      r$   r%   zTextAlignments.__init__   s)     "- 	7N46D!!.1	7r&   c                    t        | ||      S r   )r   )r   r#   r    s      r$   _create_new_text_alignmentz)TextAlignments._create_new_text_alignment   s    UHe44r&   c                    t         S r   )NotImplementedr"   	alignmentr   r#   s       r$   _update_alignmentz TextAlignments._update_alignment   s    r&   c                r   t        |      }| j                  j                         D ]  \  }}||   }t        ||d       }d}|d}nG||   j                  }|dz
  |cxk  r|dz   k  rn n| j                  ||   ||       n||k  r|dz   }n|}|k| j                  |||      }	|j                  ||	        y)z1Update an existing text edge in the current dict.c                    | j                   S r   )r   r*   s    r$   r,   z3TextAlignments._register_textline.<locals>.<lambda>   s
    QWW r&   )fnNr   g      ?r   )r   rZ   itemsr   r   rc   r^   insert)
r"   r#   coordsalignment_idalignment_arrayr   idx_closest
idx_insertcoord_closestnew_alignments
             r$   _register_textlinez!TextAlignments._register_textline   s    $X.-1-B-B-H-H-J 	B)L/<(E 2+<K J"
 / < B B3;<<**'4eX #U*!,qJ!,J% $ ? ?8\!  &&z=A9	Br&   N)	r:   r;   r<   r=   r%   staticmethodr^   rc   rp   r>   r&   r$   rX   rX      s)    C7 5 5Br&   rX   c                  f     e Zd ZdZd fd	Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Z xZS )	TextEdgeszDefines 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                :    t         |   t               || _        y r   )rB   r%   HORIZONTAL_ALIGNMENTSrQ   )r"   rQ   rF   s     r$   r%   zTextEdges.__init__   s    ./ r&   c                    t        |||      S r   )r@   r!   s       r$   r^   z$TextEdges._create_new_text_alignment   s    x//r&   c                f    | j                  |||      }| j                  |   j                  |       y)z(Add a new text edge to the current dict.N)r^   rZ   r8   )r"   r   r#   r    tes        r$   addzTextEdges.add   s/    ,,UHeDe$++B/r&   c                >    |j                  ||| j                         y r   )rR   rQ   ra   s       r$   rc   zTextEdges._update_alignment   s    x?r&   c                    |D ]>  }t        |j                         j                               dkD  s.| j                  |       @ y)z<Generates the text edges dict based on horizontal text rows.r   N)r3   r)   striprp   )r"   r   tls      r$   generatezTextEdges.generate   s;     	,B2;;=&&()A-''+	,r&   c                P   t        d | j                  d   D              t        d | j                  d   D              t        d | j                  d   D              d}t        |j                         t	        d      	      d
   }t        t        d | j                  |               S )zReturn the list of relevant text edges.

        (all share the same alignment)
        based on which list intersects horizontal text rows the most.
        c              3  `   K   | ]&  }|j                   st        |j                         ( y wr   rE   r3   r   .0rx   s     r$   	<genexpr>z)TextEdges.get_relevant.<locals>.<genexpr>   s$      &(R[[BLL!s   ..r   c              3  ^   K   | ]%  }|j                   rt        |j                         ' y wr   r   r   s     r$   r   z)TextEdges.get_relevant.<locals>.<genexpr>   s(      ;; BLL!   +-r   c              3  ^   K   | ]%  }|j                   rt        |j                         ' y wr   r   r   s     r$   r   z)TextEdges.get_relevant.<locals>.<genexpr>   s(      ;; BLL!r   r   r   r   keyr   c                    | j                   S r   )rE   rx   s    r$   r,   z(TextEdges.get_relevant.<locals>.<lambda>   s
    bkk r&   )sumrZ   maxrg   r   listfilter)r"   intersections_sumrelevant_aligns      r$   get_relevantzTextEdges.get_relevant   s      ,0,A,A&,I   //8 
  //9 
& .446JqMJ1M)4+@+@+PQ
 	
r&   c                    |j                  d        | j                  |      }| j                  ||       | j                  |      }|D ci c]  }| j	                  ||      d }}|S c c}w )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    | j                    | j                  fS r   )rC   r   r   s    r$   r,   z+TextEdges.get_table_areas.<locals>.<lambda>  s    /A r&   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_areaszTextEdges.get_table_areas  s    & 	$AB223EF//YG #'"I"I)"TGR
?CDIId34d:
 
 "!	
s   A*c                    i }|D ]H  }|s2d||j                   |j                  |j                   |j                  f<   7| j                  ||       J |S )aZ  
        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   rC   rD   _update_table_areas)r"   r   r   rx   s       r$   r   z!TextEdges._initialize_table_areas#  sV     $ 	:BBFRXXruubhh>?((b9		: r&   c                   d}|D ]+  }|j                   |d   k\  s|j                  |d   k  s)|} n |2d||j                  |j                  |j                  |j                   f<   y|j                  |       |d   t	        |j                  |d         t        |d   |j                        t        |d   |j                         f}d||<   y)aA  
        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   r-   )rD   rC   r   popminr   )r"   r   rx   foundr   updated_areas         r$   r   z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                l   |D ]  }d}|D ]+  }|j                   |d   k\  s|j                  |d   k  s)|} n |8|j                  |       t        |j                  |d         t        |j                   |d         t        |d   |j                        t        |d   |j                        f}d||<    y)aT  
        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   r-   )rC   rD   r   r   x0r   x1)r"   r   r   r}   r   r   r   s          r$   r   z,TextEdges._extend_table_areas_with_textlines\  s      	1BE# 55DG#a(8 E	  &uQx(uQx(a"%%(a"%%(	  -1L)!	1r&   c                \    t        d |D              }|r|t        t        |            z  S dS )a  
        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  N   K   | ]  }|j                   |j                  z
    y wr   )rD   rC   )r   r}   s     r$   r   z?TextEdges._calculate_average_textline_height.<locals>.<genexpr>  s     !DB"%%"%%-!Ds   #%r   )r   r7   r3   )r"   r   sum_textline_heights      r$   r   z,TextEdges._calculate_average_textline_height}  s1     "!D)!DD>G"U3y>%::NQNr&   c                l    |d   t         z
  }|d   t         z
  }|d   t         z   }|d   |dz  z   }||||fS )a\  
        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   r-   r      )TABLE_AREA_PADDING)r"   r   average_row_heightr   rC   r   rD   s          r$   r   zTextEdges._pad  sT      !W))!W))!W))!W)A--BBr&   rS   )r:   r;   r<   r=   r%   r^   ry   rc   r~   r   r   r   r   r   r   r   rU   rV   s   @r$   rs   rs      sI    !00
@,
<"@. -D1BO" r&   rs   c                      e Zd ZdZd Zd Zed        Zej                  d        Zed
d       Z	ed
d       Z
ed        Zy	)Cella  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                    || _         || _        || _        || _        ||f| _        ||f| _        ||f| _        ||f| _        d| _        d| _	        d| _
        d| _        d| _        y )NFr/   )r   rD   x2y2lbltrbrtr   r   r   r   _textr"   r   rD   r   r   s        r$   r%   zCell.__init__  sm    r(r(r(r(	

r&   c           	         t        | j                        }t        | j                        }t        | j                        }t        | j                        }d| d| d| d| d	S )Nz	<Cell x1=rH   z x2=z y2=rI   )rJ   r   rD   r   r   r   s        r$   r5   zCell.__repr__  sW    477^477^477^477^2$d2$d2$d2$a88r&   c                    | j                   S r   )r   r"   s    r$   textz	Cell.text  s    zzr&   c                H    dj                  | j                  |g      | _        y )Nr/   )r0   r   )r"   ts     r$   r   z	Cell.text  s    WWdjj!_-
r&   c                :    | j                    xs | j                   S )z'Whether or not cell spans horizontally.)r   r   r   s    r$   hspanz
Cell.hspan  s     99}.DJJ.r&   c                :    | j                    xs | j                   S )z%Whether or not cell spans vertically.)r   r   r   s    r$   vspanz
Cell.vspan  s     88|.4;;.r&   c                h    | j                   | j                  z   | j                  z   | j                  z   S )z1The number of sides on which the cell is bounded.)r   r   r   r   r   s    r$   boundz
Cell.bound  s)     xx$++%		1DJJ>>r&   N)returnbool)r:   r;   r<   r=   r%   r5   propertyr   setterr   r   r   r>   r&   r$   r   r     sz    %N9   
[[. . / / / / ? ?r&   r   c                      e Zd ZdZd Zd Zd Zed        Zed        Z	d Z
d Zdd	Zd
 Zd Zd Zd Zd Zd ZddZd Zd Zd Zd Zd Zd Zd Zd Zy)Tablea  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                   || _         || _        |D cg c]*  }|D cg c]  }t        |d   |d   |d   |d          c}, c}}| _        t	        j
                         | _        d| _        d| _        d| _	        d | _
        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        y c c}w c c}}w )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%   zTable.__init__  s    		LPQqTBtAaD!A$!ad3BQ
,,.

	

! CQs   	C!CCCc                P    d| j                   j                   d| j                   dS )zReturn a string representation of the class .

        Returns
        -------
        [type]
            [description]
        <z shape=rI   )rF   r:   r   r   s    r$   r5   zTable.__repr__0  s(     4>>**+74::,a@@r&   c                    | j                   |j                   k(  r| j                  |j                  k  ry| j                   |j                   k  ryy)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__zTable.__lt__:  s?     99

"zzEKK'99uzz! "r&   c                    g }| j                   D ]9  }|j                  |D cg c]  }|j                  j                          c}       ; |S c c}w )z1Returns two-dimensional list of strings in table.)r   r8   r   r|   )r"   drowcells       r$   dataz
Table.dataM  sH     :: 	:CHHC8Ddiioo'89	: 9s   !A
c                    t        | j                  d      t        | j                  d      | j                  | j                  d}|S )zxReturns a parsing report.

        with % accuracy, % whitespace,
        table number on page and page number.
        r-   )r   r   r   r   )rJ   r   r   r   r   )r"   reports     r$   parsing_reportzTable.parsing_reportU  s<     dmmQ/3ZZII	
 r&   c                f   | j                   | j                  jt        t        j                  j                  | j                        d      | _        t        d      }|j                  | j                  | j                         t        j                  | j                        | _         | j                   S )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_imagezTable.get_pdf_imagee  s    ;;'#>GG$$T]]3V$  1dCt/?/?@**T%5%56DK{{r&   c                n    | j                   D ]%  }|D ]  }dx|_        x|_        x|_        |_          ' | S )zSet all table edges to True.T)r   r   r   r   r   )r"   r   r   s      r$   set_all_edgeszTable.set_all_edgesq  sK    :: 	GC GBFF	FDJFDKG	G r&   c                N    | j                  ||       | j                  ||       | S )a  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_edgeszTable.set_edgesx  s)      	  95"":y9r&   c                h    t        |      D ]$  \  }}t        j                  ||d   |      s"|c S  y )Nr   rL   )	enumeraterN   rO   )r"   ri   r   r  ir   s         r$   _find_close_pointzTable._find_close_point  s8    f% 	DAq||E1Q4;	 r&   c                ,   |D ]  }| j                  | j                  |d   |      }|&| j                  | j                  |d   |      }|t        | j                        }| j                  | j                  |d   |      }| j	                  |||        y )Nr   r   r   )r  r   r3   r   _update_vertical_edges)r"   r   r  vstartendr  s          r$   r   zTable._set_vertical_edges  s     
	7A **499adIFE}((AaD)DC{$))n&&tyy!A$	BA''sA6
	7r&   c                v   |Gt        | j                        dz
  }|dk\  r)t        ||      D ]  }d| j                  |   |   _         y y |dk(  r)t        ||      D ]  }d| j                  |   d   _         y t        ||      D ]3  }d| j                  |   |   _        d| j                  |   |dz
     _        5 y Nr   r   T)r3   r   ranger   r   r   r"   r  r  indexjs        r$   r	  zTable._update_vertical_edges  s    =		NQ&Ezuc* 6A15DJJqM%(.6  aZ5#& -(,

1a %- 5#& 6,0

1e$)15

1eai(.6r&   c                ,   |D ]  }| j                  | j                  |d   |      }|&| j                  | j                  |d   |      }|t        | j                        }| j                  | j                  |d   |      }| j	                  |||        y )Nr   r-   r   )r  r   r3   r   _update_horizontal_edges)r"   r  r  hr  r  r  s          r$   r   zTable._set_horizontal_edges  s     
	9A **499adIFE}((AaD)DC{$))n&&tyy!A$	BA))%a8
	9r&   c                v   |Gt        | j                        dz
  }|dk\  r)t        ||      D ]  }d| j                  |   |   _         y y |dk(  r)t        ||      D ]  }d| j                  d   |   _         y t        ||      D ]3  }d| j                  |   |   _        d| j                  |dz
     |   _        5 y r  )r3   r   r  r   r   r   r  s        r$   r  zTable._update_horizontal_edges  s    =		NQ&Ezuc* 7A26DJJu%a(/7  aZ5#& ,'+

1a $, 5#& 7+/

5!!$(26

519%a(/7r&   c                   t        | j                        }t        | j                        |dk(  sdk(  r| S t        | j                        |k7  st	        fd| j                  D              rt        d      t        |      D ]3  }d| j                  |   d   _        d| j                  |   dz
     _        5 t              D ]3  }d| j                  d   |   _	        d| j                  |dz
     |   _
        5 | S )z Sets table border edges to True.r   c              3  :   K   | ]  }t        |      k7    y wr   r3   )r   r   num_colss     r$   r   z#Table.set_border.<locals>.<genexpr>  s      .
%(CH .
s   zQInconsistent cells structure: cells should match the dimensions of rows and cols.Tr   )r3   r   r   r   any
ValueErrorr  r   r   r   r   )r"   num_rows	row_index	col_indexr  s       @r$   
set_borderz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                t    || S |D ].  }|dk(  r| j                          |dk(  s| j                          0 | S )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.
        r  r
  )_copy_horizontal_text_copy_vertical_text)r"   	copy_text	directions      r$   copy_spanning_textzTable.copy_spanning_text  sM      K" 	+IC**,c!((*		+ r&   c                   t        t        | j                              D ]  }t        t        | j                  |               D ]  }| j                  |   |   j                  j	                         dk(  s1| j                  |   |   j
                  sN| j                  |   |   j                  rk| j                  |   |dz
     j                  | j                  |   |   _          y)a  
        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
        r/   r   N)r  r3   r   r   r|   r   r   r"   r  r  s      r$   r"  zTable._copy_horizontal_text   s     s4::' 	FA3tzz!}-. FJJqM!$))//1R7

1a(.. JJqM!,11,0JJqM!a%,@,E,EDJJqM!$)F	Fr&   c                   t        t        | j                              D ]  }t        t        | j                  |               D ]  }| j                  |   |   j                  j	                         dk(  s1| j                  |   |   j
                  sN| j                  |   |   j                  rk| j                  |dz
     |   j                  | j                  |   |   _          y)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
        r/   r   N)r  r3   r   r   r|   r   r   r(  s      r$   r#  zTable._copy_vertical_text  s     s4::' 	FA3tzz!}-. FJJqM!$))//1R7

1a(.. JJqM!,00,0JJq1u,=a,@,E,EDJJqM!$)F	Fr&   c                n    ddddd}|j                  |        | j                  j                  |fi | y)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   )encodingr  headerquotingN)updater   to_csv)r"   r   kwargskws       r$   r0  zTable.to_csv(  s5     "EUqQ
		&t"r"r&   c                    ddi}|j                  |        | j                  j                  di |}t        |d      5 }|j	                  |       ddd       y# 1 sw Y   yxY w)zWrite Table(s) to a JSON file.

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

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

        orientrecordswNr>   )r/  r   to_jsonopenwrite)r"   r   r1  r2  json_stringfs         r$   r7  zTable.to_json7  s]     	"
		&%dggoo++$_ 	!GGK 	! 	! 	!s   AA"c                    ddi}d| j                    d| j                   }|j                  |       t        j                  |      } | j
                  j                  |fd|i| y)zWrite Table(s) to an Excel file.

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

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

        r,  r+  page--table-
sheet_nameN)r   r   r/  r   ExcelWriterr   to_excel)r"   r   r1  r2  r?  writers         r$   rA  zTable.to_excelH  s`     '"TYYKwtzzl;

		&%=J="=r&   c                     | j                   j                  di |}t        |dd      5 }|j                  |       ddd       y# 1 sw Y   yxY w)zWrite Table(s) to an HTML file.

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

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

        r6  r+  r,  Nr>   )r   to_htmlr8  r9  )r"   r   r1  html_stringr;  s        r$   rE  zTable.to_htmlY  sJ     &dggoo//$g. 	!!GGK 	! 	! 	!   AAc                     | j                   j                  di |}t        |dd      5 }|j                  |       ddd       y# 1 sw Y   yxY w)zWrite Table(s) to a Markdown file.

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

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

        r6  r+  rD  Nr>   )r   to_markdownr8  r9  )r"   r   r1  	md_stringr;  s        r$   rI  zTable.to_markdownh  sL     (DGG''1&1	$g. 	!GGI	 	 	rG  c                   ddd}|j                  |       t        j                  |      }d| j                   d| j                   } | j
                  j                  ||fi | |j                          |j                          y)zWrite Table(s) to sqlite database.

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

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

        r2   F)	if_existsr  r=  r>  N)	r/  sqlite3connectr   r   r   to_sqlcommitclose)r"   r   r1  r2  conn
table_names         r$   	to_sqlitezTable.to_sqlitew  sm     %u5
		&t$TYYKwtzzl;
z4.2.

r&   )r-   r   )r:   r;   r<   r=   r%   r5   r   r   r   r   r   r   r  r  r   r	  r   r  r   r&  r"  r#  r0  r7  rA  rE  rI  rT  r>   r&   r$   r   r     s    > (A&    
(7697:6F(F(#!">"!r&   r   c                  :    e Zd ZU dZded<   ded<   ded<   ded<   y)	_Kwz.Helper class to define file related arguments.zos.PathLike[Any] | strr   strdirnamerootextN)r:   r;   r<   r=   __annotations__r>   r&   r$   rV  rV    s    8
  L
I	Hr&   rV  c                  x    e Zd ZdZddZd Zd Zd ZddZddZ	e
d        Zedd	       ZdddZddZdddZy
)	TableListzDefines 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                    || _         y r   _tables)r"   tabless     r$   r%   zTableList.__init__  s	    (.r&   c                P    d| j                   j                   d| j                   dS )Nr   z n=rI   )rF   r:   nr   s    r$   r5   zTableList.__repr__  s&    4>>**+3tvvha88r&   c                ,    t        | j                        S r   )r3   r`  r   s    r$   __len__zTableList.__len__  s    4<<  r&   c                     | j                   |   S r   r_  )r"   idxs     r$   __getitem__zTableList.__getitem__  s    ||C  r&   c                ,    t        | j                        S r   )iterr`  r   s    r$   __iter__zTableList.__iter__  s    DLL!!r&   c                    t        |       S r   )nextr   s    r$   __next__zTableList.__next__  s    Dzr&   c                     t        | d|       S )Nto_)getattr)tabler;  s     r$   _format_funczTableList._format_func  s    uA3i((r&   c                    t        |       S )z!The number of tables in the list.r  r   s    r$   rc  zTableList.n  s     4yr&   Nc                    |d   }|d   }|d   }| j                   D ]\  }| d|j                   d|j                   | }t        j                  j                  ||      }| j                  ||      }	 |	|       ^ y )NrX  rY  rZ  -page-r>  )r`  r   r   r   r   r0   rs  )
r"   r;  r1  rX  rY  rZ  rr  r   filepath	to_formats
             r$   _write_filezTableList._write_file  s    #f~Um\\ 	 Evejj\cUKHww||GX6H))%3Ih		 r&   c                   |d   }|d   }|d   }|d   }t         j                  j                  t         j                  j                  |      |      dz   }t	        j
                  |dd      5 }| j                  D ]q  }| d	|j                   d
|j                   | }	t         j                  j                  ||	      }
|j                  |
t         j                  j                  |
             s 	 d d d        y # 1 sw Y   y xY w)Nr   rX  rY  rZ  .zipr6  T
allowZip64rv  r>  )r   r   r0   rX  zipfileZipFiler`  r   r   r9  r   )r"   r1  r   rX  rY  rZ  zipnamezrr  r   rw  s              r$   _compress_dirzTableList._compress_dir  s    f~#f~Um'',,rwwt4d;fD__Wcd; 	>q >"V6%**WU[[M#O77<<:"''"2"28"<=>	> 	> 	>s   -BC88Dc                   t         j                  j                  |      }t         j                  j                  |      }t         j                  j	                  |      \  }}|rt        j                         }||||d}|dv r* | j                  dd|i| |r | j                  di | yy|dk(  r#t         j                  j                  ||      }	t        j                  |	      }
| j                  D ];  }d|j                   d|j                   }|j                  j!                  |
|       = |
j#                          |rt         j                  j                  t         j                  j                  |      |      dz   }t%        j&                  |d	d
      5 }|j)                  |	t         j                  j                  |	             ddd       yy|dk(  rt         j                  j                  ||      }	| j                  D ]  }|j+                  |	        |rt         j                  j                  t         j                  j                  |      |      dz   }t%        j&                  |d	d
      5 }|j)                  |	t         j                  j                  |	             ddd       yyy# 1 sw Y   yxY w# 1 sw Y   yxY w)aB  Export 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   rX  rY  rZ  )csvhtmljsonmarkdownr;  excelr=  r>  )r?  r{  r6  Tr|  Nsqliter>   )r   r   rX  r   splitexttempfilemkdtempry  r  r0   r   r@  r`  r   r   r   rA  rQ  r~  r  r9  rT  )r"   r   r;  compressrX  r   rY  rZ  r1  rw  rB  rr  r?  r  r  s                  r$   exportzTableList.export  sP    ''//$'77##D)GG$$X.	c&&(G#cR33D+q+F+""",V, '\ww||GX6H^^H-F A$UZZL}E
!!&Z!@A LLN'',,rwwt'<dCfL__WcdC BqGGHbgg&6&6x&@AB B  (]ww||GX6H *)*'',,rwwt'<dCfL__WcdC BqGGHbgg&6&6x&@AB B 	 B BB Bs   0J160J=1J:=K)ra  zIterable[Table]r   None)r   zIterator[Table])r   r   )r   intr   )r1  zUnpack[_Kw]r   r  )r  F)r   rW  )r:   r;   r<   r=   r%   r5   re  rh  rk  rn  rq   rs  r   rc  ry  r  r  r>   r&   r$   r]  r]    s]    	/9!!" ) )   
>+Br&   r]  ))r=   
__future__r   rN   r   rM  sysr  r~  operatorr   typingr   r   r   r   pandasr   version_infor
   r   typing_extensionsbackendsr   utilsr   r   r   rP   r   ru   VERTICAL_ALIGNMENTSALL_ALIGNMENTSr   r@   rX   rs   r   r   rV  r]  r>   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&   