使用python将标头添加到docx文件 [英] Adding a header to docx file with python
本文介绍了使用python将标头添加到docx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有数百个Word文档,需要为其添加特定的页眉(如在典型的MS Word页眉/页脚中一样).不是标题需要修改,这些文档只是不包含一个.有什么方法可以使用Python-docx模块执行此操作吗?我最近发现了它,而且看起来很有希望.
I have several hundred word documents for which I need to add a specific header (as in a typical MS Word header/ footer). It is not that the header needs to be modified, these documents just don't contain one. Is there any way to do this with the Python-docx module? I recently discovered it and it seems promising.
推荐答案
如果用户没有docx软件包,也可以通过win32使用此软件包来完成.
And if the user doesn't have docx package, it can be done through win32 too, using this.
..//
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()
...///
这篇关于使用python将标头添加到docx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文