1

can you help me with the column autowidth in excel. I have a data frame named "fp" and when i export it in excel, i have the following output Output1. i would like to adjust automatically the width of column so that it will look like this: Desired output.

the initial code is

filename = filedialog.askopenfilename(initialdir="/", title="Select file to convert")

fichierp = pd.read_excel(r'%s' % filename, index_col=1)
fp = fichierp.iloc[:, [0, 1, 2, 5, 8, 10,13]]

what i have tried is:

for column in fp:

column_width = max(fp[column].astype(str).map(len).max(), len(column))
col_idx = fp.columns.get_loc(column)
fp.set_column(col_idx, col_idx, column_width)`
  • If you are able to use ````xlsxWriter```` then you can use the code available [here](https://stackoverflow.com/questions/29463274/simulate-autofit-column-in-xslxwriter#:~:text=As%20a%20general%20rule%2C%20you,of%20characters%20in%20that%20column.) to adjust the column widths with ````set_column```` – Redox May 09 '22 at 15:13

0 Answers0