Jan-14-2024, 07:56 AM
(Jan-14-2024, 07:52 AM)1418 Wrote: Can you tell me what additional library I need, cheers
First you can try this answer
writer = pd.ExcelWriter('/path/to/output/file.xlsx') df.to_excel(writer, sheet_name='sheetName', index=False, na_rep='NaN') for column in df: column_length = max(df[column].astype(str).map(len).max(), len(column)) col_idx = df.columns.get_loc(column) writer.sheets['sheetName'].set_column(col_idx, col_idx, column_length) writer.save()which should adjust all your columns to a reasonable width, unless you have a really long text in them.
or manual way
col_idx = df.columns.get_loc('columnName') writer.sheets['sheetName'].set_column(col_idx, col_idx, 15)note that you need to use ExcelWriter from pandas in both examples
or install xlsxwriter or styleframe or openpyxl or whatever else they used in the example and do it that way