Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tables with multiple header rows and merging cells #1064

Closed
mohindra9211 opened this issue Dec 14, 2023 · 4 comments · Fixed by #1088
Closed

Tables with multiple header rows and merging cells #1064

mohindra9211 opened this issue Dec 14, 2023 · 4 comments · Fixed by #1088

Comments

@mohindra9211
Copy link

mohindra9211 commented Dec 14, 2023

How do I create a table that includes multiple headers, and what is the process for merging cells (vertical merging)? I've tried different methods, but so far, none have produced the expected results.

tbl

This is my code

from fpdf import FPDF
from fpdf.fonts import FontFace

MULTI_HEADING_TABLE_DATA = (
    ("ID", "Category 1", "Category 2","Total"),
    ("ID", "Fruits", "Sales", "Vegetable", "Sales", "Total"),
    ("1", "A", "3", "4", "AA","25"),
    ("2", "B", "4", "5", "BB","38"),
    ("3", "C", "5", "6", "CC","40"),
)

pdf = FPDF()
pdf.set_font("Times", size=12)
pdf.add_page()

# Define the number of columns for each category
num_cols_per_category = [1, 2, 2,1]
headings_style = FontFace(color=(255,255,255), fill_color=(69, 69, 69))
with pdf.table(
    headings_style = headings_style,
    text_align="CENTER",
    cell_fill_color=230,
    cell_fill_mode="ROWS",
    borders_layout="MINIMAL",
    num_heading_rows=2,
) as table:
    for j, rowdata in enumerate(MULTI_HEADING_TABLE_DATA):
        if j == 0:
            # row with colspan
            row = table.row()
            for i, cell in enumerate(rowdata):
                row.cell(text=cell, colspan=num_cols_per_category[i])
        else:
            table.row(cells=rowdata)

# Save the PDF file
pdf.output("multi_heading_table.pdf")

Code Output
This is just an example; Please ignore the values ​​in the table.

Screenshot 2023-12-14 150118

@mohindra9211 mohindra9211 changed the title Tables with multiple header rows Tables with multiple header rows and merging cells Dec 14, 2023
@gmischler
Copy link
Collaborator

How do I create a table that includes multiple headers, and what is the process for merging cells? I've tried different methods, but so far, none have produced the expected results.

So what exactly is the expected result?
If you're thinking about vertical merging of cells, rowspan is not currently supported.
As always, contributions are welcome.

@mjasperse
Copy link

I've been playing around with making a simple rowspan implementation. It's coming along pretty well, although verifying all the corner cases, adding appropriate tests and updating the documentation will take some time. It's in the works though.

image

@Lucas-C
Copy link
Member

Lucas-C commented Jan 2, 2024

I've been playing around with making a simple rowspan implementation. It's coming along pretty well, although verifying all the corner cases, adding appropriate tests and updating the documentation will take some time. It's in the works though.

Nice @mjasperse!

Would you like to submit a PR? 🙂

@mjasperse
Copy link

Yep, hoping to submit a PR with the rowspan implementation this week! The corner cases needed some thought, will probably need a bit of iteration - but I think I've got the most common use-cases covered.

e.g.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants