1

Error Python FPDF. Drawing a table

with pdf.table(
    borders_layout="NO_HORIZONTAL_LINES",
    cell_fill_color=(224, 235, 255),
    cell_fill_logic=lambda i, j: i % 2,
    col_widths=(42, 39, 35, 42),
    headings_style=headings_style,
    line_height=6,
    text_align=("LEFT", "CENTER", "RIGHT", "RIGHT"),
    width=160,
) as table:
    for data_row in data:
        row = table.row()

TypeError: __init__() got an unexpected keyword argument 'cell_fill_logic'

mistiru
  • 2,996
  • 3
  • 11
  • 27
  • https://pyfpdf.github.io/fpdf2/Tutorial.html cell_fill_logic=lambda i, j: i % 2, ???!!!!! – pippo1980 May 07 '23 at 16:24
  • 1
    As the error message says, the constructor of the class FPDF.Table does not have a `cell_fill_logic` keyword argument. The only ones like that are `cell_fill_color` and `cell_fill_mode`. It looks like wherever you are copying this code from either has an error, or is talking about a different version, or maybe you copied it wrong. See for yourself in the docs, here: https://pyfpdf.github.io/fpdf2/fpdf/table.html – BoarGules May 07 '23 at 16:31
  • better link https://pyfpdf.github.io/fpdf2/Tutorial.html#tuto-5-creating-tables – pippo1980 May 07 '23 at 16:42
  • I have already seen inside their documentation.And it's their own code I have copy and past in my IDE. (https://pyfpdf.github.io/fpdf2/Tutorial.html).The code is in the Tuto 5 – Romuald AMEGBEDJI May 07 '23 at 19:50

3 Answers3

0

use cell_fill_mode instead of cell_fill_logic as per comment above https://pyfpdf.github.io/fpdf2/fpdf/table.html#fpdf.table.Table

pippo1980
  • 2,181
  • 3
  • 14
  • 30
  • well cell_fill_mode = https://pyfpdf.github.io/fpdf/fpdf/enums.html#fpdf.enums.TableCellFillMode and here I get lost : class TableCellFillMode(CoerciveEnum): ??? whta the heck is a CoerciveEnum ??? – pippo1980 May 07 '23 at 17:52
  • 1
    see comments below anser by Pravash Panigrahi – pippo1980 May 07 '23 at 17:56
0

OK, new code, with input as countries.txt:

sssssssssssssssssss,mmmmmmmmmmmmmmmm,mmmmmmmmmmmmmmmmmmmmmmmsss
jjjjjjj,kkkkkkkkkkk,lllllllllllll
hhhhhhhhhhhh,ggggggggggggggggggg,ooooooooooooo

my code :

import csv
from fpdf import FPDF, fpdf


with open("countries.txt", encoding="utf8") as csv_file:
    data = list(csv.reader(csv_file, delimiter=","))
    

pdf = FPDF()
pdf.set_font("helvetica", size=14)

pdf.add_page()
pdf.set_draw_color(255, 0, 0)
pdf.set_line_width(0.3)

with pdf.table(
    # borders_layout="NO_HORIZONTAL_LINES",
    cell_fill_color=(255, 235, 0),
    
    
    # cell_fill_mode= 'ALL',
    cell_fill_mode= 'NONE',
    
    first_row_as_headings = False,
    
    col_widths=(42, 39, 35, 42),
    line_height=6,
    text_align=("LEFT", "CENTER", "RIGHT", "RIGHT"),
    width=160,
) as table:
    for data_row in data:
        row = table.row()
            
        for i, _data in enumerate(data_row):
            _color = (224, 235, 255) if i % 2 == 0 else None
            
        #     row.cell(_data, fill=_color)   ###  ----> TypeError: cell() got an unexpected keyword argument 'fill'

            row.cell(_data, style = fpdf.FontFace(fill_color = _color))

pdf.output("tuto5.pdf")

output with cell_fill_mode= 'NONE',:

image from pdf

output with cell_fill_mode= 'ALL',:

enter image description here

pippo1980
  • 2,181
  • 3
  • 14
  • 30
0

I've found the solution. It's not 'cell_fill_logic' but "first_row_as_headings" .Because ' lambda i, j: i % 2' returns a boolean . And cell_fill_mode in the documentation is not boolean.When I try with 'first_row_as_headings' , there is no error. And the value of the parameter 'cell_fill_mode' is not NONE but 'ROWS' . If you try with these modifications you can get their result (https://github.com/PyFPDF/fpdf2/raw/master/tutorial/tuto5.pdf).I think there is an error in the documentation of FPDF at Tutorial 5 where I found the script (https://pyfpdf.github.io/fpdf2/Tutorial.html#tuto-5-creating-tables) I hope they will correct their error. But I thank all of you for your suggestions.