I am trying to change the column width of my dataframe through the use of pandas' style and set_properties, but the width parameter is the only thing that doesn't seem to work while the rest of the parameters work and apply the appropriate changes.
with pd.ExcelWriter(results_path, mode="a", engine="openpyxl", if_sheet_exists="overlay") as writer:
df.style.set_properties(
**{'width': '600px', 'text-align': 'right', 'background-color': '#B4C6E7', 'color': 'black',
'border': '1.3px solid black'}).to_excel \
(writer, sheet_name="Sheet", header=True, startrow=0, startcol=0,index=False)
I also tried using pd.set_option('display.max_colwidth', None), but that did not have an effect either.
The
widthparameter inset_properties()is used to set the width of a column's content, not the entire column itself. Alsoset_properties()is more focused on cell-leveling styling and formatting, whileset_column()is used to control the column width. Along with that it's important to know thatpd.set_option('display.max_colwidth',None)affects how text is displayed when printed to the console, not the actual column width in the output. To change the width of the entire column, you would need to use other methods shown below: