Ask Your Question
0

UNO: access cells in Impress table

asked 2019-07-21 01:39:58 +0100

Hi-Angel gravatar image

updated 2019-07-21 02:18:13 +0100

I have slides with tables as in screenshot below. I've found a way to access slides, tables; but I have no idea how to access the actual content of these tables, e.g. to read/write text in cells. They don't provide iterators, I don't see any Rows- or Cols- providing properties… I'm completely lost here.

image description

FTR, I have this code to access slides and tables:

#!python
import uno

# run libreoffice as:
# soffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"

def connectToLO():
    # get the uno component context from the PyUNO runtime
    localContext = uno.getComponentContext()
    resolver = localContext.ServiceManager.createInstanceWithContext(
        "com.sun.star.bridge.UnoUrlResolver", localContext )
    # connect to the running office
    ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
    smgr = ctx.ServiceManager
    desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
    return desktop.CurrentComponent

model = connectToLO()
slide1 = model.DrawPages.getByIndex(0)
tables = [item for item in slide1 if item.ShapeType == 'com.sun.star.drawing.TableShape']
print(tables[0].Name)

In terminal:

$ python test.py
Table 3

How do I access cells in table now?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2019-07-21 03:59:04 +0100

Hi-Angel gravatar image

I occasionally stumbled upon this post which in turn led me to an answer: given a table, one have to use table.Model.Rows to access rows (likewise for Columns), and then getCellByPosition(0,0) to access 1st cell in the row. Modified version of the code from question:

#!python
import uno

# run libreoffice as:
# soffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"

def connectToLO():
    # get the uno component context from the PyUNO runtime
    localContext = uno.getComponentContext()
    resolver = localContext.ServiceManager.createInstanceWithContext(
        "com.sun.star.bridge.UnoUrlResolver", localContext )
    # connect to the running office
    ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
    smgr = ctx.ServiceManager
    desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
    return desktop.CurrentComponent

model = connectToLO()
slide1 = model.DrawPages.getByIndex(0)
tables = [item for item in slide1 if item.ShapeType == 'com.sun.star.drawing.TableShape']
print([s.getCellByPosition(0,0).String for s in tables[0].Model.Columns])

In terminal:

$ python test.py
['#', 'FOO', 'BAR', 'BUZZ']
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-07-21 01:39:58 +0100

Seen: 29 times

Last updated: Jul 21