예제 #1
0
def test_no_default_style(datadir):
    from ..stylesheet import apply_stylesheet
    datadir.chdir()
    archive = ZipFile(BytesIO(), "a")
    archive.write("no_default_styles.xml", "xl/styles.xml")

    wb = Workbook()
    wb._named_styles = []
    apply_stylesheet(archive, wb)

    assert wb._named_styles != []
예제 #2
0
    def write(self,
              filename,
              data,
              sheet=None,
              sheet_index=0,
              has_title=False,
              is_overwrite=False):
        '''Write data to excel file.

        Arguments:
            file {str} -- The absolute name of excel file
            data {list} -- The data list which will write to excel file.

        Keyword Arguments:
            sheet {str} -- The name of destnation sheet.
            sheet_index {int} -- The index of sheet, start with 0.
            has_title {bool} -- True means the first row as table header (default: {False})
            is_overwrite {bool} -- When the destnation file exist, it will be overwrite
                when this flag is true (default: {False}).
        '''

        if not is_overwrite and os.path.isfile(filename):
            logger.error(
                'The destination file {} is already exist, abort!'.format(
                    filename))
            sys.exit(1)

        if os.path.isfile(filename):
            wb = load_workbook(filename)
            sheets = wb.get_sheet_names()
            if sheet and sheet in sheets:
                ws = wb.get_sheet_by_name(sheet)
                wb.remove_sheet(ws)

            ws = wb.create_sheet(sheet, index=sheet_index)
            if not sheet:
                ws = wb.active
        else:
            wb = Workbook()
            if sheet:
                ws = wb.create_sheet(sheet, index=sheet_index)
            else:
                ws = wb.active

        ws.print_options.horizontalCentered = True
        ws.print_options.verticalCentered = True
        wb._named_styles = NamedStyleList()

        max_width = {}
        for row_id, row in enumerate(data, start=1):
            for column_id, column in enumerate(row, start=1):
                # for column width
                last_width = max_width[
                    column_id] if column_id in max_width else 0
                # calcalated the column
                unicode_column = column if isinstance(
                    column, unicode) else str(column).decode('utf-8')
                chinese_chars = chinese_counter(unicode_column)
                curr_width = len(unicode_column) + chinese_chars + 2
                # update the biggest column width
                if curr_width > last_width:
                    chinese_chars = chinese_counter(unicode_column)
                    # chinese has 2 characters
                    max_width[column_id] = curr_width

                # for table header
                if has_title and row_id == 1:
                    ws.cell(row=1, column=column_id,
                            value=column).style = self.__header_style
                else:
                    # write data
                    cell = ws.cell(row=row_id, column=column_id, value=column)
                    cell.border = self.__border
                    cell.alignment = self.__alignment

        # set column width
        for k, v in max_width.items():
            ws.column_dimensions[get_column_letter(k)].width = v
        # save file
        wb.save(filename)