Solved

Issue inserting graphic into Openoffice writer document using python

Posted on 2010-11-25
4
991 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 58
bigHeights  challenge 13 56
Python 2.7 - Passing arguments 8 48
How can i compile this github project?? 2 44
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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 …

746 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

10 Experts available now in Live!

Get 1:1 Help Now