def test_set_book_attribute(): file_name = os.path.join("tests", "fixtures", "test-multiple.csvz") with open(file_name, "rb") as f: csvz_content = f.read() book = Book() book.csvz = csvz_content expected = ("---pyexcel:sheet1---\r\n" + "1,4,9\r\n" + "2,5,8\r\n" + "3,6,7\r\n" + "---pyexcel---\r\n" + "---pyexcel:sheet2---\r\n" + "1,4,9\r\n" + "2,5,8\r\n" + "3,6,7\r\n" + "---pyexcel---\r\n" + "---pyexcel:sheet3---\r\n" + "1,4,9\r\n" + "2,5,8\r\n" + "3,6,7\r\n" + "---pyexcel---\r\n") eq_(book.csv, expected)
def test_set_bookdict(): b = Book() b.bookdict = {"sheet1": [[1]], "sheet2": [[2]]} expected = dedent(""" sheet1: +---+ | 1 | +---+ sheet2: +---+ | 2 | +---+""").strip() eq_(str(b), expected)
def test_set_bookdict(): b = Book() b.bookdict = {"sheet1": [[1]], "sheet2": [[2]]} expected = dedent(""" sheet1: +---+ | 1 | +---+ sheet2: +---+ | 2 | +---+""").strip() eq_(str(b), expected) expected = OrderedDict([("sheet1", [[1]]), ("sheet2", [[2]])]) eq_(b.bookdict, expected)
def group_rows_by_column(self, column_index_or_name): """Group rows with similiar column into a two dimensional array. Example:: >>> import pyexcel as p >>> sample_data = [ ... ["22/09/2017", "morning"], ... ["22/09/2017", "afternoon"], ... ["23/09/2017", "morning"], ... ["23/09/2017", "afternoon"] ... ] >>> sheet = p.Sheet(sample_data) >>> sheet.group_rows_by_column(0) 22/09/2017: +------------+-----------+ | 22/09/2017 | morning | +------------+-----------+ | 22/09/2017 | afternoon | +------------+-----------+ 23/09/2017: +------------+-----------+ | 23/09/2017 | morning | +------------+-----------+ | 23/09/2017 | afternoon | +------------+-----------+ :returns: an instance of a Book """ from pyexcel import Book groups = defaultdict(list) if isinstance(column_index_or_name, int): for row in self.to_array(): groups[row[column_index_or_name]].append(row) else: if len(self.colnames) == 0: self.name_columns_by_row(0) column_index = self.colnames.index(column_index_or_name) for row in self.rows(): if len(groups[row[column_index]]) == 0: groups[row[column_index]].append(self.colnames) groups[row[column_index]].append(row) return Book(groups)
def test_book_register_presentation(): Book.register_presentation(FIXTURE) b = Book({"sheet": [[1, 2]]}) assert b.dummy == FIXTURE