FPDF2 Python TypeError

557 Views Asked by At

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'

3

There are 3 best solutions below

0
On

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

0
On

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.

2
On

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