Python-UNO bridge

PyUNO能被用于以下三种模式:
1.在LO进程中脚本框架内(OOo2.0版本以后)http://www.openoffice.org/udk/python/scriptingframework/index.html
2.在Python执行中(在LO进程之外)
以下情况,你可以考虑使用这个模式:
  • 刚开始使用PyUNO(因为这个是更直观的方法)
  • 想要从一个单独的进程中调用Python脚本(e.g. a cgi-script within a http-server)
  • 想要最短的周转时间(code - execute - code - execute ...)
调用模式:
  1. 启动soffice.bin进程 soffice "-accept=socket,host=localhost,port=2002;urp;"
  2. Python 脚本执行,与soffice.bin进程进行进程间通信 ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
3.在LO进程内
 
以下情况,你可以考虑使用这个模式:
  1. 你想轻松将你的代码运行到多个其他机器(使用UNO packages)
  2. 脚本将被UI 事件触发(menu 或者 toolbars)
  3. 你已经有了使用PyUNO的经验
  4. 你想让你的脚本运行性能最好
这个模式是将py脚本编写成UNO组件的形式,并且打包在odt中供LO使用,具体例子查看最上方参考链接。
 
UNO语言绑定
UNO 类型映射
IDL 类型
Python
integer types (byte, short, unsigned short, long, unsigned long, hyper, unsigned hyper
Python内部只知道C数据类型long和longlong作为整数类型。在大多数机器上,一个long是一个32位值而longlong是一个64位的值.
  • 值来自UNO(例如,UNO方法的返回值)
    byte,short,unsigned short,long or unsigned long 转换到python long. 
    type hyper or unsigned hyper 转换到python long long.
  • 值传给UNO(例如,作为UNO方法的参数)
    如果有一个具体类型的idl方法,值转化为具体的类型(实际上调用服务做了这个工作)。
    如果方法只有一个any,每一个整数值转换为最小的数据类型(5 becomes a byte, 150 becomes a short, 0x1f023 becomes a long and values larger than 0xffffffff become a hyper)
boolean
Python 内部有boolean 数据类型,继承integer type 存在单件真假,pyuno使用区分整数和布尔值.
如果一个UNO方法的参数为boolean,你也可以使用数值传递
例如:
#idl signature void takeBool( [in] boolean bool )

unoObject.takeBool( 1 )       # valid, passing true (PyUNO runtime
                              # does the conversion
unoObject.takeBool( True) )   # valid, passing true
unoObject.takeBool( False )   # valid, passing false
然而当你想明确的指定传递boolean,并用any类型作为参数,你必须使用True 或 False

# idl signature void foo( [in] any value )

# implementation expects a boolean (which is separately documented
# e.g. in the service specification.
unoObject.foo( True ) # valid, pass a true
unoObject.foo( 1 )    # bad, just passing a 1, implementation will

string
通常情况下,string映射python unicode string,但是当传递8位的python string , UNO 桥会将8位的string转换为unicode string.
# idl signature foo( [in] string value )
# both lines are valid
unoObject.foo( u'my foo string' )
unoObject.foo( 'my foo string' )
enum
例:
from com.sun.star.uno.TypeClass import UNSIGNED_LONG
 
unoObject.setValue(UNSIGNED_LONG)
if unoObject.getValue() == UNSIGNED_LONG;
 
unoObject 映射为枚举类型
type
例:
from com.sun.star.lang import typeOfXComponent

unoObject.setType( typeOfXComponent )
if unoObject.getType() == typeOfXComponent:
struct(and exception)
例:
第一种实现方式:使用结构体的构造函数,拷贝构造函数,以及结构体支持等于号操作。
from com.sun.star.beans import PropertyValue
from com.sun.star.uno import Exception,RuntimeException

propVal = PropertyValue()                 # Default constructor
propVal.Name = "foo"
propVal.Value = 2

if propVal == PropertyValue( "foo", 2 ):  # Memberwise constructor
   # true !
   pass

if propVal == PropertyValue( propVal ):   # Copy Constructor
   # true
第二种实现方式: uno.createUnoStruct()
struct = uno.createUnoStruct( "com.sun.star.beans.PropertyValue" )

struct.Name = "foo"
struct2 = struct
struct2.Name = "python"           # modifies also struct, probably not desired !
unoObject.call( struct, struct2 ) # passes the same struct 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映射到python为tuple
secquence<byte>映射为uno.ByteSecquence. 包含字符串类型的成员变量value, value存放字节流。
# idl signature writeBytes( [in] sequence%lt; byte > data )
#
out.writeBytes( uno.ByteSequence( "abc" ) )

# you could also write the following
begin = uno.ByteSequence( "ab" )
out.writeBytes( begin + "c" )

# but this does not work !
out.writeBytes( "abc" ) # ERROR, no implicit conversion supported by the runtime !


# idl signature long readBytes( [out] sequence<byte> , [in] length )
len,seq = in.readBytes( dummy, 3 )

# the statements do the same thing
print seq == "abc":
print seq == uno.ByteSequence( "abc" )
any 通常情况下,写python脚本时不需要接触到any类型,只需要根据UNO接口方法所需要的any类型,传入具体的数据类型就OK。
但是有些特殊情况,需要传入指定的any值
例如:
# the normal call
uno.setPropertyValue( "foo", (4,5))

# the uno.invoke call
uno.invoke( obj, "setPropertyValue" , ("foo",uno.Any( "[]short", (4,5))) )
我们可以使用uno.Any(),传递类型名称和值来构造Any
# constructs a uno.Any, that contains a byte
byteAny = uno.Any( "byte" , 5 )

# constructs a sequences of shorts
byteAny = uno.Any( "[]short", (4,5))
 
文档python脚本,保存在zip中。
vnd.sun.star.script:push_me.py$pushMe?language=Python&location=document
 
全局脚本,保存在目录:instdirshareScriptspython
vnd.sun.star.script:HelloWorld.py$HelloWorldPython?language=Python&location=share
 
用户脚本,保存在目录:instdiruserScriptspython
vnd.sun.star.script:HelloWorld.py$HelloWorldPython?language=Python&location=user
 
嵌入uno-package LO的用户目录(只读)
vnd.sun.star.script:pyhello2.uno.pkg|package|hallo.py$HelloWorldPython?language=Python&location=user:uno_packages
原文地址:https://www.cnblogs.com/linTracy/p/5367225.html