gelonida
asked on
Open Office Text and python: extract some cells from each table in document
I'd like to locate all tables in an open office text document and then extract the contents of some cells of these tables.
Extracting the cells is probably rather easy as soon as I solved my main problem:
extracting all tables.
Please look at my not working code snippet.
Extracting the cells is probably rather easy as soon as I solved my main problem:
extracting all tables.
Please look at my not working code snippet.
def mymacro_text_tables():
info = []
doc = XSCRIPTCONTEXT.getDocument()
text = doc.Text
fh = open("result.txt","a")
#well here I'm lost.
# how to get all tables in a document
tables = ??????
table_enum = tables.createEnumeration() # this line is guessed
cnt = 0
while table_enum.hasMoreElements(): # this line is guessed
cnt += 1
table = table_enum.nextElement() # this line is guessed
for cell_name in ["A1","B1","A2"]:
cell = table.getCellByName(cell_name)
do_something_with_cell(cell)
fh.write( "found %d tables\n" % cnt )
fh.close()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
your example contained exactly the API calls, that I was missing
ASKER
Thanks a lot for your answer:
What helped me was
- line 42: tables = doc.TextTables # I still don't know where to find all properties in the doc
- line 43: tablenames = tables.getElementNames() # No idea, why createEnumeration() fails
- line 46: cellnames = table.getCellNames() # I'd expected to use something like getCells, but this is fine
I still have to learn how to faster find out which functions, properties a given object has.
The doc at http://api.openoffice.org/docs/common/ref/com/sun/star/text/module-ix.html
is not really optimal for learning, as one has to descend into every interface in order to find out what one can do with a given object.
In general I think I still have to l