Reputation: 103
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.
Upvotes: 3
Views: 4718
Reputation: 189
To keep a proper word heading format.
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.
Upvotes: 0
Reputation: 919
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
Upvotes: 0
Reputation: 107
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()
...///
Upvotes: 1
Reputation: 2114
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) )
Upvotes: 0