B
    ¢ßJdA  ã               @   s˜   d dl Z d dlZd dlZd dlmZ d dlmZ yd dlmZ W n  e	k
r`   d dl
mZ Y nX eeƒZdd„ Zdd„ Zd	d
„ Zddd„Zdd„ ZdS )é    N)ÚPath)Útqdm)Úversionc       	      C   sü   dd l }|j d¡}d}| d rŒxÊttt| d ƒ d¡ƒƒD ]J}t| d ƒt|jƒd  }|j	 
t|ƒ¡}|jt|ƒ|d	 | d¡ q<W n`td
d}t| d ƒ ¡ }t| d ƒ ¡ }|j	 
t|ƒ¡}|jt|ƒ|d	 | d¡ | d
¡ |sø| ¡  d S )Nr   zWord.Applicationé   ÚbatchÚinputz*.docxÚoutputz.pdf)Z
FileFormaté   )Útotal)Zwin32com.clientÚclientZDispatchr   Úsortedr   ÚglobÚstrÚstemZ	DocumentsZOpenZSaveAsÚCloseÚresolveÚupdateZQuit)	ÚpathsÚkeep_activeZwin32comÚwordZwdFormatPDFZdocx_filepathZpdf_filepathÚdocÚpbar© r   úC/var/www/html/venv/lib/python3.7/site-packages/docx2pdf/__init__.pyÚwindows   s$     


r   c       	   	   C   sê   t tƒjd  ¡ }dddt|ƒt| d ƒt| d ƒt|ƒ ¡ g}dd„ }| d	 rjttt | d ƒ d
¡ƒƒnd}t	|d}xl||ƒD ]`}yt
 |¡}W n tk
rª   w‚Y nX |d dkrÄ| d¡ q‚|d dkr‚t|ƒ t d¡ q‚W d S )Nzconvert.jxaz/usr/bin/osascriptz-lÚ
JavaScriptr   r   c             s   s:   t j| t jd}x$|j ¡  ¡ }|s&P | d¡V  qW d S )N)Ústderrzutf-8)Ú
subprocessÚPopenÚPIPEr   ÚreadlineÚrstripÚdecode)ÚcmdÚprocessÚliner   r   r   Úrun5   s    zmacos.<locals>.runr   z*.docxr	   )r
   ÚresultÚsuccessÚerror)r   Ú__file__Úparentr   r   ÚlowerÚlenÚlistr   r   ÚjsonÚloadsÚ
ValueErrorr   ÚprintÚsysÚexit)	r   r   Úscriptr#   r&   r
   r   r%   Úmsgr   r   r   Úmacos)   s*    

&
r7   c             C   sê   t | ƒ ¡ } |rt |ƒ ¡ nd }i }|  ¡ rdd|d< t| ƒ|d< |rR| ¡ sZt‚nt| ƒ}||d< n‚d|d< t| ƒ d¡s~t‚t| ƒ|d< |r®| ¡ r®t|t| jƒd  ƒ}n0|rÆt|ƒ d¡sÞt‚nt| jt| jƒd  ƒ}||d< |S )NTr   r   r   Fz.docxz.pdf)r   r   Úis_dirr   ÚAssertionErrorÚendswithr   r+   )Ú
input_pathÚoutput_pathr   r   r   r   Úresolve_pathsK   s(    
r=   Fc             C   s>   t | |ƒ}tjdkrt||ƒS tjdkr2t||ƒS tdƒ‚d S )NÚdarwinÚwin32zSdocx2pdf is not implemented for linux as it requires Microsoft Word to be installed)r=   r3   Úplatformr7   r   ÚNotImplementedError)r;   r<   r   r   r   r   r   Úconverte   s    




rB   c                 sÒ   dd l } dd l‰ dtjkr,ttƒ t d¡ |  d¡}‡ fdd„}ˆ j||d}|j	ddd	 |j	d
ddd |j	ddddd |j	ddddd t
tjƒdkr´| ¡  t d¡ n| ¡ }t|j|j|jƒ d S )Nr   z	--versiona:  
    Example Usage:

    Convert single docx file in-place from myfile.docx to myfile.pdf:
        docx2pdf myfile.docx

    Batch convert docx folder in-place. Output PDFs will go in the same folder:
        docx2pdf myfolder/

    Convert single docx file with explicit output filepath:
        docx2pdf input.docx output.docx

    Convert single docx file and output to a different explicit folder:
        docx2pdf input.docx output_dir/

    Batch convert docx folder. Output PDFs will go to a different explicit folder:
        docx2pdf input_dir/ output_dir/
    c                s   ˆ j | ddS )Né    )Úmax_help_position)ÚRawDescriptionHelpFormatter)Úprog)Úargparser   r   Ú<lambda>   s   zcli.<locals>.<lambda>)ÚdescriptionÚformatter_classr   zIinput file or folder. batch converts entire folder or convert single file)Úhelpr   ú?zoutput file or folder)ÚnargsrK   z--keep-activeÚ
store_trueFz%prevent closing word after conversion)ÚactionÚdefaultrK   zdisplay version and exitr	   )ÚtextwraprG   r3   Úargvr2   Ú__version__r4   ÚdedentÚArgumentParserÚadd_argumentr-   Ú
print_helpÚ
parse_argsrB   r   r   r   )rQ   rI   rJ   ÚparserÚargsr   )rG   r   Úcliq   s4    


r[   )NF)r3   r/   r   Úpathlibr   Z	tqdm.autor   Úimportlib.metadatar   ÚImportErrorÚimportlib_metadataÚ__package__rS   r   r7   r=   rB   r[   r   r   r   r   Ú<module>   s   "
