3
from pandas import ExcelWriter
writer = ExcelWriter('PythonExport.xlsx')
parta.to_excel(writer,index=False)
writer.save()

This is the current situation

This is the desired outcome

How do I adjust the width of the excel cell to see all the words clearly using python?

I tried other methods which i saw on stackoverflow but they didn't work for me. An example is trying 'ws.Columns.AutoFit()' but I received an error saying AttributeError: '_XlsxWriter' object has no attribute 'Columns'

1 Answers1

5

To use set_column method, you need to have your worksheet object. Take a look at this it will make the size of the cells entered to 50:

writer = pandas.ExcelWriter('PythonExport.xlsx')
parta.to_excel(writer, sheet_name='Sheet1')
workbook  = writer.book
worksheet = writer.sheets['Sheet1']
worksheet.set_column(1, 2, 50)
writer.save()

Parameters of the set_column() method: first_col, last_col, width, format(optional),options(optional)

Additionally, you could do something like:

width = len("your text")
worksheet.set_column(1, 2, 50)
Shivam Mishra
  • 1,731
  • 2
  • 11
  • 29