Skip to content
Advertisement

How do I extract tables from word via table titles?

I’m facing the problem of trying to extract data from word files in the form of tables. I have to iterate through 500 word files and extract a specific table in each file, but the table appears at a different point in each word file. This is the code I have:

JavaScript

Which goes through all the files fine, but gets an error as some of the word documents do not have the table it looks for, as it looks only for an element: wordDoc.tables[8] so an IndexError appears. I want to be able to change it from this, to instead look for a table with certain column titles: CONTACT NAME POSITION LOCATION EMAIL TELEPHONE ASSET CLASS

Is there a way that I can modify the code shown to be able to find the tables I’m looking for?

Many thanks.

Advertisement

Answer

Instead of changing the logic to look up tables with certain column names you can catch the index error and ignore it. This will enable you to continue without error when that table is not present in a document. This is done using try and except.

JavaScript

Also, note that it is better to use os.path.join() when combining paths instead of concatenating the path strings.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement