0

I create a dataframe with very long titles and can also display them correctly with the Styler Object. In a next step, I would like to pass the dataframe formatted in this way in ".to_latex" of pylatex- Class and generate a PDF. Unfortunately, neither the set "style attributes" are permanently adopted by Dataframe, nor can I pass a styler object to_latex. Does anyone have a solution to this?

My Code so far:

df = pd.DataFrame(
    np.random.randn(10, 3),
    columns=["Very Long Column Title " + str(i) for i in range(3)],
)

df.style.set_table_styles(
    [dict(selector="th", props=[("max-width", "90px")])], overwrite=True
)

doc = pl.Document()
doc.packages.append(pl.Package("booktabs"))

with doc.create(pl.Section("Table")):
    with doc.create(pl.Table(position="htbp")) as table:
        table.add_caption("Test")
        table.append(pl.Command("centering"))
        table.append(pl.NoEscape(df.to_latex(header=True)))

doc.generate_pdf("full", clean_tex=False)
Peter70
  • 1
  • 1

0 Answers0