Exemplo n.º 1
0
def load_data(file_name=None,
              file_content=None,
              file_stream=None,
              file_type=None,
              force_file_type=None,
              sheet_name=None,
              sheet_index=None,
              sheets=None,
              library=None,
              streaming=False,
              **keywords):
    """Load data from any supported excel formats

    :param filename: actual file name, a file stream or actual content
    :param file_type: used only when filename is not a physial file name
    :param force_file_type: used only when filename refers to a physical file
                            and it is intended to open it as forced file type.
    :param sheet_name: the name of the sheet to be loaded
    :param sheet_index: the index of the sheet to be loaded
    :param keywords: any other parameters
    """
    result = {}
    inputs = [file_name, file_content, file_stream]
    number_of_none_inputs = [x for x in inputs if x is not None]
    if len(number_of_none_inputs) != 1:
        raise IOError(constants.MESSAGE_ERROR_02)

    if file_type is None:
        if force_file_type:
            file_type = force_file_type
        else:
            try:
                file_type = file_name.split(".")[-1]
            except AttributeError:
                raise Exception("file_name should be a string type")

    reader = READERS.get_a_plugin(file_type, library)
    if file_name:
        reader.open(file_name, **keywords)
    elif file_content:
        reader.open_content(file_content, **keywords)
    elif file_stream:
        reader.open_stream(file_stream, **keywords)
    if sheet_name:
        result = reader.read_sheet_by_name(sheet_name)
    elif sheet_index is not None:
        result = reader.read_sheet_by_index(sheet_index)
    elif sheets is not None:
        result = reader.read_many(sheets)
    else:
        result = reader.read_all()
    if streaming is False:
        for key in result.keys():
            result[key] = list(result[key])
        reader.close()
        reader = None

    return result, reader
Exemplo n.º 2
0
def load_data(file_name=None,
              file_content=None,
              file_stream=None,
              file_type=None,
              sheet_name=None,
              sheet_index=None,
              sheets=None,
              library=None,
              streaming=False,
              **keywords):
    """Load data from any supported excel formats

    :param filename: actual file name, a file stream or actual content
    :param file_type: used only when filename is not a physial file name
    :param sheet_name: the name of the sheet to be loaded
    :param sheet_index: the index of the sheet to be loaded
    :param keywords: any other parameters
    """
    result = {}
    inputs = [file_name, file_content, file_stream]
    number_of_none_inputs = [x for x in inputs if x is not None]
    if len(number_of_none_inputs) != 1:
        raise IOError(constants.MESSAGE_ERROR_02)
    if file_type is None:
        try:
            file_type = file_name.split(".")[-1]
        except AttributeError:
            raise Exception("file_name should be a string type")

    reader = READERS.get_a_plugin(file_type, library)
    if file_name:
        reader.open(file_name, **keywords)
    elif file_content:
        reader.open_content(file_content, **keywords)
    elif file_stream:
        reader.open_stream(file_stream, **keywords)
    if sheet_name:
        result = reader.read_sheet_by_name(sheet_name)
    elif sheet_index is not None:
        result = reader.read_sheet_by_index(sheet_index)
    elif sheets is not None:
        result = reader.read_many(sheets)
    else:
        result = reader.read_all()
    if streaming is False:
        for key in result.keys():
            result[key] = list(result[key])
        reader.close()
        reader = None

    return result, reader
Exemplo n.º 3
0
"""
    pyexcel.plugins.parsers
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    A list of built-in parsers

    :copyright: (c) 2015-2020 by Onni Software Ltd.
    :license: New BSD License
"""
from pyexcel.plugins import PyexcelPluginChain

from pyexcel_io.plugins import READERS
from pyexcel_io.constants import DB_SQL, DB_DJANGO

PyexcelPluginChain(__name__).add_a_parser(
    relative_plugin_class_path="excel.ExcelParser",
    file_types=READERS.get_all_formats(),
).add_a_parser(
    relative_plugin_class_path="sqlalchemy.SQLAlchemyExporter",
    file_types=[DB_SQL],
).add_a_parser(relative_plugin_class_path="django.DjangoExporter",
               file_types=[DB_DJANGO])
Exemplo n.º 4
0
"""
    pyexcel.plugins.parsers
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    A list of built-in parsers

    :copyright: (c) 2015-2017 by Onni Software Ltd.
    :license: New BSD License
"""
from pyexcel_io.plugins import READERS
from pyexcel_io.constants import DB_SQL, DB_DJANGO

from pyexcel.plugins import PyexcelPluginChain


PyexcelPluginChain(__name__).add_a_parser(
    relative_plugin_class_path='excel.ExcelParser',
    file_types=READERS.get_all_formats()
).add_a_parser(
    relative_plugin_class_path='sqlalchemy.SQLAlchemyExporter',
    file_types=[DB_SQL]
).add_a_parser(
    relative_plugin_class_path='django.DjangoExporter',
    file_types=[DB_DJANGO]
)
Exemplo n.º 5
0
"""
    pyexcel.plugins.parsers
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    A list of built-in parsers

    :copyright: (c) 2015-2017 by Onni Software Ltd.
    :license: New BSD License
"""
from pyexcel_io.plugins import READERS
from pyexcel_io.constants import DB_SQL, DB_DJANGO

from pyexcel.plugins import PyexcelPluginChain

PyexcelPluginChain(__name__).add_a_parser(
    relative_plugin_class_path='excel.ExcelParser',
    file_types=READERS.get_all_formats()).add_a_parser(
        relative_plugin_class_path='sqlalchemy.SQLAlchemyExporter',
        file_types=[DB_SQL]).add_a_parser(
            relative_plugin_class_path='django.DjangoExporter',
            file_types=[DB_DJANGO])