Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Issue inserting graphic into Openoffice writer document using python

Posted on 2010-11-25
4
Medium Priority
?
1,056 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

972 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