o
    ;Di                     @   s   d dl mZmZmZ d dlZd dlmZmZ d dlm	Z	m
Z
 d dlmZmZ 		dddZG d	d
 d
e	ZdddZdd Zdd Zee	_dddZee	_dS )    )absolute_importprint_functiondivisionN)PY3	text_type)Tabledata)read_source_from_argwrite_source_from_argFc           	   
   K   s    t | f|||||||d|S )a~  
    Extract a table from a sheet in an Excel .xlsx file.

    N.B., the sheet name is case sensitive.

    The `sheet` argument can be omitted, in which case the first sheet in
    the workbook is used by default.

    The `range_string` argument can be used to provide a range string
    specifying a range of cells to extract.

    The `min_row`, `min_col`, `max_row` and `max_col` arguments can be
    used to limit the range of cells to extract. They will be ignored
    if `range_string` is provided.

    The `read_only` argument determines how openpyxl returns the loaded 
    workbook. Default is `False` as it prevents some LibreOffice files
    from getting truncated at 65536 rows. `True` should be faster if the
    file use is read-only and the files are made with Microsoft Excel.

    Any other keyword arguments are passed through to
    :func:`openpyxl.load_workbook()`.

    )sheetrange_stringmin_rowmin_colmax_rowmax_col	read_only)XLSXView	filenamer   r   r   r   r   r   r   kwargs r   E/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/io/xlsx.pyfromxlsx   s   
r   c                   @   s$   e Zd Z			dddZdd ZdS )r   NFc	           
      K   s:   || _ || _|| _|| _|| _|| _|| _|| _|	| _d S Nr   )
selfr   r   r   r   r   r   r   r   r   r   r   r   __init__.   s   
zXLSXView.__init__c              	   c   s$   dd l }t| j}|dy}|jd|| jd| j}| jd u r*||jd  }nt	| jt
r9||j| j  }n|t| j }| jd urK|| j }n|j| j| j| j| jd}|D ]}tdd |D V  qZz|j  W n	 tyw   Y n	w W d    d S W d    d S 1 sw   Y  d S )Nr   rbr   r   )r   r   r   r   c                 s   s    | ]}|j V  qd S r   )value).0cellr   r   r   	<genexpr>P   s    z$XLSXView.__iter__.<locals>.<genexpr>r   )openpyxlr	   r   openload_workbookr   r   r   
sheetnames
isinstanceintstrr   	iter_rowsr   r   r   r   tuple_archivecloseAttributeError)r   r"   sourcesource2wbwsrowsrowr   r   r   __iter__;   s@   


"zXLSXView.__iter__NNNNNNF)__name__
__module____qualname__r   r4   r   r   r   r   r   ,   s    
r   Treplacec                 C   s   t |||}t|||}|r2t| }zt|}ttt|}	t|	g|}
W n t	y1   |}
Y nw t
| }
|
D ]}|| q8t|}|d}|| W d   dS 1 sZw   Y  dS )a-  
    Write a table to a new Excel .xlsx file.

    N.B., the sheet name is case sensitive.

    The `mode` argument controls how the file and sheet are treated:

      - `replace`: This is the default. It either replaces or adds a
        named sheet, or if no sheet name is provided, all sheets
        (overwrites the entire file).

      - `overwrite`: Always overwrites the file. This produces a file
        with a single sheet.

      - `add`: Adds a new sheet. Raises `ValueError` if a named sheet
        already exists.

    The `sheet` argument can be omitted in all cases. The new sheet
    will then get a default name.
    If the file does not exist, it will be created, unless `replace`
    mode is used with a named sheet. In the latter case, the file
    must exist and be a valid .xlsx file.
    r0   N)_load_or_create_workbook_insert_sheet_on_workbookiternextlistmapr   	itertoolschainStopIterationr   appendr
   r#   save)tblr   r   write_headermoder0   r1   ithdrfldsr2   r3   targettarget2r   r   r   toxlsxX   s$   "rM   c                 C   s   t rt}nt}dd l}d }|dksG|dkr|d u sGz"t| }|d}|j|dd}W d    n1 s5w   Y  W n |yF   d }Y nw |d u rQ|jdd}|S )	Nr   	overwriter9   r   Fr   T)
write_only)r   FileNotFoundErrorIOErrorr"   r	   r#   r$   Workbook)r   rG   r   FileNotFoundr"   r0   r.   r/   r   r   r   r:      s$   r:   c                 C   s   | dkr%z|t | }|d|j W |S  ty$   |j|d}Y |S w | dkr@|j|d}|d ur>|j|kr>td| |S | dkrL|j|d}|S td|  )Nr9      )titleaddzSheet %s already exists in filerN   zUnknown mode '%s')r(   delete_rowsr   KeyErrorcreate_sheetrU   
ValueError)rG   r   r0   r1   r   r   r   r;      s$   
r;   c              	   C   s@  ddl }t|}|d}|j|dd}|du r!||jd  }nt|tr.||j|  }n|t| }|rZt| }	zt	|	}
t
tt|
}t|g|	}W n tyY   |	}Y nw t| }|D ]}|| q`t|}|d}|| W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )z7
    Appends rows to an existing Excel .xlsx file.
    r   Nr   Fr   r0   )r"   r	   r#   r$   r%   r&   r'   r(   r<   r=   r>   r?   r   r@   rA   rB   r   rC   r
   rD   )rE   r   r   rF   r"   r.   r/   r0   r1   rH   rI   rJ   r2   r3   rK   rL   r   r   r   
appendxlsx   s8   
"r[   r5   )NTr9   )NF)
__future__r   r   r   r@   petl.compatr   r   petl.util.baser   r   petl.io.sourcesr	   r
   r   r   rM   r:   r;   r[   r   r   r   r   <module>   s   
!
,+

 