I am using Python 3 to generate an Excel file. In each run a number of columns is different.
With static number of columns, I would use e.g.:
writer = pd.ExcelWriter("File.xlsx", engine = "xlsxwriter")
writer.sheets["Sheet"].set_column("A:G", 10)
What is a code expression to select all Excel columns (replacing "A:G" above)?
CodePudding user response:
If you can determine the shape of your data, using the integer based notation can be helpful.
writer.sheets["Sheet"].set_column(0,data.shape[1], 10)
CodePudding user response:
Another way of actually auto-adjusted width found [here].
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.close()