Example #1
0
    def __add__(self, other):
        """Overload the + sign

        :returns: a new book
        """
        from pyexcel.book import Book, local_uuid
        content = {}
        content[self.name] = self.__array
        if isinstance(other, Book):
            other_in_dict = other.to_dict()
            for key in other_in_dict.keys():
                new_key = key
                if len(other_in_dict.keys()) == 1:
                    new_key = other.filename
                if new_key in content:
                    uid = local_uuid()
                    new_key = "%s_%s" % (key, uid)
                content[new_key] = other_in_dict[key]
        elif isinstance(other, Matrix):
            new_key = other.name
            if new_key in content:
                uid = local_uuid()
                new_key = "%s_%s" % (other.name, uid)
            content[new_key] = other.get_internal_array()
        else:
            raise TypeError
        new_book = Book()
        new_book.load_from_sheets(content)
        return new_book
Example #2
0
    def __add__(self, other):
        """Overload the + sign

        :returns: a new book
        """
        from pyexcel.book import Book, local_uuid

        content = {}
        content[self.name] = self.__array
        if isinstance(other, Book):
            other_in_dict = other.to_dict()
            for key in other_in_dict.keys():
                new_key = key
                if len(other_in_dict.keys()) == 1:
                    new_key = other.filename
                if new_key in content:
                    uid = local_uuid()
                    new_key = "%s_%s" % (key, uid)
                content[new_key] = other_in_dict[key]
        elif isinstance(other, Matrix):
            new_key = other.name
            if new_key in content:
                uid = local_uuid()
                new_key = "%s_%s" % (other.name, uid)
            content[new_key] = other.get_internal_array()
        else:
            raise TypeError
        new_book = Book()
        new_book.load_from_sheets(content)
        return new_book
def _add(name, left, right):
    from pyexcel.book import Book, local_uuid

    content = {}
    content[name] = left
    if isinstance(right, Book):
        right_in_dict = copy.deepcopy(right.to_dict())
        for key in right_in_dict.keys():
            new_key = key
            if len(right_in_dict.keys()) == 1:
                new_key = right.filename
            if new_key in content:
                uid = local_uuid()
                new_key = "%s_%s" % (key, uid)
            content[new_key] = right_in_dict[key]
    elif isinstance(right, Matrix):
        new_key = right.name
        if new_key in content:
            uid = local_uuid()
            new_key = "%s_%s" % (right.name, uid)
        content[new_key] = copy.deepcopy(right.get_internal_array())
    else:
        raise TypeError
    new_book = Book()
    new_book.load_from_sheets(content)
    return new_book
Example #4
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)
Example #5
0
def merge_all_to_a_book(filelist, outfilename=DEFAULT_OUT_XLS_FILE):
    """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
    """
    merged = Book()
    for file_name in filelist:
        merged += get_book(file_name=file_name)
    merged.save_as(outfilename)
Example #6
0
def merge_all_to_a_book(filelist, outfilename=DEFAULT_OUT_XLS_FILE):
    """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
    """
    merged = Book()
    for file_name in filelist:
        merged += get_book(file_name=file_name)
    merged.save_as(outfilename)
Example #7
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)
Example #8
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)
Example #9
0
def get_book(**keywords):
    """Get an instance of :class:`Book` from an excel source

    :param file_name: a file with supported file extension
    :param file_content: the file content
    :param file_stream: the file stream
    :param file_type: the file type in *content*
    :param session: database session
    :param tables: a list of database table
    :param models: a list of django models
    :param bookdict: a dictionary of two dimensional arrays
    :param url: a download http url for your excel file

    see also :ref:`a-list-of-data-structures`

    Here is a table of parameters:

    ========================== ===============================
    source                     parameters
    ========================== ===============================
    loading from file          file_name, keywords
    loading from memory        file_type, content, keywords
    loading from sql           session, tables
    loading from django models models
    loading from dictionary    bookdict
    ========================== ===============================

    Where the dictionary should have text as keys and two dimensional
    array as values.
    """
    book_stream = sources.get_book_stream(**keywords)
    book = Book(book_stream.to_dict(),
                filename=book_stream.filename,
                path=book_stream.path)
    return book
Example #10
0
def get_book(**keywords):
    """
    Get an instance of :class:`Book` from an excel source
    """
    book_stream = sources.get_book_stream(**keywords)
    book = Book(book_stream.to_dict(),
                filename=book_stream.filename,
                path=book_stream.path)
    return book
Example #11
0
def merge(output_file_type, csv_output_delimiter, csv_output_encoding,
          csv_output_quotechar, csv_output_escapechar, csv_output_quoting,
          csv_output_lineterminator, csv_output_no_doublequote, sources,
          output):
    """
    Merge excel files in various file formats into one excel file

    SOURCES: space separated parameters. a file name, a directory name
             or a file patterns
    OUTPUT: a file name or '-'. '-' tells the command to use stdout
    """
    file_list = []
    dir_list = []
    glob_list = []

    merged_book = Book()
    for source in sources:
        if os.path.isfile(source):
            file_list.append(source)
        elif os.path.isdir(source):
            dir_list.append(source)
        else:
            glob_list.append(source)

    params = {}
    if output_file_type == 'csv' or output.endswith('csv'):
        params = _make_csv_params(csv_output_lineterminator,
                                  csv_output_encoding, csv_output_delimiter,
                                  csv_output_quoting, csv_output_quotechar,
                                  csv_output_escapechar,
                                  csv_output_no_doublequote)

    for afile in _join_the_list(file_list, dir_list, glob_list):
        try:
            book = get_book(file_name=afile)
            merged_book += book
            if output != "-":
                click.echo(".")
        except FileTypeNotSupported:
            # version pyexcel>=0.4.4
            click.echo("Skipping %s" % afile)
        except NotImplementedError:
            click.echo("Skipping %s" % afile)
    if merged_book.number_of_sheets() > 0:
        if output == '-':
            merged_book.save_to_memory(output_file_type, sys.stdout, **params)
        else:
            merged_book.save_as(output, **params)
    else:
        click.echo("Nothing to be merged")
Example #12
0
def merge(output_file_type,
          csv_output_delimiter, csv_output_encoding, csv_output_quotechar,
          csv_output_escapechar, csv_output_quoting,
          csv_output_lineterminator, csv_output_no_doublequote,
          sources, output):
    """
    Merge excel files in various file formats into one excel file

    SOURCES: space separated parameters. a file name, a directory name
             or a file patterns
    OUTPUT: a file name or '-'. '-' tells the command to use stdout
    """
    file_list = []
    dir_list = []
    glob_list = []

    merged_book = Book()
    for source in sources:
        if os.path.isfile(source):
            file_list.append(source)
        elif os.path.isdir(source):
            dir_list.append(source)
        else:
            glob_list.append(source)

    params = {}
    if output_file_type == 'csv' or output.endswith('csv'):
        params = _make_csv_params(
            csv_output_lineterminator, csv_output_encoding,
            csv_output_delimiter, csv_output_quoting,
            csv_output_quotechar, csv_output_escapechar,
            csv_output_no_doublequote)

    for afile in _join_the_list(file_list, dir_list, glob_list):
        try:
            book = get_book(file_name=afile)
            merged_book += book
            if output != "-":
                click.echo(".")
        except FileTypeNotSupported:
            # version pyexcel>=0.4.4
            click.echo("Skipping %s" % afile)
        except NotImplementedError:
            click.echo("Skipping %s" % afile)
    if merged_book.number_of_sheets() > 0:
        if output == '-':
            merged_book.save_to_memory(output_file_type, sys.stdout, **params)
        else:
            merged_book.save_as(output, **params)
    else:
        click.echo("Nothing to be merged")