python 学习笔记20 批量修改页眉页脚

2023-12-13 04:10:31

需求:修改指定目录下所有文件的页眉页脚,或者往里面添加内容。

1. 这里先做了word的实现,如下:

需要先安装 pip3 install pywin32,另外页眉页脚格式设置可以参考:

浅谈Word.Application,关于js操作word文档的使用_new word.application-CSDN博客

import os
#pip3 install pywin32
import win32com.client as win32
import pythoncom

#将需要替换页眉页脚的文档放到path下
path = r'C:\Users\d\Desktop\pdf改名脚本\22\2022年\test'
#原始页眉页脚内容
old_name = u'999'
#新页眉页脚内容
new_name = u'888'
#替换失败记录日志
err_log = path + u'\\head修改出错列表.txt'
def log(text):
    with open( err_log,"a+" ) as f:
        f.write(text)
        f.write('\n')

def change_headerfooter(path):
    ''' 更改文件的页眉页脚 '''
    pythoncom.CoInitialize()
    word = win32.Dispatch('Word.Application')
    word.Visible = 0
    word.DisplayAlerts = 0
    for parent, dirnames, filenames in os.walk(path):
        for fn in filenames:
            filedir = os.path.join(parent, fn)
            #获取需替换目录下的docx结尾的文档名称
            if fn.endswith('.docx'):
                print(filedir)
                try:
                    doc = word.Documents.Open( filedir )
                    a = word.ActiveDocument.Sections
                    n = 0
                    for i in range( len(a) ):
                    	#获取当前页眉
                        head = word.ActiveDocument.Sections[i].Headers[0]
                        old_head = str(head)
                        #获取当前页脚
                        foot = word.ActiveDocument.Sections[i].Footers[0]
                        old_foot = str(foot)
                        #print( old_head )
                        #if old_name in old_head:
                        if 1:
                        	#old_name存在页眉中时,进行进行替换
                            #用于替换页眉
                            #new_head = old_head.replace( old_name, new_name )
                            #用于补充页眉
                            #new_head = old_head + new_name
                            #print( new_head )
                            word.ActiveDocument.Sections[i].Headers[0].Range.Find.ClearFormatting()
                            word.ActiveDocument.Sections[i].Headers[0].Range.Find.Replacement.ClearFormatting()
                            #页眉重置
                            word.ActiveDocument.Sections[i].Headers[0].Range.Delete()
                            #设置字体大小
                            word.ActiveDocument.Sections[i].Headers[0].Range.Font.Size=20
                            #设置对齐方式,0-左;1-中;2-右
                            word.ActiveDocument.Sections[i].Headers[0].Range.ParagraphFormat.Alignment = 0
                            
                            word.ActiveDocument.Sections[i].Headers[0].Range.InsertAfter(new_name)
                            #替换旧页眉
                            #word.ActiveDocument.Sections[i].Headers[0].Range.Find.Execute( old_head, False, False, False, False, False, False, 1, False, new_name, 2 )

                        #if old_name in old_foot:
                        if 1:
                        	##old_name存在页脚中时,进行进行替换
                            #new_foot = old_foot.replace( old_name, new_name )
                            #new_foot = old_foot + new_name
                            word.ActiveDocument.Sections[i].Footers[0].Range.Find.ClearFormatting()
                            word.ActiveDocument.Sections[i].Footers[0].Range.Find.Replacement.ClearFormatting()
                            #页脚重置
                            word.ActiveDocument.Sections[i].Footers[0].Range.Delete()
                            word.ActiveDocument.Sections[i].Footers[0].Range.InsertAfter(new_name)
                            #替换旧页脚
                            #word.ActiveDocument.Sections[i].Footers[0].Range.Find.Execute( old_foot, False, False, False, False, False, False, 1, False, new_name, 2 )
                        n = n+1
                    doc.Close()
                except Exception as e:
                    print(e)
                    log(str(filedir))

change_headerfooter(path)

文章来源:https://blog.csdn.net/wzhang1987/article/details/134881839
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。