import os import openpyxl src_dir = r'C:\Users\PC\Desktop\目录' tgt_dir = src_dir + '_页脚添加文件名' os.makedirs(tgt_dir, exist_ok=True) count = 0 for name in os.listdir(src_dir): if os.path.splitext(name)[1] in ('.xls', '.xlsx'): src_path = os.path.join(src_dir, name) print('正在处理', src_path) wb = openpyxl.open(src_path) wb.active.oddFooter.right.text = '&F' wb.save(os.path.join(tgt_dir, name)) count += 1 print(count, '个文件已保存至', tgt_dir)