Link to home
Start Free TrialLog in
Avatar of opike
opike

asked on

Attempting to insert image at end of openoffice writer doc

My goal is to have a script that will insert text and an image at the end of a Writer doc. The attached code works fine on an empty doc, but when I run it a second time on the same document (after it contains text and an image), I get the exception

"  File "test.py", line 33, in ?
    oViewCursor.gotoEnd(False)
uno.com.sun.star.uno.RuntimeException: no text selection"


 
import sys
import Danny.OOo.OOoLib as OOoLib
import unohelper
import os.path
import glob
from com.sun.star.beans import PropertyValue
from array import array


L =  [ PropertyValue() for i in range(4)]


L[0].Name="FileName"
#L[0].Value="file:///home/ollie/Desktop/Screenshot.png"
L[0].Value="file://" + sys.argv[1]
print L[0].Value
L[1].Name="FilterName"
L[1].Value="<All formats>"
L[2].Name = "AsLink"
L[2].Value = False
L[3].Name="Style"
L[3].Value="Graphics"

desktop = OOoLib.getDesktop()
doc=desktop.getCurrentComponent()

oText = doc.getText()
xEnd = oText.getEnd()
xEnd.setString("This is some text.\n")


oViewCursor = doc.CurrentController.ViewCursor
oViewCursor.gotoEnd(False)


dispatcher = OOoLib.getServiceManager().createInstance('com.sun.star.frame.DispatchHelper') 
dispatcher.executeDispatch(doc.getCurrentController(), ".uno:InsertGraphic", "", 0, tuple(L))


#dispatcher.executeDispatch(doc.getCurrentController().getFrame(),'.uno:InsertGraphic','',0,tuple(L))

Open in new window

SOLUTION
Avatar of Mytix
Mytix

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of opike
opike

ASKER

Gives me the same error on the gotoStart(True) call:

Traceback (most recent call last):
  File "test.py", line 33, in ?
    oViewCursor.gotoStart(True)
uno.com.sun.star.uno.RuntimeException: no text selection
Avatar of opike

ASKER

I've figured out that the error occurs the 2nd time because after the first run, the script leaves the image as being selected in the document and the gotoEnd and gotoStart functions must be working on the current selection and not the entire document.

I'm now researching on how to fix this...
Avatar of opike

ASKER

I found this method:
doc.CurrentController.select(<object>)

But it requires that another object is specified to set the selection to, where here I'd like to unselect everything. There doesn't appear to be a way to do that.
Wasn't there a getEmpty() function?
Avatar of opike

ASKER

You'll have to help me out with some more specifics....

I didn't see a getEmpty() function in the api documentation:
http://api.openoffice.org/docs/common/ref/index-files/index-1.html
Avatar of opike

ASKER

The trick of recording a macro to see what code gets created by Writer isn't helpful here since it appears that the macro recorder doesn't record selection events.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of opike

ASKER

Figured out the solution.