o
    ‹¨Êh  ã                   @   sÄ   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZ dZdZd	Zdd
d„Zddd„Zefdd„Zefdd„Zefdd„Zefdd„Zefdd„Zefdd„Zddd„Zddd„ZdS )z¹
    pyexcel.cookbook
    ~~~~~~~~~~~~~~~~~~~

    Cookbook for pyexcel

    :copyright: (c) 2014-2017 by Onni Software Ltd.
    :license: New BSD License, see LICENSE for more details
é    N)ÚBook)Úsave_asÚget_bookÚ	get_sheet)ÚOrderedDict)ÚMESSAGE_WARNINGzpyexcel_merged.csvz
merged.xlsz
pyexcel_%sc                 C   óp   t |  }|r|}tj |¡rttƒ‚t| dd�}|j}| ¡ D ]}| 	t
|ƒ¡}| ||| ¡ q| |¡ dS )aZ  Update one or more columns of a data file with series

    The data structure of column_dicts should be:
    key should be first row of the column
    the rest of the value should an array
    :param str infilename: an accessible file name
    :param dict column_dicts: dictionaries of columns
    :param str outfilename: save the sheet as


    r   )Ú	file_nameÚname_columns_by_rowN)ÚOUT_FILE_FORMATTERÚosÚpathÚexistsÚNotImplementedErrorr   r   ÚcolnamesÚkeysÚindexÚstrÚset_column_atr   )Ú
infilenameÚcolumn_dictsÚoutfilenameÚdefault_out_fileÚsheetÚseriesÚkr   © r   úB/var/www/html/env/lib/python3.10/site-packages/pyexcel/cookbook.pyÚupdate_columns   s   r   c                 C   r   )a3  Update one or more rows of a data file with series

    datastucture: key should an integer of the row to be updated
    value should be an array of the data
    :param str infilename: an accessible file name
    :param dict row_dicts: dictionaries of rows
    :param str outfilename: save the sheet as
    r   )r	   Úname_rows_by_columnN)r   r   r   r   r   r   r   Úrownamesr   r   r   Ú
set_row_atr   )r   Ú	row_dictsr   r   r   r   r   r   r   r   r   Úupdate_rows/   s   	r#   c                 C   s^   t j |¡r
ttƒ‚g }| D ]}t|d�}| t| ¡ ƒ¡ qt|d�}| 	¡  | 
|¡ |S )zdmerge many files horizontally column after column
    :param str outfilename: save the sheet as
    ©r	   )Úarray)r   r   r   r   r   r   ÚextendÚlistÚcolumnsÚ	transposer   )Ú
file_arrayr   Úcontentr	   r   Úmerged_sheetr   r   r   Úmerge_filesE   s   


r-   c                 C   ó*   t j |¡r
ttƒ‚| |g}t||ƒ dS )zŸmerge two files

    :param str file1: an accessible file name
    :param str file2: an accessible file name
    :param str outfilename: save the sheet as
    N)r   r   r   r   r   r-   )Úfile1Úfile2r   Úfilesr   r   r   Úmerge_two_filesU   s   r2   c                 C   s@   t j |¡r
ttƒ‚tƒ }| D ]}| |j¡ qt||d� dS )zŽmerge many readers

    With FilterableReader and SeriesReader, you can do custom filtering
    :param str outfilename: save the sheet as
    )Údest_file_nameÚadictN)	r   r   r   r   r   r   ÚupdateÚdictr   )Úreader_arrayr   r+   Úreaderr   r   r   Úmerge_readersb   s   r9   c                 C   r.   )zFmerge two readers

    :param str outfilename: save the sheet as

    N)r   r   r   r   r   r9   )Úreader1Úreader2r   r7   r   r   r   Úmerge_two_readersp   s   r<   c                 C   sF   t ƒ }| D ]}t|d�}tj |¡\}}||_||7 }q| |¡ dS )z˜merge a list of csv files into a excel book

    :param list filelist: a list of accessible file path
    :param str outfilename: save the sheet as
    r$   N)r   r   r   r   ÚsplitÚnamer   )Úfilelistr   Úmergedr	   r   Ú_Útailr   r   r   Úmerge_csv_to_a_book|   s   

rC   c                 C   s,   t ƒ }| D ]	}|t|d�7 }q| |¡ dS )zšmerge a list of excel files into a excel book

    :param list filelist: a list of accessible file path
    :param str outfilename: save the sheet as
    r$   N)r   r   r   )r?   r   r@   r	   r   r   r   Úmerge_all_to_a_book‹   s   rD   c                 C   s>   t | d�}|r
|}n| }|D ]}d|j|f }| |¡ qdS )z–Split a file into separate sheets

    :param str file_name: an accessible file name
    :param str outfilename: save the sheets with file suffix
    r$   ú%s_%sN)r   r>   r   )r	   r   ÚbookÚsaveasr   Úfilenamer   r   r   Úsplit_a_book—   s   
þrI   c                 C   s:   t | d�}|r
|}n| }|| }d||f } | | ¡ dS )z´Extract a sheet from a excel book

    :param str file_name: an accessible file name
    :param str sheetname: a valid sheet name
    :param str outfilename: save the sheet as
    r$   rE   N)r   r   )r	   Ú	sheetnamer   rF   rG   r   r   r   r   Úextract_a_sheet_from_a_book§   s   
rK   )N)Ú__doc__r   Úpyexcel.bookr   Úpyexcel.corer   r   r   Úpyexcel._compactr   Úpyexcel.constantsr   ÚDEFAULT_OUT_FILEÚDEFAULT_OUT_XLS_FILEr   r   r#   r-   r2   r9   r<   rC   rD   rI   rK   r   r   r   r   Ú<module>   s&    	


