使用 Python 自动调整 Excel 工作表的所有列

Sha*_*mar 6 python excel

有没有人知道任何 Python --> Excel 库,其中包含一种在给定 Excel 文件名的情况下自动调整工作表中所有列的方法?

Nor*_*Cat 10

您可以使用pywin32库:

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()
Run Code Online (Sandbox Code Playgroud)