網(wǎng)上搜這個問題,沒有現(xiàn)成的答案,自己找了下,把代碼記下
注:
我這里是修改的doc格式的word,不是docx
參考網(wǎng)址:Header and Footer — python-docx 0.8.9 documentation
python自動化辦公:玩轉(zhuǎn)word之頁眉頁腳秘笈 - 云+社區(qū) - 騰訊云
#coding=utf8
import os
import docx
nowpath='需要修改的文件的路徑'
topath='另存到的路徑'
os.chdir(nowpath)
for iin os.listdir('.'):
print(i)
if os.path.splitext(i)[1]== '.doc':
try:
file=docx.Document(i)
file.sections[0].header.paragraphs[0].text= '你要修改成的頁眉'
file.sections[0].footer.paragraphs[0].text= '你要修改成的頁腳'
file.save(os.path.join(topath,i))
except Exception as e:
print(e)
continue
遇見的錯誤:
file 'xxxx.doc' is not a Word file, content type is 'application/vnd.openxmlformats-officedocument.themeManager+xml'
不是word格式的文件不能轉(zhuǎn)
我的博客即將同步至騰訊云+社區(qū)侨舆,邀請大家一同入駐:https://cloud.tencent.com/developer/support-plan?invite_code=3125dlqjnk8wc