Reference Link: Http://www.openoffice.org/udk/python/python-bridge.htmlPyUNO can be used in the following three modes: 1. In the LO process within the script framework (after the OOo2.0 version)/HTTP/ Www.openoffice.org/udk/python/scriptingframework/index.html2. In python execution (outside of the LO process), you might consider using this pattern:
- Just started using Pyuno (because this is a more intuitive approach)
- You want to invoke a Python script from a separate process (e.g. a cgi-script within a http-server)
- Want the shortest turnaround time (Code-execute-code-execute ... )
Call Pattern:
- Start the Soffice.bin process Soffice "-ACCEPT=SOCKET,HOST=LOCALHOST,PORT=2002;URP;"
- Python script execution, interprocess communication with the soffice.bin process CTX = Resolver.resolve ("UNO:SOCKET,HOST=LOCALHOST,PORT=2002;URP; Staroffice.componentcontext ")
3. You may consider using this mode for the following scenarios within the LO process:
- You want to easily run your code to multiple other machines (using UNO packages)
- Script will be triggered by UI events (menu or toolbars)
- You already have the experience of using Pyuno.
- You want your script to run the best performance
The pattern is to write the Py script into the form of a UNO component and package it in the ODT for LO use, for example, to see the top reference link. Uno language binding Uno type mapping
IDL type |
Python |
Integer types (byte, short, unsigned short, long, unsigned long, hyper, unsigned hyper |
Python internally only knows the C data type long and Longlong as integer types. On most machines, a long is a 32-bit value and Longlong is a 64-bit value.
- Values from Uno (for example, the return value of a Uno method)
byte,short,unsigned Short,long or unsigned long converts to a python long. Type hyper or unsigned hyper convert to Python long long.
- Value passed to Uno (for example, as a parameter to a Uno method)
If there is a specific type of IDL method, the value is converted to a specific type (actually invoking the service does this work). If the method has only one any, each integer value is converted to the smallest data type (5 becomes a byte, becomes a short, 0x1f023 becomes a long and values larger than 0XFFFFFF FF become a Hyper)
|
boolean |
python inside there is a Boolean data type, inherit integer type there is a single true and false, Pyuno uses distinguished integers and Boolean values. If the parameter of a Uno method is Boolean, you can also use numeric delivery For example: #idl signature void Takebool ( [In] boolean bool) Unoobject.takebool (1) # Valid, passing True (Pyuno runtime # does The Conversionunoobject.takebool (True)) # Valid, passing Trueunoobject.takebool (False) # valid, passing False However when you want to explicitly specifies that a Boolean is passed, and any type is used as the argument, you must use TRUE or False # idl signature void foo ([in] any value) # Impl Ementation expects a Boolean (which is separately documented# e.g. in the service Specification.unoObject.foo (True) # VA Lid, pass a trueunoobject.foo (1) # Bad, just passing a 1, implementation'll |
String |
Typically, a string maps a python Unicode string, but when you pass a 8-bit Python string, the UNO bridge converts a 8-bit string to a Unicode string.
# IDL Signature Foo ([in] string value) # Both lines is Validunoobject.foo (U ' my foo string ') Unoobject.foo (' My foo stri Ng ') |
Enum |
Example: from Com.sun.star.uno.TypeClass import Unsigned_long unoobject.setvalue (Unsigned_long) if unoobject.getvalue () = = Unsigned_long; Unoobject mapping to enum type |
Type |
Cases: From Com.sun.star.lang import Typeofxcomponentunoobject.settype (typeofxcomponent) if unoobject.gettype () = = Typeofxcomponent: |
struct (and exception) |
Example: The first implementation: using struct constructors, copy constructors, and struct support equals sign operations. From Com.sun.star.beans import PropertyValueFrom Com.sun.star.uno Import exception,runtimeexceptionpropval = PropertyValue () # Default Constructorpropval.name = "foo" propval.value = 2if PropVal = = PropertyValue ("foo", 2): # Memberwise Constructor # True! Passif PropVal = = PropertyValue (propval): # Copy Constructor # True The second way of implementation:uno.createunostruct ()struct = uno.createunostruct ("Com.sun.star.beans.PropertyValue") struct. Name = "foo" struct2 = Structstruct2. Name = "Python" # modifies also struct, probably not desired!unoobject.call (struct, struct2) # passes the same stru Ct 2 times!struct. Name = "Doobidooo" # even worse style. If the UNO object is implemented # in Python, you possibly modify the callee ' s value. # Don ' t do this!
|
secquence |
secquence mapped to python tuple . Secquence<byte> maps to Uno. Bytesecquence. A member variable containing the string type value, value that holds the byte stream. # IDL Signature Writebytes ([in] sequence%lt; byte > Data) #out. Writebytes (Uno. Bytesequence ("abc")) # You could also write the Followingbegin = Uno. Bytesequence ("AB") out.writebytes (begin + "C") # But this does does work!out.writebytes ("abc") # ERROR, no implicit CO Nversion supported by the runtime!# IDL signature Long readbytes ([out] sequence<byte>, [inch] length) Len,seq = in. Readbytes (Dummy, 3) # The statements do the same thingprint seq = = "abc":p rint seq = = Uno. Bytesequence ("abc") |
any |
However, in some special cases, you need to pass in the specified any value For example: # The normal calluno.setpropertyvalue ("foo", (4,5)) # The Uno.invoke Calluno.invoke (obj, "setPropertyValue", ("foo", Uno.) Any ("[]short", (4,5)))) we can use Uno. Any (), passing the type name and value to construct any # constructs a uno. Any, that contains a bytebyteany = Uno. Any ("byte", 5) # Constructs a sequences of Shortsbyteany = Uno. Any ("[]short", (4,5)) |
Reference link: http://www.openoffice.org/udk/python/scriptingframework/index.html document python script, saved in zip. Vnd.sun.star.script:push_me.py$pushme?language=python&location=document global script, saved in directory: Instdir\share\Scripts\ Pythonvnd.sun.star.script:helloworld.py$helloworldpython?language=python&location=share user script, saved in directory: Instdir\ User\scripts\pythonvnd.sun.star.script:helloworld.py$helloworldpython?language=python&location=user User directory embedded in uno-package lo (read only) Vnd.sun.star.script:pyhello2.uno.pkg|package|hallo.py$helloworldpython?language=python &location=user:uno_packages
Python-uno Bridge