from win32com.client import Dispatch if __name__ == '__main__': excel = Dispatch("ket.Application") # 启动wps excel 应用。Microsoft excel用Excel.Application excel.Visible = False wb = excel.Workbooks.Open(r'C:\Users\meiya\PycharmProjects\setExcelColumnHeight\系统管理-用户管理.xlsx') # 打开excel文档 for sheet in wb.Worksheets: prefix = sheet.Name.split('-')[0] if prefix == '画面元素': columnWidth = 5 elif prefix == '处理流程': columnWidth = 4 elif prefix == '信息校验': columnWidth = 10 usedRange = sheet.UsedRange # 获取sheet的已使用范围 rows = usedRange.Rows.Count # 获取已使用范围的最大行数 cols = usedRange.Columns.Count # 获取已使用范围的最大列数 for i in range(1, cols + 1): sheet.Columns(i).ColumnWidth = columnWidth wb.Save() excel.Quit()
标签:__,sheet,wb,columnWidth,excel,prefix,win32com,已用 From: https://www.cnblogs.com/zhangmeiyan/p/16627581.html