Using Python to auto-fit all columns in an Excel worksheet

Does anyone know of any Python -> Excel libraries containing a method for automatically matching all columns in a worksheet with the name of an Excel file?

+4
source share
1 answer

You can use the 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()
+1
source

Source: https://habr.com/ru/post/1543120/


All Articles