Skip to content Skip to sidebar Skip to footer

Adding A Header To Docx File With Python

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 doc

Solution 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()

...///

Solution 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)  )   

Solution 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

Solution 4:

To keep a proper word heading format.

  1. create a template docx file with header/footer
  2. insert placeholders (i.e. #headerText) through Word into the docx (group the text)
  3. expand the xml tree of the template
  4. replace the placeholders with your desired text
  5. 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.

Post a Comment for "Adding A Header To Docx File With Python"