Example #1
0
 def prep_cell(self, cell: Cell):
     super().prep_cell(cell)
     if isinstance(self.drf_field, IntegerField):
         cell.number_format = get_setting("INTEGER_FORMAT") or FORMAT_NUMBER
     else:
         cell.number_format = get_setting(
             "DECIMAL_FORMAT") or FORMAT_NUMBER_00
Example #2
0
 def prep_cell(self, cell: Cell):
     super().prep_cell(cell)
     if isinstance(self.drf_field, DateTimeField):
         cell.number_format = get_setting(
             "DATETIME_FORMAT") or FORMAT_DATE_DATETIME
     elif isinstance(self.drf_field, DateField):
         cell.number_format = get_setting(
             "DATE_FORMAT") or FORMAT_DATE_YYYYMMDD2
     elif isinstance(self.drf_field, TimeField):
         cell.number_format = get_setting(
             "TIME_FORMAT") or FORMAT_DATE_TIME4
Example #3
0
def test_number_format(DummyWorksheet, Cell):
    ws = DummyWorksheet
    ws.parent._number_formats.add("dd--hh--mm")

    cell = Cell(ws, column="A", row=1)
    cell.number_format = "dd--hh--mm"
    assert cell.number_format == "dd--hh--mm"
Example #4
0
def test_number_format(DummyWorksheet, Cell):
    ws = DummyWorksheet
    ws.parent._number_formats.add("dd--hh--mm")

    cell = Cell(ws, column="A", row=1)
    cell.number_format = "dd--hh--mm"
    assert cell.number_format == "dd--hh--mm"
Example #5
0
def set_cell_to_number(cell: Cell, cell_format: str = '0') -> Cell:
    """Sets the data type on a cell

    Sets the data type on a cell to number if the value of the cell is a number.
    Will not work on a list of numbers (cannot be processed as a single number).

    :param cell:
    :param cell_format:
    :return:
    """
    if cell.value and not cell.column == 'A':
        if cell.value.isdigit():
            cell.data_type = 'n'
            cell.number_format = cell_format
        with suppress(ValueError):
            if float(cell.value):
                cell.data_type = 'n'
                cell.number_format = cell_format
    return cell
Example #6
0
def set_and_fill_hours_cell(cell: Cell, cell_value: str) -> None:
    if cell_value is not None:
        cell.value = cell_value
    else:
        cell.value = cell_value
    alignment = Alignment(vertical=constants.VERCTICAL_TOP.value,
                          horizontal=constants.CENTER_ALINGMENT.value)
    cell.alignment = alignment
    cell.number_format = constants.HOURS_FORMAT.value
    set_borders_between_columns(cell)
def copyCellFormat(cellsrc:Cell, sheetSrc:Worksheet, celldest:Cell, sheetDes:Worksheet):
    celldest.fill = copy(cellsrc.fill)
    celldest.font = copy(cellsrc.font)
    celldest.border = copy(cellsrc.border)
    celldest.alignment = copy(cellsrc.alignment)
    celldest.number_format = copy(cellsrc.number_format)
    celldest.protection = copy(cellsrc.protection)

    for merged_cell in sheetSrc.merged_cells:
        if merged_cell.min_col==cellsrc.col_idx and merged_cell.min_row==cellsrc.row:
            sheetDes.merge_cells(start_row= celldest.row, end_row= celldest.row,
                                 start_column= merged_cell.min_col, end_column= merged_cell.max_col)
            break
Example #8
0
def styled_row(sheet,
               data,
               number_format=None,
               bold=False,
               color=None,
               color_txt=None):
    """ Adds styles to row data before writing

    """
    for cell in data:
        cell = Cell(sheet, column="A", row=1, value=cell)
        if number_format is not None:
            cell.number_format = number_format
        if bold:
            cell.font = Font(bold=True)
        if color is not None:
            cell.fill = PatternFill(start_color=color, fill_type='solid')
        if color_txt is not None:
            cell.font = Font(color=color_txt)
        yield cell
Example #9
0
    def write_sheet(self, name, fields, rows):
        # add the sheet to the workbook
        ws = self.workbook.create_sheet(title=name)
        # add the header row
        row = list()
        for field in fields:
            cell = Cell(ws, value=field["title"])
            # pylint: disable=assigning-non-slot
            cell.alignment = Alignment(horizontal="center")
            cell.font = Font(name=ReportWriter.TYPEFACE,
                             size=ReportWriter.FONT_SIZE,
                             bold=True)
            row.append(cell)
        ws.append(row)
        # add the data rows
        for data in rows:
            row = list()
            for field in fields:
                cell = Cell(ws, value=data[field["name"]])
                # pylint: disable=assigning-non-slot
                cell.alignment = Alignment(
                    horizontal=ReportWriter.DEFAULT_ALIGNMENT)
                if "align" in field.keys():
                    cell.alignment = Alignment(horizontal=field["align"])
                cell.font = Font(name=ReportWriter.TYPEFACE,
                                 size=ReportWriter.FONT_SIZE)
                if "number_format" in field.keys():
                    cell.number_format = field["number_format"]
                row.append(cell)
            ws.append(row)

        # apply column widths
        for i, field in enumerate(fields):
            # get the alphabetical column index
            alpha_index = get_column_letter(i + 1)
            # set the column width
            ws.column_dimensions[alpha_index].width = field["width"]
 def put_time(cell: Cell, duration, font=None, border=None, alignment=None):
     cell = XlsxDocumentService.put_text(cell, duration, font, border, alignment)
     cell.number_format = '0.00'
     return cell
Example #11
0
 def mk_cell(value):
     cell = Cell(ws)
     cell.number_format = '@' # Ensure text cell format
     cell.set_explicit_value(value)
     return cell
Example #12
0
 def mk_cell(value):
     cell = Cell(ws)
     cell.number_format = '@'  # Ensure text cell format
     cell.set_explicit_value(value)
     return cell
Example #13
0
 def _cell(self, sheet: Worksheet, value: E, number_format: str) -> Cell:
     cell = Cell(sheet, value=value)
     cell.number_format = number_format
     return cell
Example #14
0
 def put_time(cell: Cell, duration, font=None, border=None, alignment=None):
     cell = XlsxDocumentService.put_text(cell, duration, font, border,
                                         alignment)
     cell.number_format = '0.00'
     return cell