Solved

Issue inserting graphic into Openoffice writer document using python

Posted on 2010-11-25
4
1,023 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…

749 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