pythonflaskpylatex

How to handle wide tables in pylatex?


I want to generate a PDF using Pylatex. I can handle long table problems but I still have a wide table problem. I'm using the A4 format because it should be printable. I do not want to rotate table but I can use another solution. Do you have any recommendations for that? Here is my code block:

model = # I'm getting a model object in this line
column_size = len(item.get("columns"))
tabular_size = "|c" * column_size + "|"
table = LongTable(tabular_size)
table.add_hline()
headers = tuple(str(item['header']).replace("_", " ") for item in item.get("columns"))
table.add_row(headers)
table.add_hline()
for obj in model:
    row_list = []
    for val in item.get("columns"):
        key = (val["key"])
        value = getattr(obj, key)
        row_list.append(value)
    table.add_row(MultiColumn(
         column_size,
         data=list(row_list))
         )
    table.add_hline()
    
doc.append(VerticalSpace(r'0.3in'))
doc.append(NewLine())

Solution

  • There are a few solutions to this problem that dont involve having to rotate the table. These include

    1. Reducing the font size:

      eg. table.append(NoEscape(r'\small'))

    2. Using the \thead command will allow table to wrap text within the cell, therefore reducing the overall width of the table.

    3. Reducing inter column spacing by manipulating the value of 'tabcolsep' as required

      eg. table.append(NoEscape(r'\setlength{\tabcolsep}{4pt}'))

    4. Change entire page to landscape mode without rotating the table so that the wider dimension then becomes the height.