a
    °©ùd  ã                   @   s®   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
T dd	lmZ dd
lmZ ddl
mZmZmZmZmZ G dd„ de	ƒZedkrªeddƒZee ¡ ƒ dS )é    N)ÚPath)Ú	PdfReader)Ú	Converteré   )ÚBaseé   )Ú*)ÚWrongConverterError)ÚFontDetector)Úrun_shell_commandÚextract_text_dataÚcreate_txt_fileÚextract_figure_from_imgÚconvert_pdf_to_docxc                       sN   e Zd Zddœ‡ fdd„Zdd„ Zdd„ Zd	d
„ Zeedœ‡ fdd„Z‡  Z	S )ÚPdfConverterN)Úreturnc                    s   t ƒ  ¡  d S )N)ÚsuperÚ__init__)Úself©Ú	__class__© úq/home/svplap0089/Collabll/API Projects/ConvertPro/convertor/docconvertor/fileconverter/converter/pdf_converter.pyr      s    zPdfConverter.__init__c                 C   s2   t | jƒ t| jƒ}|jd  ¡ dkr*dS dS d S )Nr   Ú FT)ÚprintÚinput_file_pathr   ZpagesZextract_text)r   Z
pdf_readerr   r   r   Ú_check_if_scanned_pdf   s
    

z"PdfConverter._check_if_scanned_pdfc                 C   sš   | j }|dkr(t| jƒ}t|| j| jƒS |dv r@t| j| j|ƒS || jv r–| dd¡}d|› d| j› dt| jƒj	› d�}t
d	|› �ƒ t|| j| j ƒS d S )
Nz.txt)z.docxz.docÚ.r   z?soffice --infilter="writer_pdf_import" --headless --convert-to z "z" --outdir "ú"zShell Command: )Úoutput_file_formatr   r   r   Zinput_file_namer   Z_supported_formatsÚreplacer   Úparentr   r   )r   ÚformatZ	text_dataZshell_commandr   r   r   Ú_convert    s    

ÿ
"
ÿzPdfConverter._convertc           
      C   sœ   t ƒ }tjdd��R}t| j|ƒ}t|ƒ\}}t||ƒ}t|ƒ}t|||ƒ}t	|| jƒ}W d   ƒ n1 sj0    Y  || _|  
¡ \}}	td|› �ƒ ||	fS )Nr   )ÚdirzOCR File Location: )r
   ÚtempfileÚTemporaryDirectoryZextract_images_from_pdfr   r   Zcreate_hocr_fileZcreate_background_imagesZcombine_text_imageZmerge_pdf_filesr#   r   )
r   Zfont_detectorZtmp_dirZimg_locationsZfigure_locationZhocr_file_locationsZbackground_img_locationsZpdf_file_locationsZfile_locationÚstatusr   r   r   Ú_run_ocr1   s    
ÿ*zPdfConverter._run_ocr)r   r   c                    sP   t ƒ  ||¡ | jdkr$td| jƒ‚| j| jkr4dS |  ¡ rD|  ¡ S |  ¡ S d S )Nú.pdfr   )ZsameF)r   Zinitialize_detailsZinput_file_formatr	   r   r   r(   r#   )r   r   r   r   r   r   ÚconvertA   s    
zPdfConverter.convert)
Ú__name__Ú
__module__Ú__qualname__r   r   r#   r(   Ústrr*   Ú__classcell__r   r   r   r   r      s
   r   Ú__main__zFinal Report.docxr)   )r%   ÚloggingÚpathlibr   ZPyPDF2r   Zpdf2docxr   Úbaser   Zutils.document_converterZutils.errorsr	   Zutils.font_detectionr
   r   r   r   r   r   r   r+   Úpr   r*   r   r   r   r   Ú<module>   s   8
