Exemplo n.º 1
0
def load(file_name, sheetname=None, **keywords):
    """Constructs an instance :class:`Sheet` from a sheet of an excel file

    except csv, most excel files has more than one sheet.
    Hence sheetname is required here to indicate from which sheet the instance
    should be constructed. If this parameter is omitted, the first sheet, which
    is indexed at 0, is used. For csv, sheetname is always omitted because csv
    file contains always one sheet.
    :param str sheetname: which sheet to be used for construction
    :param int name_colmns_by_row: which row to give column names
    :param int name_rows_by_column: which column to give row names
    :param dict keywords: other parameters
    """
    if isinstance(file_name, tuple):
        sheet = get_sheet(
            file_type=file_name[0],
            file_content=file_name[1],
            sheet_name=sheetname,
            **keywords
        )
    else:
        sheet = get_sheet(
            file_name=file_name, sheet_name=sheetname, **keywords
        )
    return sheet
Exemplo n.º 2
0
def merge_files(file_array, outfilename=DEFAULT_OUT_FILE):
    """merge many files horizontally column after column
    :param str outfilename: save the sheet as
    """
    if os.path.exists(outfilename):
        raise NotImplementedError(MESSAGE_WARNING)
    content = []
    for file_name in file_array:
        sheet = get_sheet(file_name=file_name)
        content.extend(list(sheet.columns()))
    merged_sheet = get_sheet(array=content)
    merged_sheet.transpose()
    merged_sheet.save_as(outfilename)
    return outfilename
Exemplo n.º 3
0
def merge_files(file_array, outfilename=DEFAULT_OUT_FILE):
    """merge many files horizontally column after column
    :param str outfilename: save the sheet as
    """
    if os.path.exists(outfilename):
        raise NotImplementedError(MESSAGE_WARNING)
    content = []
    for f in file_array:
        sheet = get_sheet(file_name=f)
        content.extend(list(sheet.columns()))
    merged_sheet = get_sheet(array=content)
    merged_sheet.transpose()
    merged_sheet.save_as(outfilename)
    return outfilename
def load_from_dict(the_dict, with_keys=True, **keywords):
    """Return a sheet from a dictionary of one dimensional arrays

    :param dict the_dict: its value should be one dimensional array
    :param bool with_keys: indicate if dictionary keys should be
                           appended or not
    """
    return get_sheet(adict=the_dict, with_keys=with_keys, **keywords)
Exemplo n.º 5
0
def load_from_dict(the_dict, with_keys=True, **keywords):
    """Return a sheet from a dictionary of one dimensional arrays

    :param dict the_dict: its value should be one dimensional array
    :param bool with_keys: indicate if dictionary keys should be
                           appended or not
    """
    return get_sheet(adict=the_dict, with_keys=with_keys, **keywords)
def Reader(file_name=None, sheetname=None, **keywords):
    """
    A single sheet excel file reader

    Default is the sheet at index 0. Or you specify one using sheet index
    or sheet name. The short coming of this reader is: column filter is
    applied first then row filter is applied next

    use as class would fail though
    changed since 0.0.7
    """
    if isinstance(file_name, tuple):
        return get_sheet(file_type=file_name[0],
                         file_content=file_name[1],
                         sheet_name=sheetname,
                         **keywords)
    else:
        return get_sheet(file_name=file_name, sheet_name=sheetname, **keywords)
Exemplo n.º 7
0
def load_from_records(records, **keywords):
    """Return a sheet from a list of records

    Sheet.to_records() would produce a list of dictionaries. All dictionaries
    share the same keys.
    :params list records: records are likely to be produced by
                          Sheet.to_records() method.
    """
    return get_sheet(records=records, **keywords)
Exemplo n.º 8
0
def merge_csv_to_a_book(filelist, outfilename):
    merged = Book()
    for file_name in filelist:
        sheet = get_sheet(file_name=file_name, delimiter=';')
        _, tail = os.path.split(
            file_name.replace("result_", '').replace('.csv', ''))
        sheet.name = tail
        merged += sheet
    merged.save_as(outfilename)
def load_from_records(records, **keywords):
    """Return a sheet from a list of records

    Sheet.to_records() would produce a list of dictionaries. All dictionaries
    share the same keys.
    :params list records: records are likely to be produced by
                          Sheet.to_records() method.
    """
    return get_sheet(records=records, **keywords)
Exemplo n.º 10
0
def Reader(file_name=None, sheetname=None, **keywords):
    """
    A single sheet excel file reader

    Default is the sheet at index 0. Or you specify one using sheet index
    or sheet name. The short coming of this reader is: column filter is
    applied first then row filter is applied next

    use as class would fail though
    changed since 0.0.7
    """
    if isinstance(file_name, tuple):
        return get_sheet(file_type=file_name[0],
                         file_content=file_name[1],
                         sheet_name=sheetname,
                         **keywords)
    else:
        return get_sheet(file_name=file_name, sheet_name=sheetname,
                         **keywords)
Exemplo n.º 11
0
def load(file_name, sheetname=None, **keywords):
    """Constructs an instance :class:`Sheet` from a sheet of an excel file

    except csv, most excel files has more than one sheet.
    Hence sheetname is required here to indicate from which sheet the instance
    should be constructed. If this parameter is omitted, the first sheet, which
    is indexed at 0, is used. For csv, sheetname is always omitted because csv
    file contains always one sheet.
    :param str sheetname: which sheet to be used for construction
    :param int name_colmns_by_row: which row to give column names
    :param int name_rows_by_column: which column to give row names
    :param dict keywords: other parameters
    """
    if isinstance(file_name, tuple):
        sheet = get_sheet(file_type=file_name[0],
                          file_content=file_name[1],
                          sheet_name=sheetname,
                          **keywords)
    else:
        sheet = get_sheet(file_name=file_name, sheet_name=sheetname,
                          **keywords)
    return sheet
Exemplo n.º 12
0
def merge_csv_to_a_book(filelist, outfilename=DEFAULT_OUT_XLS_FILE):
    """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
    """
    merged = Book()
    for file_name in filelist:
        sheet = get_sheet(file_name=file_name)
        _, tail = os.path.split(file_name)
        sheet.name = tail
        merged += sheet
    merged.save_as(outfilename)
Exemplo n.º 13
0
def merge_csv_to_a_book(filelist, outfilename=DEFAULT_OUT_XLS_FILE):
    """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
    """
    merged = Book()
    for file_name in filelist:
        sheet = get_sheet(file_name=file_name)
        head, tail = os.path.split(file_name)
        sheet.name = tail
        merged += sheet
    merged.save_as(outfilename)
Exemplo n.º 14
0
def load_from_memory(file_type, file_content, sheetname=None, **keywords):
    """Constructs an instance :class:`Sheet` from memory

    :param str file_type: one value of these: 'csv', 'tsv', 'csvz',
    'tsvz', 'xls', 'xlsm', 'xslm', 'ods'
    :param iostream file_content: file content
    :param str sheetname: which sheet to be used for construction
    :param dict keywords: any other parameters
    """
    return get_sheet(file_type=file_type,
                     file_content=file_content,
                     sheet_name=sheetname,
                     **keywords)
Exemplo n.º 15
0
def load_from_memory(file_type,
                     file_content,
                     sheetname=None,
                     **keywords):
    """Constructs an instance :class:`Sheet` from memory

    :param str file_type: one value of these: 'csv', 'tsv', 'csvz',
    'tsvz', 'xls', 'xlsm', 'xslm', 'ods'
    :param iostream file_content: file content
    :param str sheetname: which sheet to be used for construction
    :param dict keywords: any other parameters
    """
    return get_sheet(file_type=file_type,
                     file_content=file_content,
                     sheet_name=sheetname,
                     **keywords)
Exemplo n.º 16
0
def ColumnSeriesReader(file_name=None, sheetname=None, series=0, **keywords):
    """A single sheet excel file reader and it has row headers in a selected column

    use as class would fail
    changed since 0.0.7
    """
    if isinstance(file_name, tuple):
        return get_sheet(file_type=file_name[0],
                         file_content=file_name[1],
                         name_rows_by_column=series,
                         **keywords)
    else:
        return load(file_name,
                    sheetname=sheetname,
                    name_rows_by_column=series,
                    **keywords)
Exemplo n.º 17
0
def ColumnSeriesReader(file_name=None, sheetname=None, series=0, **keywords):
    """A single sheet excel file reader and it has row headers in a selected column

    use as class would fail
    changed since 0.0.7
    """
    if isinstance(file_name, tuple):
        return get_sheet(file_type=file_name[0],
                         file_content=file_name[1],
                         name_rows_by_column=series,
                         **keywords)
    else:
        return load(file_name,
                    sheetname=sheetname,
                    name_rows_by_column=series,
                    **keywords)
Exemplo n.º 18
0
def update_rows(infilename, row_dicts, outfilename=None):
    """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
    """
    default_out_file = OUT_FILE_FORMATTER % infilename
    if outfilename:
        default_out_file = outfilename
    if os.path.exists(default_out_file):
        raise NotImplementedError(MESSAGE_WARNING)
    sheet = get_sheet(file_name=infilename, name_rows_by_column=0)
    series = sheet.rownames
    for k in row_dicts.keys():
        index = series.index(str(k))
        sheet.set_row_at(index, row_dicts[k])
    sheet.save_as(default_out_file)
Exemplo n.º 19
0
def update_rows(infilename, row_dicts, outfilename=None):
    """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
    """
    default_out_file = OUT_FILE_FORMATTER % infilename
    if outfilename:
        default_out_file = outfilename
    if os.path.exists(default_out_file):
        raise NotImplementedError(MESSAGE_WARNING)
    sheet = get_sheet(file_name=infilename, name_rows_by_column=0)
    series = sheet.rownames
    for k in row_dicts.keys():
        index = series.index(str(k))
        sheet.set_row_at(index, row_dicts[k])
    sheet.save_as(default_out_file)
Exemplo n.º 20
0
def update_columns(infilename, column_dicts, outfilename=None):
    """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


    """
    default_out_file = OUT_FILE_FORMATTER % infilename
    if outfilename:
        default_out_file = outfilename
    if os.path.exists(default_out_file):
        raise NotImplementedError(MESSAGE_WARNING)
    sheet = get_sheet(file_name=infilename, name_columns_by_row=0)
    series = sheet.colnames
    for k in column_dicts.keys():
        index = series.index(str(k))
        sheet.set_column_at(index, column_dicts[k])
    sheet.save_as(default_out_file)
Exemplo n.º 21
0
def update_columns(infilename, column_dicts, outfilename=None):
    """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


    """
    default_out_file = OUT_FILE_FORMATTER % infilename
    if outfilename:
        default_out_file = outfilename
    if os.path.exists(default_out_file):
        raise NotImplementedError(MESSAGE_WARNING)
    sheet = get_sheet(file_name=infilename, name_columns_by_row=0)
    series = sheet.colnames
    for k in column_dicts.keys():
        index = series.index(str(k))
        sheet.set_column_at(index, column_dicts[k])
    sheet.save_as(default_out_file)