我有数百个 Word 文档,需要为其添加特定页眉(如典型的 MS Word 页眉/页脚)。并不是说需要修改标题,只是这些文档不包含标题。有没有办法使用 Python-docx 模块来做到这一点?我最近发现了它,它看起来很有希望。
如果用户没有 docx 包,也可以通过 win32 来完成,使用这个。
..//
import win32com.client
if win32com.client.gencache.is_readonly == True:
win32com.client.gencache.is_readonly = False
win32com.client.gencache.Rebuild()
from win32com.client.gencache import EnsureDispatch
from win32com.client import constants
word = win32com.client.gencache.EnsureDispatch("Word.Application")
word.Visible = False
#tell word to open the document
word.Documents.Open (IP_Directory_Dest + "\\" + name)
#open it internally
doc = word.Documents(1)
# for changing the header information of the Document
word.Visible = True
word.ActiveDocument.Sections(1).Headers(win32com.client.constants.wdHeaderFooterPrimary).Range.Text='STUFF U WANT AS UR DOCUMENT HEADER'
word.ActiveDocument.Save()
...///
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)