<commit_before>"""
Read and write xlsx files, using openpyxl.

"""

import os

import petl

from petlx.util import UnsatisfiedDependency


dep_message = """
The package openpyxl is required. Instructions for installation can be found at 
https://bitbucket.org/ericgazoni/openpyxl/wiki/Home or try pip install openpyxl.
"""


def fromxlsx(filename, sheet=None):
    """
    Extract a table from a sheet in an Excel (.xlsx) file.
    
    N.B., the sheet name is case sensitive, so watch out for, e.g., 'Sheet1'.

    The package openpyxl is required. Instructions for installation can be found at 
    https://bitbucket.org/ericgazoni/openpyxl/wiki/Home or try ``pip install openpyxl``.

    .. versionchanged:: 0.15

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

    """
    
    return XLSXView(filename, sheet)


class XLSXView(petl.util.RowContainer):
    
    def __init__(self, filename, sheet=None):
        self.filename = filename
        self.sheet = sheet

    def __iter__(self):
        try:
            import openpyxl
        except ImportError as e:
            raise UnsatisfiedDependency(e, dep_message)

        wb = openpyxl.reader.excel.load_workbook(filename=self.filename, use_iterators=True)
        if self.sheet is None:
            ws = wb.get_sheet_by_name(wb.get_sheet_names()[0])
        elif isinstance(self.sheet, int):
            ws = wb.get_sheet_by_name(wb.get_sheet_names()[self.sheet])
        else:
            ws = wb.get_sheet_by_name(str(self.sheet))
        return (tuple(cell.internal_value for cell in row) for row in ws.iter_rows())<commit_msg>closes #47 by passing through remaining kwargs<commit_after>"""
Read and write xlsx files, using openpyxl.

"""

import os

import petl

from petlx.util import UnsatisfiedDependency


dep_message = """
The package openpyxl is required. Instructions for installation can be found at 
https://bitbucket.org/ericgazoni/openpyxl/wiki/Home or try pip install openpyxl.
"""


def fromxlsx(filename, sheet=None, **kwargs):
    """
    Extract a table from a sheet in an Excel (.xlsx) file.
    
    N.B., the sheet name is case sensitive, so watch out for, e.g., 'Sheet1'.

    The package openpyxl is required. Instructions for installation can be found at 
    https://bitbucket.org/ericgazoni/openpyxl/wiki/Home or try ``pip install openpyxl``.

    .. versionchanged:: 0.15

    The ``sheet`` argument can be omitted, in which case the first sheet in the workbook is used by default. Any other
    keyword arguments are passed through to :func:`openpyxl.load_workbook()`.

    """
    
    return XLSXView(filename, sheet, **kwargs)


class XLSXView(petl.util.RowContainer):
    
    def __init__(self, filename, sheet=None, **kwargs):
        self.filename = filename
        self.sheet = sheet
        self.kwargs = kwargs

    def __iter__(self):
        try:
            import openpyxl
        except ImportError as e:
            raise UnsatisfiedDependency(e, dep_message)

        wb = openpyxl.load_workbook(filename=self.filename, use_iterators=True, **self.kwargs)
        if self.sheet is None:
            ws = wb.get_sheet_by_name(wb.get_sheet_names()[0])
        elif isinstance(self.sheet, int):
            ws = wb.get_sheet_by_name(wb.get_sheet_names()[self.sheet])
        else:
            ws = wb.get_sheet_by_name(str(self.sheet))
        return (tuple(cell.internal_value for cell in row) for row in ws.iter_rows())
{'fraction_non_alphanumeric': 0.09690366972477064, 'fraction_numerical': 0.0034403669724770644, 'mean_word_length': 2.942372881355932, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7441304', 'n_tokens_mistral': 1113, 'n_tokens_neox': 1061, 'n_words': 326}