Solved

Attempting to insert image at end of openoffice writer doc

Posted on 2010-11-27
9
1,252 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:opike
  • 7
  • 2
9 Comments
 
LVL 3

Assisted Solution

by:Mytix
Mytix earned 500 total points
ID: 34227816
Does replacing this line:
oViewCursor = doc.CurrentController.ViewCursor
oViewCursor.gotoEnd(False)

Open in new window

With this:
oViewCursor = doc.CurrentController.ViewCursor
oViewCursor.gotoStart(True)
oViewCursor.gotoEnd(False)

Open in new window

Work? I think it needs to be forced or something

0
 

Author Comment

by:opike
ID: 34240176
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
0
 

Author Comment

by:opike
ID: 34240403
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...
0
 

Author Comment

by:opike
ID: 34240794
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:Mytix
ID: 34244991
Wasn't there a getEmpty() function?
0
 

Author Comment

by:opike
ID: 34249162
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
0
 

Author Comment

by:opike
ID: 34257473
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.
0
 

Accepted Solution

by:
opike earned 0 total points
ID: 34268295
Got this working... I added the following line to the end of the script to deselect the image. Then subsequent executions of the script ran fine.

dispatcher.executeDispatch(doc.getCurrentController(), ".uno:Escape", "", 0, tuple())
0
 

Author Closing Comment

by:opike
ID: 34299402
Figured out the solution.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now