问题
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.
回答1:
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()
...///
回答2:
Pretty simple.
from docx import *
document = yourdocument.docx
docbody = document.xpath('/w:document/w:body',namespaces=wordnamespaces)[0]
docbody.append(heading('Your header text',1) )
回答3:
Well if I understood, you need to create a header section in many docx files. As far as I concern people are working on python-docx to implement this. While this new feature is not available, you might directly add this to your docx file.
In case you don't know yet, docx files can be unzipped. Inside it's structure there are some header.xml files.
One suggestions is, create a docx file with the header, and then, using lxml and zipfile modules, you may simple update the header.xml file in all your docx files.
If you thing this could apply to help you solve you problem, let me know and i might guide you through.
Regards
回答4:
To keep a proper word heading format.
- create a template docx file with header/footer
- insert placeholders (i.e. #headerText) through Word into the docx (group the text)
- expand the xml tree of the template
- replace the placeholders with your desired text
- output new word document
This is not an ideal solution to edit docx, but it mostly solves my python docx text insertion needs.
Eventually the python docx might add more features to help edit headers/footers.
来源:https://stackoverflow.com/questions/11656036/adding-a-header-to-docx-file-with-python