Skip to content

How to horizontally center a table? align="CENTER" is not working for me #1306

Closed
@Lucas-C

Description

@Lucas-C

Discussed in #1304

Originally posted by DerekRobin November 19, 2024
I was wondering how to horizontally center a table on the page? I know about the align parameter that can be passed when instantiating a table. However, for some reason passing align="CENTER" does not align the table to the center of the page.

I have included the code I am using to generate the table below from a pandas dataframe (df)

df = df.applymap(str)
COLUMNS = [list(df)]  
ROWS = df.values.tolist()  
DATA = COLUMNS + ROWS
with self.table(
    borders_layout="NONE",
    headings_style=FontFace(emphasis="BOLD", color="#000000", fill_color="#ececec"),
    cell_fill_color="#ffffff",
    cell_fill_mode="EVEN_ROWS",
    line_height=self.font_size * 1.5,
    text_align=(
        "LEFT",
        "CENTER",
        "CENTER",
        "CENTER",
        "CENTER",
        "CENTER",
        "CENTER",
        "CENTER",
        "CENTER",
        "CENTER",
    ),
    col_widths=(190, 70, 70, 70, 70, 70, 70, 70),
    align="CENTER"
) as table:
    for i, data_row in enumerate(DATA):
        # some entries of data_row are conditionally modified here
        # in order to ensure the table has the correct data
        # the code within this function does not interface with any FPDF code
        data_row = modify_data_row()
       
        if i == 0:
            table.row(data_row)
        else:
            table.row(data_row, style=FontFace(color=color, fill_color=fill_color))
```</div>

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions