jorisschellekens / borb

borb is a library for reading, creating and manipulating PDF files in python.
https://borbpdf.com/
Other
3.4k stars 147 forks source link

Different color for the header on an `even_odd` colored table #114

Closed obdura closed 2 years ago

obdura commented 2 years ago

Hi, recently I were doing a document with tables. I were confronted with the problem that I could not put a header with different color on an even_odd_row_colors() colored table.

I created a custom class that inherits from FixedColumnWidthTable (I may have do it generally inheriting from Table) to solve this purpose. This now allows to create a different colored header on these kind of colored tables:

from borb.pdf.canvas.layout.table.table import Table
from borb.pdf.canvas.layout.table.fixed_column_width_table import FixedColumnWidthTable
from borb.pdf.canvas.color.color import Color

class CustomFixedColumnTable(FixedColumnWidthTable):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def even_odd_row_colors_with_a_different_color_header(
        self, even_row_color: Color, odd_row_color: Color, header_row_color: Color
    ) -> "Table":
        """
        This function colors the Table with the classic "zebra stripes",
        but with a different color at the header.
        e.a. one color for all even rows, and a contrasting color for the odd rows.
        This function returns self.
        """
        for tc in self._get_cells_at_row(0):
            tc._background_color = header_row_color

        for r in range(1, self._number_of_rows):
            for tc in self._get_cells_at_row(r):
                if r % 2 == 0:
                    tc._background_color = even_row_color
                else:
                    tc._background_color = odd_row_color
        return self

It helped me to create a table like this one:

image

Maybe this kind of improvement could be added to a new version of borb.

Also, it is a good library that you have made.

jorisschellekens commented 2 years ago

Hi there,

I love your suggestion. I'm going to include the following improvement to Table

    def even_odd_row_colors(
        self,
            even_row_color: Color,
            odd_row_color: Color,
            header_row_color: typing.Optional[Color] = None
    ) -> "Table":
        """
        This function colors the Table with the classic "zebra stripes"
        e.a. one color for all even rows, and a contrasting color for the odd rows.
        :param even_row_color:      the Color to be used for even rows
        :param odd_row_color:       the Color to be used for odd rows
        :param header_row_color:    the Color to be used for the header row, if None is specified the even_row_color will be used
        This function returns self.
        """
        if header_row_color is None:
            header_row_color = even_row_color
        assert header_row_color is not None
        for r in range(0, self._number_of_rows):
            for tc in self._get_cells_at_row(r):
                if r == 0:
                    tc._background_color = header_row_color
                else:
                    if r % 2 == 0:
                        tc._background_color = even_row_color
                    else:
                        tc._background_color = odd_row_color
        return self

This ensures that:

You can expect this fix in the upcoming release (normally this weekend)

Kind regards, Joris Schellekens