Python-UNO bridge

本文详细介绍了PyUNO的三种使用模式及其适用场景,包括脚本框架内的使用、Python执行中的使用以及LO进程内的使用。此外还深入探讨了UNO语言绑定中的类型映射,如整型、布尔型、字符串、枚举等的数据类型转换。
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
 
全局脚本,保存在目录:instdir\share\Scripts\python
vnd.sun.star.script:HelloWorld.py$HelloWorldPython?language=Python&location=share
 
用户脚本,保存在目录:instdir\user\Scripts\python
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

ann@ann:~$ dpkg -l | grep python3 ii libpython3-dev:amd64 3.12.3-0ubuntu2 amd64 header files and a static library for Python (default) ii libpython3-stdlib:amd64 3.12.3-0ubuntu2 amd64 interactive high-level object-oriented language (default python3 version) ii libpython3.10-minimal:amd64 3.10.4-3 amd64 Minimal subset of the Python language (version 3.10) ii libpython3.10-stdlib:amd64 3.10.4-3 amd64 Interactive high-level object-oriented language (standard library, version 3.10) ii libpython3.12-dev:amd64 3.12.3-1ubuntu0.7 amd64 Header files and a static library for Python (v3.12) ii libpython3.12-minimal:amd64 3.12.3-1ubuntu0.7 amd64 Minimal subset of the Python language (version 3.12) ii libpython3.12-stdlib:amd64 3.12.3-1ubuntu0.7 amd64 Interactive high-level object-oriented language (standard library, version 3.12) ii libpython3.12t64:amd64 3.12.3-1ubuntu0.7 amd64 Shared Python runtime library (version 3.12) ii python3 3.12.3-0ubuntu2 amd64 interactive high-level object-oriented language (default python3 version) ii python3-apport 2.28.1-0ubuntu3.7 all Python 3 library for Apport crash report handling ii python3-apt 2.7.7ubuntu4 amd64 Python 3 interface to libapt-pkg ii python3-aptdaemon 1.1.1+bzr982-0ubuntu44 all Python 3 module for the server and client of aptdaemon ii python3-aptdaemon.gtk3widgets 1.1.1+bzr982-0ubuntu44 all Python 3 GTK+ 3 widgets to run an aptdaemon client ii python3-attr 23.2.0-2 all Attributes without boilerplate (Python 3) ii python3-babel 2.10.3-3build1 all tools for internationalizing Python applications - Python 3.x ii python3-bcrypt 3.2.2-1build1 amd64 password hashing library for Python 3 ii python3-blinker 1.7.0-1 all Fast, simple object-to-object and broadcast signaling (Python3) ii python3-bpfcc 0.29.1+ds-1ubuntu7 all Python 3 wrappers for BPF Compiler Collection (BCC) ii python3-brlapi:amd64 6.6-4ubuntu5 amd64 Braille display access via BRLTTY - Python3 bindings ii python3-cairo 1.25.1-2build2 amd64 Python3 bindings for the Cairo vector graphics library ii python3-certifi 2023.11.17-1 all root certificates for validating SSL certs and verifying TLS hosts (python3) ii python3-cffi-backend:amd64 1.16.0-2build1 amd64 Foreign Function Interface for Python 3 calling C code - runtime ii python3-chardet 5.2.0+dfsg-1 all Universal Character Encoding Detector (Python3) ii python3-click 8.1.6-2 all Wrapper around optparse for command line utilities - Python 3.x ii python3-colorama 0.4.6-4 all Cross-platform colored terminal text in Python - Python 3.x ii python3-commandnotfound 23.04.0 all Python 3 bindings for command-not-found. ii python3-configobj 5.0.8-3 all simple but powerful config file reader and writer for Python 3 ii python3-cryptography 41.0.7-4ubuntu0.1 amd64 Python library exposing cryptographic recipes and primitives (Python 3) ii python3-cups:amd64 2.0.1-5build6 amd64 Python3 bindings for CUPS ii python3-cupshelpers 1.5.18-1ubuntu9 all Python utility modules around the CUPS printing system ii python3-dateutil 2.8.2-3ubuntu1 all powerful extensions to the standard Python 3 datetime module ii python3-dbus 1.3.2-5build3 amd64 simple interprocess messaging system (Python 3 interface) ii python3-debconf 1.5.86ubuntu1 all interact with debconf from Python 3 ii python3-debian 0.1.49ubuntu2 all Python 3 modules to work with Debian-related data formats ii python3-defer 1.0.6-2.1ubuntu1 all Small framework for asynchronous programming (Python 3) ii python3-dev 3.12.3-0ubuntu2 amd64 header files and a static library for Python (default) ii python3-distro 1.9.0-1 all Linux OS platform information API ii python3-distro-info 1.7build1 all information about distributions' releases (Python 3 module) ii python3-distupgrade 1:24.04.26 all manage release upgrades ii python3-fasteners 0.18-2 all provides useful locks - Python 3.x ii python3-gdbm:amd64 3.12.3-0ubuntu1 amd64 GNU dbm database support for Python 3.x ii python3-gi 3.48.2-1 amd64 Python 3 bindings for gobject-introspection libraries ii python3-httplib2 0.20.4-3 all comprehensive HTTP client library written for Python3 ii python3-ibus-1.0 1.5.29-2 all Intelligent Input Bus - introspection overrides for Python (Python 3) ii python3-idna 3.6-2ubuntu0.1 all Python IDNA2008 (RFC 5891) handling (Python 3) ii python3-jinja2 3.1.2-1ubuntu1.3 all small but fast and easy to use stand-alone template engine ii python3-json-pointer 2.0-0ubuntu1 all resolve JSON pointers - Python 3.x ii python3-jsonpatch 1.32-3 all library to apply JSON patches - Python 3.x ii python3-jsonschema 4.10.3-2ubuntu1 all An(other) implementation of JSON Schema (Draft 3, 4, 6, 7) ii python3-jwt 2.7.0-1 all Python 3 implementation of JSON Web Token ii python3-launchpadlib 1.11.0-6 all Launchpad web services client library (Python 3) ii python3-lazr.restfulclient 0.14.6-1 all client for lazr.restful-based web services (Python 3) ii python3-lazr.uri 1.0.6-3 all library for parsing, manipulating, and generating URIs ii python3-louis 3.29.0-1build1 all Python bindings for liblouis ii python3-mako 1.3.2-1 all fast and lightweight templating for the Python 3 platform ii python3-markdown-it 3.0.0-2 all Python port of markdown-it and some its associated plugins ii python3-markupsafe 2.1.5-1build2 amd64 HTML/XHTML/XML string library ii python3-mdurl 0.1.2-1 all Python port of the JavaScript mdurl package rF python3-minimal 3.12.3-0ubuntu2 amd64 minimal subset of the Python language (default python3 version) ii python3-monotonic 1.6-2 all implementation of time.monotonic() - Python 3.x ii python3-nacl 1.5.0-4build1 amd64 Python bindings to libsodium (Python 3) ii python3-netaddr 0.8.0-2ubuntu1 all manipulation of various common network address notations (Python 3) ii python3-netifaces:amd64 0.11.0-2build3 amd64 portable network interface information - Python 3.x ii python3-netplan 1.1.2-2~ubuntu24.04.1 amd64 Declarative network configuration Python bindings ii python3-oauthlib 3.2.2-1 all generic, spec-compliant implementation of OAuth for Python3 ii python3-olefile 0.46-3 all Python module to read/write MS OLE2 files ii python3-paramiko 2.12.0-2ubuntu4.1 all Make ssh v2 connections (Python 3) ii python3-pexpect 4.9-2 all Python 3 module for automating interactive applications ii python3-pil:amd64 10.2.0-1ubuntu1 amd64 Python Imaging Library (Python3) ii python3-pip 24.0+dfsg-1ubuntu1.2 all Python package installer ii python3-pip-whl 24.0+dfsg-1ubuntu1.2 all Python package installer (pip wheel) ii python3-pkg-resources 68.1.2-2ubuntu1.2 all Package Discovery and Resource Access using pkg_resources ii python3-problem-report 2.28.1-0ubuntu3.7 all Python 3 library to handle problem reports ii python3-ptyprocess 0.7.0-5 all Run a subprocess in a pseudo terminal from Python 3 ii python3-pygments 2.17.2+dfsg-1 all syntax highlighting package written in Python 3 ii python3-pyparsing 3.1.1-1 all alternative to creating and executing simple grammars - Python 3.x ii python3-pyrsistent:amd64 0.20.0-1build2 amd64 persistent/functional/immutable data structures for Python ii python3-requests 2.31.0+dfsg-1ubuntu1.1 all elegant and simple HTTP library for Python3, built for human beings ii python3-rich 13.7.1-1 all render rich text, tables, progress bars, syntax highlighting, markdown and more ii python3-serial 3.5-2 all pyserial - module encapsulating access for the serial port ii python3-setuptools 68.1.2-2ubuntu1.2 all Python3 Distutils Enhancements ii python3-setuptools-whl 68.1.2-2ubuntu1.2 all Python Distutils Enhancements (wheel package) ii python3-six 1.16.0-4 all Python 2 and 3 compatibility library ii python3-software-properties 0.99.49.2 all manage the repositories that you install software from ii python3-speechd 0.12.0~rc2-2build3 all Python interface to Speech Dispatcher ii python3-sss 2.9.4-1.1ubuntu6.2 amd64 Python3 module for the System Security Services Daemon ii python3-systemd 235-1build4 amd64 Python 3 bindings for systemd ii python3-typing-extensions 4.10.0-1 all Backported and Experimental Type Hints for Python ii python3-tz 2024.1-2 all Python3 version of the Olson timezone database ii python3-uno 4:24.2.7-0ubuntu0.24.04.4 amd64 Python-UNO bridge ii python3-update-manager 1:24.04.12 all Python 3.x module for update-manager ii python3-urllib3 2.0.7-1ubuntu0.2 all HTTP library with thread-safe connection pooling for Python3 ii python3-wadllib 1.3.6-5 all Python 3 library for navigating WADL files ii python3-wheel 0.42.0-2 all built-package format for Python ii python3-xdg 0.28-2 all Python 3 library to access freedesktop.org standards ii python3-xkit 0.5.0ubuntu6 all library for the manipulation of xorg.conf files (Python 3) ii python3-yaml 6.0.1-2build2 amd64 YAML parser and emitter for Python3 ii python3.10-minimal 3.10.4-3 amd64 Minimal subset of the Python language (version 3.10) ii python3.12 3.12.3-1ubuntu0.7 amd64 Interactive high-level object-oriented language (version 3.12) ii python3.12-dev 3.12.3-1ubuntu0.7 amd64 Header files and a static library for Python (v3.12) ii python3.12-minimal 3.12.3-1ubuntu0.7 amd64 Minimal subset of the Python language (version 3.12) ii python3.12-venv 3.12.3-1ubuntu0.7 amd64 Interactive high-level object-oriented language (pyvenv binary, version 3.12)
最新发布
07-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值