Solved

Issue inserting graphic into Openoffice writer document using python

Posted on 2010-11-25
4
1,008 Views
Last Modified: 2012-05-10
Here's the script I have so far:

 
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[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()
dispatcher = OOoLib.getServiceManager().createInstance('com.sun.star.frame.DispatchHelper')
dispatcher.executeDispatch(doc, ".uno:InsertGraphic", "", 0, tuple(L))

Open in new window


The error message I'm getting is:

Traceback (most recent call last):
  File "test.py", line 43, in ?
    dispatcher.executeDispatch(doc, ".uno:InsertGraphic", "", 0, tuple(L))
__main__.com.sun.star.script.CannotConvertException: value has no such interface!
0
Comment
Question by:opike
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
markoilic earned 500 total points
ID: 34213005
Hi

if you look in previous thread about this topic you will that doc in line call another method to return current controller interface

doc.getCurrentController()

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

or you can change line 21
doc=desktop.getCurrentComponent().getCurrentController()
in this case OpenOffice document (wrtier or calc) should be openerd

If you may want to open new instance of calc

instance = desktop.loadComponentFromURL('private:factory/scalc', '_blank', 0, ())
dispatcher.executeDispatch(instance.getCurrentController(), ".uno:InsertGraphic", "", 0, tuple(L))

this code will insert image in new opened call document

0
 

Author Comment

by:opike
ID: 34220785
"if you look in previous thread about this topic you will that doc in line call another method to return current controller interface

doc.getCurrentController()"

Yeah, sorry I was focused on the tuple() part and missed that you had made a change to that part too. But even when I change my code to this:

 
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[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()
dispatcher = OOoLib.getServiceManager().createInstance('com.sun.star.frame.DispatchHelper')
dispatcher.executeDispatch(doc.getCurrentController(), ".uno:InsertGraphic", "", 0, tuple(L))

Open in new window


No image is inserted.

I have a document open in Writer as I run the script.
0
 
LVL 2

Expert Comment

by:markoilic
ID: 34221450
HI,

If you run the script without errors it is goog news. Now cgek your path to the picture yuu may need to add an extre slash to the path. I will check it later on the linux because I run script on the windows

"file:///home/ollie/Desktop/Screenshot.png"
0
 

Author Comment

by:opike
ID: 34221915
Yup - it was a missing slash. That makes it tough to troubleshoot when you don't get a message saying something to the effect of "file not found".

Thanks a lot for all your help - really appreciate it.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

770 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