Varun
Varun

Reputation: 107

Python : Win32 : Get the header Information in Word Document

I'm trying to automate Microsoft office word 2010 document using Python with win32 component. Over there I need to read and get the Header information of a WORD document. I had a look at MSDN library (and thought that getFieldNames could help me) and even had a look @Editing MS Word header with win32com (using this I can only edit the header information), but none of it worked for me.

My Code snippet is :

..//

#tell word to open the document
word.Documents.Open (IP_Directory_Dest + "\\" + name)

#open it internally
doc = word.Documents(1)

### get the header of file - using this gives me Attribute Error
## header = doc.getFieldNames() 
## print ('Header = ', header)

..//

Moreover when I'm using getFieldNames(), it gives an error "AttributeError: '' object has no attribute 'getFieldNames'", which make me believe that there is no such attribute like getFieldNames in object lib.

Any suggestions on this are most welcome.

Upvotes: 1

Views: 3088

Answers (1)

Varun
Varun

Reputation: 107

Got it. It should be something like this :

...///

#tell word to open the document
word.Documents.Open (IP_Directory_Dest + "\\" + copied_IR_file)

# store the header information
header_string = word.ActiveDocument.Sections(1).Headers(win32com.client.constants.wdHeaderFooterPrimary).Range.Text

...///

Upvotes: 2

Related Questions