pandasdataframecolumn-widthpandas-styles

Set columnwidth of a styled dataframe


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 ?


Solution

  • 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 :

    But to format column-with, use openpyxl.