import uno import string """ You must launch openoffice/staroffice like this on the command line: soffice "-accept=socket,host=localhost,port=2002;urp;" Then run the script. It connects to the running instance, and highlights each letter individually. """ # get the uno component context from the PyUNO runtime localContext = uno.getComponentContext() # create the UnoUrlResolver 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 # get the central desktop object desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx) # access the current writer document model = desktop.getCurrentComponent() # access the document's text property text = model.Text # create a cursor cursor = text.createTextCursor() colorDict = { 'a' : 0x4c4c4c , 'b' : 0x23ff23, 'c' : 0x00ffff, 'd' : 0xff0000, 'e' : 0x9999ff, 'f' : 0xffffff, 'g' : 0xff6633, 'h' : 0xff00ff, 'i' : 0xffff66, 'j' : 0x000000, 'k' : 0x280099, 'l' : 0x008000, 'm' : 0x00cccc, 'n' : 0x198a8a, 'o' : 0x999999, 'p' : 0xc5000b, 'q' : 0x4b1f6f, 'r' : 0xff00ff, 's' : 0x804c19, 't' : 0x7e0021, 'u' : 0x94bd5e, 'v' : 0xb3b300, 'w' : 0x99284c, 'x' : 0x0000ff, 'y' : 0x004a4a, 'z' : 0xffff00 } while cursor.goRight(1,1): target = cursor.getString(); if ( ord( target ) > 96 and ord( target ) < 123 ) or ( ord( target ) > 64 and ord( target ) < 91 ): cursor.CharBackColor = colorDict[ string.lower(target) ] cursor.goRight(0,0) # insert the text into the document #text.insertString( cursor, "Hello World", 0 ) # Do a nasty thing before exiting the python process. In case the # last call is a oneway call (e.g. see idl-spec of insertString), # it must be forced out of the remote-bridge caches before python # exits the process. Otherwise, the oneway call may or may not reach # the target object. # I do this here by calling a cheap synchronous call (getPropertyValue). ctx.ServiceManager