Read word tables

R

Rameshwari

Hi,

I would like to read a ms-word document using python.

Basically the word document contains number of tables and the rows
in each table do not have same number of columns.

Does anyone have a sample code to read a table?

Thank you
Best Regards,
Rameshwari
 
T

Thomas Guettler

Am Tue, 21 Dec 2004 11:24:35 +0000 schrieb Rameshwari:
Hi,

I would like to read a ms-word document using python.

Basically the word document contains number of tables and the rows
in each table do not have same number of columns.

Does anyone have a sample code to read a table?

Hi,

There is a small script[1] which parses the XML produced
by Excel. Something like this should be possible for msword, too.

Other way: You can export to the openoffice format and unzip it.
The read these xml files.

HTH,
Thomas

[1]:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/192914
 
A

Andrew Henshaw

Rameshwari said:
Hi,

I would like to read a ms-word document using python.

Basically the word document contains number of tables and the rows
in each table do not have same number of columns.

Does anyone have a sample code to read a table?

Thank you
Best Regards,
Rameshwari

The following code should return a list of list of lists
(tables->table->rows->cells) for the active document in
Microsoft Word.

Warning! Untested code

########################
import win32com.client

def GetTables():
app = win32com.client.Dispatch('Word.Application')
doc = app.Documents[0]
tables = []
for word_table in doc.Tables:
table = []
for word_row in word_table.Rows:
row = [cell.Range.Text for cell in word_row.Cells]
table.append(row)
tables.append(table)
return tables
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top