2

This is my extended question for the question:

How to read contents of an Table in MS-Word file Using Python?

The solution provided by @YusuMishi is great, but it does not catch the headers in the header and footer.

Let me elaborate on that: enter image description here

Using the code

import win32com.client as win32
import os
word = win32.Dispatch("Word.Application")
word.Visible = 0
p = os.path.abspath("Catch my tables.docx")
word.Documents.Open(p)
doc = word.ActiveDocument
print doc.Tables.Count

I will get 2 printed (Table 1 and Table 2)

How do I go through the information in Table 0 and Table N

Get the document here

2 Answers 2

4

Accessing Headers and Footers is a bit tricky. Here is how to do it:

HeaderTable = doc.Sections(1).Headers(1).Range.Tables(1)
FooterTable = doc.Sections(1).Footers(1).Range.Tables(1)

You can get the table count this way:

HeaderTablesCount = doc.Sections(1).Headers(1).Range.Tables.Count
FooterTablesCount = doc.Sections(1).Footers(1).Range.Tables.Count

And get the text from cells this way:

HeaderTable.Cell(1,1).Range.Text
FooterTable.Cell(1,1).Range.Text
Sign up to request clarification or add additional context in comments.

2 Comments

BTW: what is the best way to cut of the u' and \r\x07 ?
Check my answer to this question. stackoverflow.com/questions/10423593/…
0

Unfortunately I'm not programmer but have some knowledge of MS-Word VBA and objects hierarchy. This would be to much text to put in comment (where I'd rather put this tip).

If you search your table you have to analyse different Document.StoryRanges to find your table. There are Footers and Headers but they are additionally divided into different types. So, to find you Table 0 you could use something of this structure:

This is VBA code!! I hope you could adjust to your needs. And do it separately for you footers.

doc.StoryRanges(wdEvenPagesHeaderStory).Tables.Count
doc.StoryRanges(wdFirstPageHeaderStory).Tables.Count
doc.StoryRanges(wdPrimaryHeaderStory).Tables.Count

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.