From 7265cd1d2411f529f092f7858806ba46ee43dcb5 Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Tue, 20 Feb 2024 13:50:32 +0100 Subject: [PATCH] Allow custom cell_fill_mode in tables --- CHANGELOG.md | 3 ++- docs/Tables.md | 12 ++++++++++++ fpdf/enums.py | 9 ++++++++- .../table_with_cell_fill_custom_class.pdf | Bin 0 -> 1447 bytes test/table/test_table.py | 18 ++++++++++++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 test/table/table_with_cell_fill_custom_class.pdf diff --git a/CHANGELOG.md b/CHANGELOG.md index 8232fe945..00f3563b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default', ## [2.7.9] - Not released yet ### Added +* allow to define custom `cell_fill_mode` logic for tables: [_Set cells background_ - documentation section](https://py-pdf.github.io/fpdf2/Tables.html#set-cells-background) ### Fixed @@ -47,7 +48,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default', * outer table borders are now drawn continuously for nonzero `gutter_width`/`gutter_height`, with spacing applied inside the border similar to HTML tables - thanks to @mjasperse - cf. [#1071](https://github.com/py-pdf/fpdf2/issues/1071) * removed the requirement that all rows in a `Table` have the same number of columns - thanks to @mjasperse ### Deprecated -- font aliases (`Arial` → `Helvetica`, `CourierNew` → `Courer`, `TimesNewRoman` → `Times`). They will be removed in a later release. +- font aliases (`Arial` → `Helvetica`, `CourierNew` → `Courier`, `TimesNewRoman` → `Times`). They will be removed in a later release. ## [2.7.7] - 2023-12-10 ### Added diff --git a/docs/Tables.md b/docs/Tables.md index 2ce8ae81c..2ced09def 100644 --- a/docs/Tables.md +++ b/docs/Tables.md @@ -208,6 +208,18 @@ The cell color is set following those settings, ordered by priority: 4. The table setting `cell_fill_color`, if `cell_fill_mode` indicates to fill a cell 5. The document `.fill_color` set before rendering the table +Finally, it is possible to define your own cell-filling logic: + +```python +class EvenOddCellFillMode(): + def should_fill_cell(self, i, j): + return i % 2 and j % 2 + +... +with pdf.table(cell_fill_color=lightblue, cell_fill_mode=EvenOddCellFillMode()) as table: + ... +``` + ## Set borders layout diff --git a/fpdf/enums.py b/fpdf/enums.py index 2032e39c1..0104bae13 100644 --- a/fpdf/enums.py +++ b/fpdf/enums.py @@ -54,7 +54,7 @@ def coerce(cls, value): raise ValueError(f"{value} is not a valid {cls.__name__}") - raise TypeError(f"{value} cannot convert to a {cls.__name__}") + raise TypeError(f"{value} cannot be converted to a {cls.__name__}") class CoerciveIntEnum(IntEnum): @@ -314,6 +314,13 @@ class TableCellFillMode(CoerciveEnum): COLUMNS = intern("COLUMNS") "Fill only table cells in odd columns" + @classmethod + def coerce(cls, value): + "Any class that has a .should_fill_cell() method is considered a valid 'TableCellFillMode' (duck-typing)" + if callable(getattr(value, "should_fill_cell", None)): + return value + return super().coerce(value) + def should_fill_cell(self, i, j): if self is self.NONE: return False diff --git a/test/table/table_with_cell_fill_custom_class.pdf b/test/table/table_with_cell_fill_custom_class.pdf new file mode 100644 index 0000000000000000000000000000000000000000..d3b81c512825ba8788629c9bd43c03db87360649 GIT binary patch literal 1447 zcmbtUeM}o=7$@R_I}jFSN=O#peZZIo?yetqJ=Zk-Ji(0sEprLY7Ow3J9=3P1cTL+F zShl%kF>~3LvA}>R4K7Y4CTqgPKdRfvA`+A8lrb_gAe)f5s0mZ%=BDqJR?aQaKYagr z?|$$5{GN~Z_dI3wcD9nN(qbeafaqz%j7AK%iD5woB!+L}eG+hM(UEf(hSx$L&soJN za1#j8Gx{ox0T^^-m4QL0jj?tJ@OrKlN|?!nLBXfcX;idz?NG3IWnL6AoB(Hexf%#z zzaJ4OrrWug7?uHECqSF%4+n&0SOQ9T&a4v~Cv$$WRpmwraV2^|hJq{s-Lg$R7ec>T z1}BunaL5ZKK&vSg=PEwMG|R{Gei?>PtbR_0cIXv-5W}}ap;dkz=tx4blH?HN0$8-K zzj0jHZ!Vr*TpQu(E3u+PqwD$0XZTxr`=iw#9?NCd?;L!X>bCaCZ}!diuPQmQE%m}e z<(0D&Io%&859X|Z< z-P&t`w%*@n0zF4(zVv5O4zY6XV!2 zIazQbvbXzw#PziB%`46M;eth-q@615cy6k6UGByM?mHI(Wrdv|#BXc<$UXT(Y+8|H z=8-bO)T>_D%1MwFkw)-FJ^-akrx@Un@*DzsYhGvt_)cE12oj$KFx(aP$Z5SRsz;5o zEPjPUB_iryTv$cF@Be#HB>cBbl?04NoF##h8)_C-1C$V)NT7IqNc6g(4BR+sc$6oM zqHJpdNcGmtZp-X7yHqI{R1R!N<4W_|Q5%2?`^zLlF{DOIk_>@X6Ul5Qh|OD6#?cUL z!3e-$%K7pFlwPmZgBGxYF(7>egcXcIVJl08k<8=wH1y*bO`%Egh^>J_^Xd_d(WBJ< ziIIdJ6)ua(A&&RMP