Does anyone know of any Python --> Excel libraries that contains a method to autofit all columns within a sheet given a Excel filename?
You can use pywin32 library:
from win32com.client import Dispatch
excel = Dispatch('Excel.Application')
wb = excel.Workbooks.Open("D:\\output.xlsx")
#Activate second sheet
excel.Worksheets(2).Activate()
#Autofit column in active sheet
excel.ActiveSheet.Columns.AutoFit()
#Save changes in a new file
wb.SaveAs("D:\\output_fit.xlsx")
#Or simply save changes in a current file
#wb.Save()
wb.Close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With