Python - How to change column width in a pdf table in order to fit everything?

Viewed 216

How can I change the columns width of the tables in the code below in order to fit all text? The first column needs to be larger than the second and third row. For now the columns width is distributed evenly.

Code:

from fpdf import FPDF 
pdf = FPDF()

alfa = 5
lasna = 5

pdf.add_page() 
  
pdf.set_font("Times", size = 16) 

TABLE_COL_NAMES = ("Propriedade", "Valor", "Unidade")
TABLE_DATA = (
    ("Ângulo da vertente", str(alfa), "º"),
    ("Comprimento de cada asna", str(lasna), "m"),
)


pdf.set_font("Times", size=16)
line_height = pdf.font_size * 2
col_width = pdf.epw / 4  # distribute content evenly

def render_table_header():
    pdf.set_font(style="B")  # enabling bold text
    for col_name in TABLE_COL_NAMES:
        pdf.cell(col_width, line_height, col_name, border=1)
    pdf.ln(line_height)
    pdf.set_font(style="")  # disabling bold text

render_table_header()
for _ in range(1):  # repeat data rows
    for row in TABLE_DATA:
        if pdf.will_page_break(line_height):
            render_table_header()
        for datum in row:
            pdf.cell(col_width, line_height, datum, border=1)
        pdf.ln(line_height)

pdf.output("Relatório_TED.pdf") 
0 Answers
Related