標籤:
參考連結:http://www.openoffice.org/udk/python/python-bridge.htmlPyUNO能被用於以下三種模式:1.在LO進程中指令碼架構內(OOo2.0版本以後)http://www.openoffice.org/udk/python/scriptingframework/index.html2.在Python執行中(在LO進程之外)以下情況,你可以考慮使用這個模式:
- 剛開始使用PyUNO(因為這個是更直觀的方法)
- 想要從一個單獨的進程中調用Python指令碼(e.g. a cgi-script within a http-server)
- 想要最短的周轉時間(code - execute - code - execute ...)
調用模式:
- 啟動soffice.bin進程 soffice "-accept=socket,host=localhost,port=2002;urp;"
- Python 指令碼執行,與soffice.bin進程進行處理序間通訊 ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
3.在LO進程內 以下情況,你可以考慮使用這個模式:
- 你想輕鬆將你的代碼運行到多個其他機器(使用UNO packages)
- 指令碼將被UI 事件觸發(menu 或者 toolbars)
- 你已經有了使用PyUNO的經驗
- 你想讓你的指令碼運行效能最好
這個模式是將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 conversionunoObject.takeBool( True) ) # valid, passing trueunoObject.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 trueunoObject.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 validunoObject.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 typeOfXComponentunoObject.setType( typeOfXComponent )if unoObject.getType() == typeOfXComponent: |
| struct(and exception) |
例:第一種實現方式:使用結構體的建構函式,拷貝建構函式,以及結構體支援等號操作。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 第二種實現方式: 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 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 followingbegin = 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 thingprint seq == "abc":print seq == uno.ByteSequence( "abc" ) |
| any |
通常情況下,寫python指令碼時不需要接觸到any類型,只需要根據UNO介面方法所需要的any類型,傳入具體的資料類型就OK。 但是有些特殊情況,需要傳入指定的any值 例如:
# the normal calluno.setPropertyValue( "foo", (4,5))# the uno.invoke calluno.invoke( obj, "setPropertyValue" , ("foo",uno.Any( "[]short", (4,5))) )我們可以使用uno.Any(),傳遞類型名稱和值來構造Any
# constructs a uno.Any, that contains a bytebyteAny = uno.Any( "byte" , 5 )# constructs a sequences of shortsbyteAny = uno.Any( "[]short", (4,5)) |
參考連結:http://www.openoffice.org/udk/python/scriptingframework/index.html文檔python指令碼,儲存在zip中。vnd.sun.star.script:push_me.py$pushMe?language=Python&location=document 全域指令碼,儲存在目錄:instdir\share\Scripts\pythonvnd.sun.star.script:HelloWorld.py$HelloWorldPython?language=Python&location=share 使用者指令碼,儲存在目錄:instdir\user\Scripts\pythonvnd.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
Python-UNO bridge