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)
为防止手机阅读时看错行影响理解,附代码截图: