需求:修改指定目錄下所有文件的頁(yè)眉頁(yè)腳,或者往里面添加內(nèi)容。
1. 這里做了word的實(shí)現(xiàn)和excel的實(shí)現(xiàn),如下:
需要先安裝 pip3 install pywin32,另外頁(yè)眉頁(yè)腳格式設(shè)置可以參考:
word:
淺談Word.Application,關(guān)于js操作word文檔的使用_new word.application-CSDN博客
excel:
Python操作Excel教程(圖文教程,超詳細(xì))Python xlwings模塊詳解,_xlwings教程-CSDN博客文章來源:http://www.zghlxwxcb.cn/news/detail-759021.html
import os
import xlwings as xw
import win32com.client as win32
import pythoncom
#將需要替換頁(yè)眉頁(yè)腳的文檔放到path下
path = r'C:\Users\d\Desktop\pdf改名腳本\22\2022年\test'
#原始頁(yè)眉頁(yè)腳內(nèi)容
old_name = u'999'
#新頁(yè)眉頁(yè)腳內(nèi)容
new_name = u'888'
#替換失敗記錄日志
err_log = path + u'\\head修改出錯(cuò)列表.txt'
def log(text):
with open( err_log,"a+" ) as f:
f.write(text)
f.write('\n')
def change_headerfooter(path):
''' 更改文件的頁(yè)眉頁(yè)腳 '''
pythoncom.CoInitialize()
word = win32.Dispatch('Word.Application')
#print(dir(word))
word.Visible = 0
word.DisplayAlerts = 0
exapp = xw.App(visible=False, add_book=False)
# excel = win32.Dispatch('Excel.Application')
# excel.Visible = 0
# excel.DisplayAlerts = 0
for parent, dirnames, filenames in os.walk(path):
for fn in filenames:
filedir = os.path.join(parent, fn)
#獲取需替換目錄下的docx結(jié)尾的文檔名稱
if fn.endswith('.docx') or fn.endswith('.doc'):
print(filedir)
try:
doc = word.Documents.Open( filedir )
a = word.ActiveDocument.Sections
n = 0
for i in range( len(a) ):
#獲取當(dāng)前頁(yè)眉
head = word.ActiveDocument.Sections[i].Headers[0]
old_head = str(head)
#獲取當(dāng)前頁(yè)腳
foot = word.ActiveDocument.Sections[i].Footers[0]
old_foot = str(foot)
#print( old_head )
#if old_name in old_head:
if 1:
#old_name存在頁(yè)眉中時(shí),進(jìn)行進(jìn)行替換
#用于替換頁(yè)眉
#new_head = old_head.replace( old_name, new_name )
#用于補(bǔ)充頁(yè)眉
#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()
#頁(yè)眉重置
word.ActiveDocument.Sections[i].Headers[0].Range.Delete()
#設(shè)置字體大小
word.ActiveDocument.Sections[i].Headers[0].Range.Font.Size=20
#設(shè)置對(duì)齊方式,0-左;1-中;2-右
word.ActiveDocument.Sections[i].Headers[0].Range.ParagraphFormat.Alignment = 0
word.ActiveDocument.Sections[i].Headers[0].Range.InsertAfter(new_name)
#替換舊頁(yè)眉
#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存在頁(yè)腳中時(shí),進(jìn)行進(jìn)行替換
#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()
#頁(yè)腳重置
word.ActiveDocument.Sections[i].Footers[0].Range.Delete()
word.ActiveDocument.Sections[i].Footers[0].Range.InsertAfter(new_name)
#替換舊頁(yè)腳
#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))
if fn.endswith('.xlsx') or fn.endswith('.xls'):
print(filedir)
try:
ex = exapp.books.open(filedir)
#print(dir(ex.sheets))
sheet = ex.sheets
print(sheet[0].name)
if 'test' == sheet[0].name:
print(sheet.name)
else:
#如果第一個(gè)sheet不是test則加一個(gè)test的sheet
sheet.add('test',before=sheet[0].name)
ex.save()
ex.close()
except Exception as e:
log(str(filedir))
word.Quit()
exapp.quit()
change_headerfooter(path)
補(bǔ)充一下:excel沒有用pywin32去讀寫因?yàn)槟涿罹褪强ㄟM(jìn)程,無(wú)法對(duì)excel讀寫,可能和緩存的excel有關(guān),如要編輯3.xlsx,則會(huì)卡一個(gè)~3.xlsx的進(jìn)程無(wú)法結(jié)束。就換成了xlwings去讀寫。文章來源地址http://www.zghlxwxcb.cn/news/detail-759021.html
到了這里,關(guān)于python 學(xué)習(xí)筆記20 批量修改頁(yè)眉頁(yè)腳的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!