吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1414|回复: 6
收起左侧

[经验求助] 求在多个excel文件批量设置页眉页脚方法

[复制链接]
hongba2008 发表于 2023-3-22 16:49
25吾爱币
在一个文件夹内有多个Excel文件,有没有什么好方法,批量把所有文件设置相同的页眉页脚,页眉页脚左右页不同,最好页眉能插入图片。用python也可以,试了很多代码,没调试成功。

最佳答案

查看完整内容

可以使用 Python 的 openpyxl 库来批量设置 Excel 文件的页眉、页脚和插入图片。 以下是一个示例代码,可以将指定文件夹下所有Excel文件的页眉设置为“左页眉”和“右页眉”,页脚设置为“页码”,并且在页眉中插入一张名为“header_image.png”的图片: import os from openpyxl import load_workbook from openpyxl.drawing.image import Image from openpyxl.worksheet.header_footer import HeaderFooter # 设置页 ...

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

jamstory 发表于 2023-3-22 16:49
可以使用 Python 的 openpyxl 库来批量设置 Excel 文件的页眉、页脚和插入图片。

以下是一个示例代码,可以将指定文件夹下所有Excel文件的页眉设置为“左页眉”和“右页眉”,页脚设置为“页码”,并且在页眉中插入一张名为“header_image.png”的图片:

import os
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
from openpyxl.worksheet.header_footer import HeaderFooter

# 设置页眉页脚
left_header = "左页眉"
right_header = "右页眉"
footer = "&P"

# 获取当前目录下所有Excel文件
dir_path = "."
excel_files = [f for f in os.listdir(dir_path) if f.endswith(".xlsx")]

# 遍历每个Excel文件
for excel_file in excel_files:
    # 打开Excel文件
    wb = load_workbook(filename=excel_file)
    # 遍历每个工作表
    for ws in wb.worksheets:
        # 设置页眉
        header = HeaderFooter()
        header.left_header.text = left_header
        header.right_header.text = right_header
        # 插入图片到页眉
        img = Image('header_image.png')
        img.width = 100
        img.height = 50
        header.left_header.add_image(img)
        # 设置页脚
        header.footer.text = footer
        ws.header_footer = header
    # 保存Excel文件
    wb.save(excel_file)
需要注意的是,该代码中需要将图片文件“header_image.png”与Python文件放在同一个文件夹内,并且需要安装相应的依赖库openpyxl。
头像被屏蔽
noplaya 发表于 2023-3-22 16:58
 楼主| hongba2008 发表于 2023-3-23 09:23
jamstory 发表于 2023-3-22 17:09
可以使用 Python 的 openpyxl 库来批量设置 Excel 文件的页眉、页脚和插入图片。

以下是一个示例代码, ...

调试没通过
 楼主| hongba2008 发表于 2023-3-23 10:43
修改了一下,可以设置页眉页脚,但不能添加图片
import os
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
from openpyxl.worksheet.header_footer import _HeaderFooterPart

left_header = "左页眉"
right_header = "右页眉"
footer = "&P"

# 获取当前目录下所有Excel文件
dir_path = "."
excel_files = [f for f in os.listdir(dir_path) if f.endswith(".xlsx")]

# 遍历每个Excel文件
for excel_file in excel_files:
    # 打开Excel文件
    print(excel_file)
    wb = load_workbook(filename=excel_file)
    # 遍历每个工作表
    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.left = _HeaderFooterPart('嘉荫县2020年第七次全国人口普查资料', size = 9, font = '方正宋三简体')
        ws.oddFooter.left = _HeaderFooterPart('— &[页码] —', size = 9, font = '方正宋三简体')
        ws.evenHeader.right = _HeaderFooterPart('第一卷  概要', size = 9, font = '方正宋三简体')
        ws.evenFooter.right = _HeaderFooterPart('— &[页码] —', size = 9, font = '方正宋三简体')





        # # 设置页眉
        # header = HeaderFooter()
        #
        #
        # # header_left.header.text = left_header
        # # header_right.header.text = right_header
        # # 插入图片到页眉
        # img1 = Image('header_image.png')
        # img1.width = 100
        # img1.height = 50
        # _HeaderFooterPart.
        #
        # _HeaderFooterPart.left_header.add_image(img1)
        # # 设置页脚
        # header.footer.text = footer
        # ws.header_footer = header
    # 保存Excel文件
    wb.save(excel_file)
freelive 发表于 2023-3-23 15:59
https://zhuanlan.zhihu.com/p/524782825
多个 Excel 批量设置页眉页脚。
返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-5-24 05:46

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表