时间:2022-08-05 11:44:28 | 栏目:Python代码 | 点击:次
本文实例为大家分享了python批量设置多个Excel文件页眉页脚的具体代码,供大家参考,具体内容如下
import os import openpyxl from openpyxl.worksheet.header_footer import _HeaderFooterPart xlsxFiles = (fn for fn in os.listdir('.') if fn.endswith('.xlsx')) for xlsxFile in xlsxFiles: wb = openpyxl.load_workbook(xlsxFile) for ws in wb.worksheets: # 设置首页与其他页不同 ws.HeaderFooter.differentFirst = True # 设置奇偶页不同 ws.HeaderFooter.differentOddEven = True # 设置首页页眉页脚 ws.firstHeader.left = _HeaderFooterPart('第一页左页眉', size=24, color='FF0000') ws.firstFooter.center = _HeaderFooterPart('第一页中页脚', size=24, color='00FF00') # 设置奇偶页页眉页脚 ws.oddHeader.right = _HeaderFooterPart('奇数页右页眉') ws.oddFooter.center = _HeaderFooterPart('奇数页中页脚') ws.evenHeader.left = _HeaderFooterPart('偶数页左页眉') ws.evenFooter.center = _HeaderFooterPart('偶数页中页脚') wb.save('new_'+xlsxFile)
来源:python小屋