[Solved] Import python module into another python script
[Solved] Import python module into another python script
I have two python modules: TestPrograms.py and Tools.py in the directory
~/.openoffice.org/3/user/Scripts/python
I would like to use functions defined in Tools.py in TestPrograms.py, but when a function in TestProgram is launched from OOo as a script, the line "import Tools" in TestPrograms.py fails with error "No module named Tools".
I checked sys.path, and it conatins the directory "~/.openoffice.org/3/user/Scripts/python".
"import Tools" works from the python command line. Is it not possible to make it work from OOo? What am I missing?
Thanks, Nick
~/.openoffice.org/3/user/Scripts/python
I would like to use functions defined in Tools.py in TestPrograms.py, but when a function in TestProgram is launched from OOo as a script, the line "import Tools" in TestPrograms.py fails with error "No module named Tools".
I checked sys.path, and it conatins the directory "~/.openoffice.org/3/user/Scripts/python".
"import Tools" works from the python command line. Is it not possible to make it work from OOo? What am I missing?
Thanks, Nick
Last edited by gurfle on Thu Feb 25, 2010 8:06 am, edited 1 time in total.
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
AntiX M11
LibreOffice 3.4.3
Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
AntiX M11
LibreOffice 3.4.3
Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
Re: Cannot Import python module into OOo-launched python script
I find the same problem, though I suspect it is a Path problem somehow. If I put the module I want to import in the C:\Program Files\OpenOffice.org 3\program directory, the import works as desired from within OOo. I just can't get it to work from the ~\Scripts\python folder. I know that doesn't help much other than to show it can be done within OOo. I'll try to take another look at it tonight (~12 hours from now, my time).
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Cannot Import python module into OOo-launched python script
I fixed it by modifying the sys.path within the first module called by OOo. I made a little dummy module called mod_import_test and defined a function within that called Newfunc. I can call Newfunc with this code
Code: Select all
import uno
import sys
sys.path.append('C:\\Documents and Settings\\username\\Application Data\\StarOffice\\9\\user\\Scripts\\python')
from mod_import_test import Newfunc
def Main():
doc = XSCRIPTCONTEXT.getDocument()
Newfunc(doc)
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Cannot Import python module into OOo-launched python script
My setup is Debian Linux, so the paths are different, and probably quite a bit more behind-the-scenes. In fact I had already unsuccessfully attempted messing with sys.path. Prompted by your success, however, I checked the approach out again, and got a little further, but not quite where I hoped to be:
Tools.py is located in the default OOo python script folder /home/nick/.openoffice.org/3/user/Scripts/python
It contains:
Annex.py contains:
The import of Tools now works after appending the path (my trouble last time was that I was trying to use the "~" short-cut for the user home path "/nome/nick" which apparently does not work). I notice however that the appended path remains in sys.path after the first time running TestgetData, so I am leary of using this approach to the path since it could blow up on me if I call it too many times. There must be a better way to append to sys.path, possibly through some environment variable.
Now I have this problem though: Python bombs on the first line of BasciFunc because it does not recognize XSCRIPTCONTEXT.
Is it possible to get a python module imported from an OOo-invoked python module to recognize XSCRIPTCONTEXT, or is that expecting too much?
Thanks again FJCC Your help on python is invaluable, and has gotten me to appreciate a fascinating new language. If I can get it to integrate a bit better with OOo, I'll be extremely impressed
Tools.py is located in the default OOo python script folder /home/nick/.openoffice.org/3/user/Scripts/python
It contains:
Code: Select all
import uno
import random
from com.sun.star.script.provider import XScriptProviderFactory
from com.sun.star.script.provider import XScriptProvider
# from numpy import *
def Xray(myObject):
# Taken from http://www.oooforum.org/forum/viewtopic.phtml?t=23577
xCompCont = XSCRIPTCONTEXT.getComponentContext()
sm = xCompCont.ServiceManager
mspf = sm.createInstance("com.sun.star.script.provider.MasterScriptProviderFactory")
scriptPro = mspf.createScriptProvider("")
Xscript = scriptPro.getScript("vnd.sun.star.script:XrayTool._Main.Xray?language=Basic&location=application")
Xscript.invoke((myObject,), None, None)
def BasicFunc(funcname,*args):
xCompCont = XSCRIPTCONTEXT.getComponentContext()
sm = xCompCont.ServiceManager
mspf = sm.createInstance("com.sun.star.script.provider.MasterScriptProviderFactory")
scriptPro = mspf.createScriptProvider("");
Xscript = scriptPro.getScript("vnd.sun.star.script:Standard.Tools." + funcname + "?language=Basic&location=application")
Result=Xscript.invoke(args,None,None)
return Result[0]
Code: Select all
import uno
import sys
sys.path.append("/home/nick/.openoffice.org/3/user/Scripts/python") # Using ~ for home directory does not work!
from Tools import *
def TestgetData():
Xray(BasicFunc("OOWorkBooks","Annex.ods"))
Now I have this problem though: Python bombs on the first line of BasciFunc because it does not recognize XSCRIPTCONTEXT.
Is it possible to get a python module imported from an OOo-invoked python module to recognize XSCRIPTCONTEXT, or is that expecting too much?
Thanks again FJCC Your help on python is invaluable, and has gotten me to appreciate a fascinating new language. If I can get it to integrate a bit better with OOo, I'll be extremely impressed
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
AntiX M11
LibreOffice 3.4.3
Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
AntiX M11
LibreOffice 3.4.3
Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
Re: Cannot Import python module into OOo-launched python script
I ran into the same problem, which is why my example passes the document as a parameter. I'm out of my depth on this, but I have copied python code with this method of getting the current context and servicemanager
Does that work for you? I tried it in my code, using the servicemanager to get the desktop and then the currentcomponent, and it worked.
Code: Select all
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Cannot Import python module into OOo-launched python script
Yes, thanks so much : uno.getComponentContext() can be used in place of XSCRIPTCONTEXT.getComponentContext() and allows me to call the imported Xray and BasicFunc functions shown above.FJCC wrote:Does that work for you? I tried it in my code, using the servicemanager to get the desktop and then the currentcomponent, and it worked.Code: Select all
ctx = uno.getComponentContext() smgr = ctx.ServiceManager
However, I am clearly more over my head than you, FJCC, in all of this:
Could you explain how to use the servicemanager to get the desktop or current component? Scanning properties and methods of uno.getComponentContext().ServiceManager leaves me clueless . With what you have helped me uncover so far, I know I can make some pretty good use of python functions, but it sure would be a lot handier for what I have in mind if the desktop and current component could be discovered without having to pass them as arguments.
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
AntiX M11
LibreOffice 3.4.3
Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
AntiX M11
LibreOffice 3.4.3
Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
Re: Cannot Import python module into OOo-launched python script
A google on "getcomponentcontext().servicemanager" retrieved one solution at:gurfle wrote:Could you explain how to use the servicemanager to get the desktop or current component?
http://www.lucasmanual.com/mywiki/OpenOffice
If I start Open Office with command line
soffice -accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"
then
Code: Select all
def TestgetData():
local = uno.getComponentContext()
resolver = local.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local)
context = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
document = desktop.getCurrentComponent()
Xray(document)
Was your way less quirky, FJCC?
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
AntiX M11
LibreOffice 3.4.3
Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
AntiX M11
LibreOffice 3.4.3
Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
Re: Cannot Import python module into OOo-launched python script
This is what I was referring to
By the way, though I agree there must be a better way of appending a new element to sys.path, you could check whether sys.path contains the directory you need and only append if it doesn't have it.
Code: Select all
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
oDesktop = smgr.createInstanceWithContext( 'com.sun.star.frame.Desktop',ctx)
oDoc = oDesktop.getCurrentComponent()
Code: Select all
if not "/home/nick/.openoffice.org/3/user/Scripts/python" in sys.path:
sys.path.append("/home/nick/.openoffice.org/3/user/Scripts/python")
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Cannot Import python module into OOo-launched python script
Much less "quirky" than what I found !!!FJCC wrote:This is what I was referring toCode: Select all
ctx = uno.getComponentContext() smgr = ctx.ServiceManager oDesktop = smgr.createInstanceWithContext( 'com.sun.star.frame.Desktop',ctx) oDoc = oDesktop.getCurrentComponent()
Is there a more practical way to learn the uno API than through laborious (and often misleading) forum/google searches or having the good fortune of finding such helpfull forum volunteers as yourself? Can you suggest a good reference (other than http://wiki.services.openoffice.org/wik ... pers_Guide)? A nice concicse book perhaps? I generally prefer good reference books a lot better than most online resources - although I have to say the amount of assistance you yourself have just provided in getting me going with both OOo and python beats almost anything I've experienced anywhere online !!
That certainly works. I also found that setting the environment variableFJCC wrote:By the way, though I agree there must be a better way of appending a new element to sys.path, you could check whether sys.path contains the directory you need and only append if it doesn't have it.
Code: Select all
PYTHONPATH="/home/nick/.openoffice.org/3/user/Scripts/python"
Thanks again, FJCC, for all your help
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
AntiX M11
LibreOffice 3.4.3
Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
AntiX M11
LibreOffice 3.4.3
Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
Re: SOLVED:Can't Import python module into another python script
Andrew Pitonyak has a book and a web page with a lengthy document on Macros. You can find both at this web page http://www.pitonyak.org/oo.php
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: [Solved] Import python module into another python script
I describe about import feature of python working in the OOo instance for readers of this thread.
There are two kind of situation that python script is loaded from your file system.
1. a script file implements some UNO component that is loaded by the PythonLoader service. Here is the explanation for this situation: http://udk.openoffice.org/python/python ... urce_files
2. a script file describes macro and it is loaded by the ScriptProviderForPython. You can use the same way for 1.
There are two kind of situation that python script is loaded from your file system.
1. a script file implements some UNO component that is loaded by the PythonLoader service. Here is the explanation for this situation: http://udk.openoffice.org/python/python ... urce_files
2. a script file describes macro and it is loaded by the ScriptProviderForPython. You can use the same way for 1.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
Apache OpenOffice 4-dev on Xubuntu 14.04
Re: [Solved] Import python module into another python script
Thanks, your site is extremely helpful! For now I think I have the basics down enough to get really going with python programing for OOo, after having gotten a better understanding of another unexpected twist in the use of module import and reload from OOo (see: http://user.services.openoffice.org/en/ ... 07#p127769)hanya wrote:I describe about import feature of python working in the OOo instance for readers of this thread.
Again, thanks for all your great contributions to this question. I can't say how much they have helped me get a handle on writing successful python functions for OOo.
Nick
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
AntiX M11
LibreOffice 3.4.3
Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
AntiX M11
LibreOffice 3.4.3
Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1