import openpyxl
import os
pth = r'\kevin_folder\Python\excel'
#读取此目录下的所有文件
for file in os.listdir(pth):
# 判断文件后缀是否以xlsx结尾
if file.endswith('.xlsx'):
# 拼接固定路径和所有子文件名
sourcefile = os.path.join(pth,file)
# 打印文件绝对路径
print(sourcefile)
# 加载excel文件
wb = openpyxl.load_workbook(sourcefile)
# 读取该excel中所有的sheets
sheets = wb.sheetnames
# 遍历每一个sheet页并显示名称
for sheet in sheets:
# print(sheet)
# 所有的worksheeet相同的行增加相同的行高
wb[sheet].row_dimensions[2].height = 20
# 所有的worksheet相同的列增加相同的列宽
wb[sheet].column_dimensions['C'].width = 50
# 增加列宽
print(sheet)
# 重新名命
newname = 'dimTest.xlsx'
# 确定保存绝对路径
newfilename = os.path.join(pth,newname)
wb.save(newfilename)