o
    e                     @   st   d Z ddlZddlZddlmZ dd Zdd Zdd	 Zd
d Zdd Zdd Z	e
dkr8e	 Zeejej dS dS )z
conver table label to html
    N)tqdmc                 C   sF   t |ddd}|d| | W d    d S 1 sw   Y  d S )Nza+utf-8)encodingz{}	{}
)openwriteformat)keyvalZtmp_file_pathf r   ^D:\Projects\ConvertPro\env\Lib\site-packages\paddleocr/ppstructure/table/convert_label2html.pysave_pred_txt   s   "r   c                 C   s   |D ]}|  |d} q| S )zy
    skip empty cell
    @param text: text in cell
    @param sp_char_list: style char and special code
    @return:
     )replace)textsp_char_listZsp_charr   r   r   	skip_char   s   r   c                 C   s   | d d d   }dd t|D }t|ddd | d d ddd D ]'\}}|d rMd	|d }g d
}t||}t|dkrEq&||d | q&d	|}d|}|S )z= 
    Formats HTML code from tokenized annotation of img
    htmlZ	structuretokensc                 S   s   g | ]
\}}|d v r|qS ))z<td>>r   ).0itagr   r   r   
<listcomp>-   s    zgen_html.<locals>.<listcomp>Ncellsr   )z<b>z</b>u     z<i>z</i>r      z+<html><body><table>{}</table></body></html>)copy	enumeratezipjoinr   leninsertr   )ZimgZ	html_codeZ	to_insertr   cellr   r   Ztext_remove_styler   r   r   gen_html(   s   .


r%   c                 C   sp   i }t | d'}| }t|D ]}|dd}t|}|||d < qW d   |S 1 s1w   Y  |S )z2
    load gt
    @param gt_path:
    @return:
    rbr   
filenameN)r   	readlinesr   decodestripjsonloads)Zgt_pathZ	data_listr
   lineslineZ	data_lineinfor   r   r   load_gt_data<   s   

r1   c                 C   s>   t | }t| D ]\}}t|}t||| q
td dS )z`
    gen html from label file
    @param origin_gt_path:
    @param save_path:
    @return:
    zconver finishN)r1   r   itemsr%   r   print)Zorigin_gt_path	save_pathZ	data_dictZimg_namegtr   r   r   r   convertL   s
   r6   c                  C   s<   t jdd} | jdtddd | jdtddd |  }|S )	Nzargs for paddleserving)descriptionz--ori_gt_pathTzlabel gt path)typerequiredhelpz--save_pathzpath to save file)argparseArgumentParseradd_argumentstr
parse_args)parserargsr   r   r   r?   Z   s   r?   __main__)__doc__r,   r;   r   r   r   r%   r1   r6   r?   __name__rA   Zori_gt_pathr4   r   r   r   r   <module>   s   
