脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python批量设置多个Excel文件页眉页脚的脚本

python批量设置多个Excel文件页眉页脚的脚本

2021-01-22 00:08董付国 Python

这篇文章主要介绍了python批量设置多个Excel文件页眉页脚的源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python批量设置多个Excel文件页眉页脚的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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小屋

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/dongfuguo/article/details/78291813

延伸 · 阅读

精彩推荐