I need to automate the following actions:
For each table, extract the contents of the first cell (“A1”) and paste just before the table.
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext
)
ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
instance = subprocess.Popen(" ".join(
[
"/usr/bin/soffice",
'--accept="socket,host=127.0.0.1,port=2002;urp;StarOffice.ServiceManager"',
"--norestore",
"--nologo",
"--nodefault",
]), shell=True)
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
uno_path = uno.systemPathToFileUrl(str(doc_path))
doc = desktop.loadComponentFromURL(uno_path, "_blank", 0, ())
xTables = doc.getTextTables()
for xTable in xTables:
oCell = xTable.getCellByPosition(0, 0)
oVC = doc.getCurrentController().getViewCursor()
oVC.gotoRange(oCell, False)
oVC.goUp(1, False)
oEnum = oVC.getText().createEnumeration()
oPrevPar = None
oNextPar = None
noteTable = False
if len(xTable.DataArray) == 1 and len(xTable.DataArray[0]) == 2 and xTable.DataArray[0][0].lower() == "note":
noteTable = True
oEnum = oVC.getText().createEnumeration()
oPrevPar = None
oPar = None
while oEnum.hasMoreElements():
oPrevPar = oPar
oPar = oEnum.nextElement()
if oPar == xTable:
oPrevPar.Text.insertTextContent(oPrevPar.Text.getEnd(), xTable.getCellByName("A1"), True) # Does not work
Please note, that I want to copy not only the cell’s text, but also all the complicated mosaic of formatting.