Set columnwidth of a styled dataframe

85 Views Asked by At

Considering this code :

df = pd.DataFrame([[1, 2], [3, 4], [4, 6]], index=["A1", "A2", "Total"])

dfs = df.style

I want to add the css style 'column-width:200' to all the columns of dfs

When exporting to Excel (openpyxl engine), i want that this column width is preserved.

I think it's impossible. Can you confirm/infirm ?

1

There are 1 best solutions below

0
floupinette On BEST ANSWER

Setting column-width with a styled dataframe and export it to Excel is not possible.

To set column-width of Excel table, use openpyxl. There are plenty of examples on internet.

Styled dataframes are usefull to format cells :

  • Formatting will be exportable to Excel.
  • Formatting with pandas will be much much faster than with openpyxl

But to format column-with, use openpyxl.