create a new page, using OOoPython/OverView as a template.
Front page
Search
掲示板
Reload
Help
Browse Log
掲示板の使い方
OOo 掲示板3
OOo 掲示板2
OOo 掲示板
掲示板
雑談掲示板
New
List of pages
Recent changes
Backup
簡単ヘルプ
整形ルール
Start:
*Python-UNO 概要 [#fe118d8d]
OpenOffice.org と Python 関連概要
#contents
**概要 [#obd8ed6e]
Python-UNO (Py-UNO) は Python-UNO ブリッジを介して python...
使い方は自分で調べるという人のためにいくつかページを挙げ...
-Python-UNO bridge
--http://udk.openoffice.org/python/python-bridge.html
-Python (OpenOffice.org Wiki)
--http://wiki.services.openoffice.org/wiki/Extensions_dev...
-OOoPython
--[[OOoPython]]
-Script locations
--http://udk.openoffice.org/python/scriptingframework/ind...
//-Python 2.5 へのアップデート関連 issue
//-http://www.openoffice.org/issues/show_bug.cgi?id=71327
Python-UNO ブリッジは C++ で書かれたコードなので高速に動...
OpenOffice.org の Windows パッケージではわざわざ取り除か...
また、Linux などの環境では Py-UNO パッケージをインストー...
***Py-UNO でできること [#w59468b2]
Py-UNO では次の三種類のことができます。
-[[Scripting Framework スクリプトのマクロとして動作>../Ma...
-[[UNO コンポーネント作成>../UNOComponent]]
-[[Py-UNO bridge を介して外部 Python から OOo オートメー...
マクロとしてというのは、OOo Basic で書いたマクロのように...
//***その他 [#w79e3cf5]
//OOo Con 2007 のスライドを見ていると Python を利用した C...
//-http://marketing.openoffice.org/ooocon2007/programme/t...
//この中で Why Python? "It's cooler than StarBasic." や "...
*** 日本語 [#l90aeb7b]
OOo に付属している Python は 2.3.5 のため日本語文字コード...
日本語などの文字列をソースに入れる場合には次のようにエン...
#code(python){{
#!
# -*- coding: utf_8 -*-
}}
また、shift_jis や euc_jp などの文字コードを指定して deco...
また、OOo 2.4 系では各 Linux ディストリビューションが独自...
**各種取得 [#kaa762da]
各種いろいろなものを Py-Uno で利用する方法。OOo Basic で...
uno や unohelper は OOo の program ディレクトリに置かれて...
import uno
import unohelper
***XSCRIPTCONTEXT [#x2a62fa2]
Scripting Framework のスクリプトで使われる XSCRIPTCONTEXT...
現在のドキュメントを取得します。
document = XSCRIPTCONTEXT.getDocument()
StarDesktop.getCurrentComponent() 'OOo Basic
デスクトップ com.sun.star.frame.Desktop サービスを取得し...
desktop = XSCRIPTCONTEXT.getDesktop()
StarDesktop 'OOo Basic
CreateUnoService("com.sun.star.frame.Desktop") 'another
コンポーネントコンテキストを取得します。
ctx = XSCRIPTCONTEXT.getComponentContext()
GetDefaultContext() 'OOo Basic
***サービスマネージャ [#ff55347d]
MultiServiceFactory を利用するには次のようにします。ctx ...
smgr = ctx.ServiceManager
oServiceManager = GetProcessServiceManager() ' OOo Basic
***サービス [#b528d707]
サービスマネージャから createInstanceWithContext メソッド...
desktop = smgr.createInstanceWithContext("com.sun.star.f...
oDesktop = CreateUnoService("com.sun.star.frame.Desktop"...
***struct [#maceb9a8]
struct の作成
uno.createUnoStruct("com.sun.star.awt.Size")
または、
from com.sun.star.beans import PropertyValue
porp = PropertyValue()
() が必要です。
OOo Basic では
CreateUnoStruct("com.sun.star.awt.Size")
や Dim ... New As ...
タイプ名が []com.sun.star.beans.PropertyValue などのシー...
from com.sun.star.beans import PropertyValue
args = []
a1 = PropertyValue()
a1.Name = "InputStream"
a1.Value = ioin
a2 = PropertyValue()
a2.Name = "FilterName"
a2.Value = "calc8"
args.append(a1)
args.append(a2)
desktop.loadComponentFromURL("private:stream", "_blank",...
Basic では簡単に
Dim args(1) As New com.sun.star.beans.PropertyValue
***enum [#o8363580]
enum は import します。
from com.sun.star.awt.WindowClass import TOP as UNO_WC_TOP
また、次のようにも出来ます。
uno.Enum("com.sun.star.awt.WindowClass","TOP")
OOo Basic では
nEnum = com.sun.star.awt.WindowClass.TOP
With ステートメントも利用できます。
***Constants [#bb6344c5]
enum 同様に import します。
from com.sun.star.beans.MethodConcept import ALL as UNO_...
また、
uno.getConstantByName("com.sun.star.beans.MethodConcept....
OOo Basic は enum と同様。
***配列 [#a0a5e59e]
配列というより UNO の sequence 型。API 関数から戻される s...
edit_model.setPropertyValues(
("MultiLine",),
(True,) )
oEdit_model.setPropertyValues( _
Array("MultiLine"), Array(true) ) ' OOo Basic
***boolean [#s48fefad]
True または False
OOo Basic だと大文字と小文字に依存しませんが、python だと...
***string [#we8b94e2]
文字列は python unicode。8 bit を与えたときは自動的に変換...
***Char [#re0fd7f0]
文字というか char は uno.py で定義されている Char クラス...
***ByteSequence [#ce24419e]
Python ではバイトは文字として扱うが、UNO 側から渡されたバ...
***インターフェース [#w6f75959]
サービスを実装したり、リスナーを利用するときにはインター...
import unohelper
from com.sun.star.awt import XActionListener
class aclistener(unohelper.Base,XActionListener):
def __init__(self,ctx):
self.ctx = ctx
return
def actionPerformed(self,ev):
pass
def disposing(self,ev):
pass
***URL <-> System FilePath [#g8257503]
システムパスから URL
unohelper.systemPathToFileUrl( systemPath )
URL からシステムパス
unohelper.fileUrlToSystemPath( url )
***引数と返り値 [#d0d6671f]
API メソッドの引数に [out] のものが時々ありますが、Py-UNO...
Basic では
aURL = CreateUnoStruct("com.sun.star.util.URL")
aURL.Complete = ".uno:Paste"
CreateUnoService("com.sun.star.util.URLTransformer")._
parseStrict(aURL)
'parseStrict の返り値は void で、最初の引数が [inout]
Py-UNO だと
aURL = uno.createUnoStruct("com.sun.star.util.URL")
aURL.Complete = ".uno:Paste"
transformer = smgr.createInstanceWithContext(
"com.sun.star.util.URLTransformer",ctx)
dummy,aURL = transformer.parseStrict(aURL)
# 元々の返り値,out のある引数の値
# ここでは dummy は void (Python では None)
詳細は上記リンク参照。
***ダイアログ [#k28f0023]
com.sun.star.awt.DialogProvider サービスを利用してダイア...
def dialog_test():
ctx = XSCRIPTCONTEXT.getComponentContext()
smgr = ctx.ServiceManager
dialogprovider = smgr.createInstanceWithContext(
"com.sun.star.awt.DialogProvider",ctx)
url = "vnd.sun.star.script:Standard.Dialog1?location=ap...
dialog = dialogprovider.createDialog(url)
dialog.execute()
ダイアログを指定する URL はダイアログライブラリの URI で...
***実行するスクリプト一覧に表示する関数名 [#h14f3d66]
OOo で python スクリプトをマクロとして実行するときは関数...
g_exportedScripts = calc_thread,
タプルで表示したい関数名を指定します。g_exportedScripts ...
***import [#r82840b6]
2.4 から Python の標準ライブラリ以外の独自のファイルイン...
実行するスクリプトファイルと同じディレクトリに pythonpath...
test.py (実行するスクリプトファイル)
--pythonpath/inc.py (インポートするファイル)
import inc
ディレクトリの代わりに pythonpath.zip ファイルを作成して...
Framework script でなく Py-UNO コンポーネントのときにも同...
2.4 で修正済み該当 issue [[i76281>http://www.openoffice.o...
import したファイルは拡張機能パッケージがインストールされ...
***メッセージボックス [#vda709dc]
簡単なメッセージボックスは 2.3 以降では次のように簡単に表...
#code(python){{
import uno
def test10():
message(XSCRIPTCONTEXT.getDesktop(),"message","title")
def message(desktop,msg="",title=""):
frame = desktop.getCurrentFrame()
win = frame.getContainerWindow()
toolkit = win.getToolkit()
rect = uno.createUnoStruct("com.sun.star.awt.Rectangle")
msgbox = toolkit.createMessageBox(
win,rect,"messbox",1,title,msg)
msgbox.execute()
}}
**例外 [#r5a8fe63]
基本的に Python の try、except、else、raise を使う。UNO ...
また、利用する例外は struct と同じように import しておき...
http://udk.openoffice.org/python/python-bridge.html#excep...
例外を raise で送出できます。
#code(python){{
import uno
import unohelper
from com.sun.star.lang import XServiceInfo
from com.sun.star.container import XIndexContainer
from com.sun.star.uno.TypeClass import STRING as TC_STRING
from com.sun.star.lang import \
IndexOutOfBoundsException as UNO_IndexOutOfBoundsExcepti...
IllegalArgumentException as UNO_IllegalArgumentException...
IndexOutOfBoundsException as UNO_IndexOutOfBoundsException
IMPLE_NAME = u'mytools.service.Test1'
class new_service(unohelper.Base, XIndexContainer, XServi...
"""This service implements container that retains string...
def __init__(self):
print "initialize..."
self.cont = []
def getImplementationName(self):
return IMPLE_NAME
def supportsService(self, name):
return name == IMPLE_NAME
def getSupportedServiceNames(self):
return (IMPLE_NAME,)
def insertByIndex(self, i, item):
if isinstance(item, basestring):
if 0 <= i <= len(self.cont):
self.cont.insert(i, item)
else:
uno_e = UNO_IndexOutOfBoundsException()
uno_e.Message = u'Index out of bounds. %s' % len(self...
raise uno_e
else:
uno_e = UNO_IllegalArgumentException()
uno_e.ArgumentPosition = 1
raise uno_e
def removeByIndex(self, i):
if 0 <= i < len(self.cont):
del self.cont[i]
else:
uno_e = UNO_IndexOutOfBoundsException()
uno_e.Message = u'Index out of bounds. %s' % len(self....
raise uno_e
def replaceByIndex(self, i, item):
if isinstance(item, basestring):
if 0 <= i < len(self.cont):
self.cont[i] = item
else:
uno_e = UNO_IndexOutOfBoundsException()
uno_e.Message = u'Index out of bounds. %s' % len(self...
raise uno_e
else:
uno_e = UNO_IllegalArgumentException()
uno_e.ArgumentPosition = 1
raise uno_e
def getCount(self):
return len(self.cont)
def getByIndex(self, i):
try:
return self.cont.get(i)
except ValueError,e:
uno_e = UNO_IndexOutOfBoundsException()
uno_e.Message = u'Index out of bounds. %s' % len(self....
raise uno_e
def getElementType(self):
return uno.Type(u'string', TC_STRING)
def hasElements(self):
if len(self.cont) > 0:
return True
return False
}}
**標準出力 [#rf334ae9]
Windows 環境では標準出力が利用できません。一方、Linux な...
sys.stdout を適当な I/O オブジェクトと置き換えると Window...
End:
*Python-UNO 概要 [#fe118d8d]
OpenOffice.org と Python 関連概要
#contents
**概要 [#obd8ed6e]
Python-UNO (Py-UNO) は Python-UNO ブリッジを介して python...
使い方は自分で調べるという人のためにいくつかページを挙げ...
-Python-UNO bridge
--http://udk.openoffice.org/python/python-bridge.html
-Python (OpenOffice.org Wiki)
--http://wiki.services.openoffice.org/wiki/Extensions_dev...
-OOoPython
--[[OOoPython]]
-Script locations
--http://udk.openoffice.org/python/scriptingframework/ind...
//-Python 2.5 へのアップデート関連 issue
//-http://www.openoffice.org/issues/show_bug.cgi?id=71327
Python-UNO ブリッジは C++ で書かれたコードなので高速に動...
OpenOffice.org の Windows パッケージではわざわざ取り除か...
また、Linux などの環境では Py-UNO パッケージをインストー...
***Py-UNO でできること [#w59468b2]
Py-UNO では次の三種類のことができます。
-[[Scripting Framework スクリプトのマクロとして動作>../Ma...
-[[UNO コンポーネント作成>../UNOComponent]]
-[[Py-UNO bridge を介して外部 Python から OOo オートメー...
マクロとしてというのは、OOo Basic で書いたマクロのように...
//***その他 [#w79e3cf5]
//OOo Con 2007 のスライドを見ていると Python を利用した C...
//-http://marketing.openoffice.org/ooocon2007/programme/t...
//この中で Why Python? "It's cooler than StarBasic." や "...
*** 日本語 [#l90aeb7b]
OOo に付属している Python は 2.3.5 のため日本語文字コード...
日本語などの文字列をソースに入れる場合には次のようにエン...
#code(python){{
#!
# -*- coding: utf_8 -*-
}}
また、shift_jis や euc_jp などの文字コードを指定して deco...
また、OOo 2.4 系では各 Linux ディストリビューションが独自...
**各種取得 [#kaa762da]
各種いろいろなものを Py-Uno で利用する方法。OOo Basic で...
uno や unohelper は OOo の program ディレクトリに置かれて...
import uno
import unohelper
***XSCRIPTCONTEXT [#x2a62fa2]
Scripting Framework のスクリプトで使われる XSCRIPTCONTEXT...
現在のドキュメントを取得します。
document = XSCRIPTCONTEXT.getDocument()
StarDesktop.getCurrentComponent() 'OOo Basic
デスクトップ com.sun.star.frame.Desktop サービスを取得し...
desktop = XSCRIPTCONTEXT.getDesktop()
StarDesktop 'OOo Basic
CreateUnoService("com.sun.star.frame.Desktop") 'another
コンポーネントコンテキストを取得します。
ctx = XSCRIPTCONTEXT.getComponentContext()
GetDefaultContext() 'OOo Basic
***サービスマネージャ [#ff55347d]
MultiServiceFactory を利用するには次のようにします。ctx ...
smgr = ctx.ServiceManager
oServiceManager = GetProcessServiceManager() ' OOo Basic
***サービス [#b528d707]
サービスマネージャから createInstanceWithContext メソッド...
desktop = smgr.createInstanceWithContext("com.sun.star.f...
oDesktop = CreateUnoService("com.sun.star.frame.Desktop"...
***struct [#maceb9a8]
struct の作成
uno.createUnoStruct("com.sun.star.awt.Size")
または、
from com.sun.star.beans import PropertyValue
porp = PropertyValue()
() が必要です。
OOo Basic では
CreateUnoStruct("com.sun.star.awt.Size")
や Dim ... New As ...
タイプ名が []com.sun.star.beans.PropertyValue などのシー...
from com.sun.star.beans import PropertyValue
args = []
a1 = PropertyValue()
a1.Name = "InputStream"
a1.Value = ioin
a2 = PropertyValue()
a2.Name = "FilterName"
a2.Value = "calc8"
args.append(a1)
args.append(a2)
desktop.loadComponentFromURL("private:stream", "_blank",...
Basic では簡単に
Dim args(1) As New com.sun.star.beans.PropertyValue
***enum [#o8363580]
enum は import します。
from com.sun.star.awt.WindowClass import TOP as UNO_WC_TOP
また、次のようにも出来ます。
uno.Enum("com.sun.star.awt.WindowClass","TOP")
OOo Basic では
nEnum = com.sun.star.awt.WindowClass.TOP
With ステートメントも利用できます。
***Constants [#bb6344c5]
enum 同様に import します。
from com.sun.star.beans.MethodConcept import ALL as UNO_...
また、
uno.getConstantByName("com.sun.star.beans.MethodConcept....
OOo Basic は enum と同様。
***配列 [#a0a5e59e]
配列というより UNO の sequence 型。API 関数から戻される s...
edit_model.setPropertyValues(
("MultiLine",),
(True,) )
oEdit_model.setPropertyValues( _
Array("MultiLine"), Array(true) ) ' OOo Basic
***boolean [#s48fefad]
True または False
OOo Basic だと大文字と小文字に依存しませんが、python だと...
***string [#we8b94e2]
文字列は python unicode。8 bit を与えたときは自動的に変換...
***Char [#re0fd7f0]
文字というか char は uno.py で定義されている Char クラス...
***ByteSequence [#ce24419e]
Python ではバイトは文字として扱うが、UNO 側から渡されたバ...
***インターフェース [#w6f75959]
サービスを実装したり、リスナーを利用するときにはインター...
import unohelper
from com.sun.star.awt import XActionListener
class aclistener(unohelper.Base,XActionListener):
def __init__(self,ctx):
self.ctx = ctx
return
def actionPerformed(self,ev):
pass
def disposing(self,ev):
pass
***URL <-> System FilePath [#g8257503]
システムパスから URL
unohelper.systemPathToFileUrl( systemPath )
URL からシステムパス
unohelper.fileUrlToSystemPath( url )
***引数と返り値 [#d0d6671f]
API メソッドの引数に [out] のものが時々ありますが、Py-UNO...
Basic では
aURL = CreateUnoStruct("com.sun.star.util.URL")
aURL.Complete = ".uno:Paste"
CreateUnoService("com.sun.star.util.URLTransformer")._
parseStrict(aURL)
'parseStrict の返り値は void で、最初の引数が [inout]
Py-UNO だと
aURL = uno.createUnoStruct("com.sun.star.util.URL")
aURL.Complete = ".uno:Paste"
transformer = smgr.createInstanceWithContext(
"com.sun.star.util.URLTransformer",ctx)
dummy,aURL = transformer.parseStrict(aURL)
# 元々の返り値,out のある引数の値
# ここでは dummy は void (Python では None)
詳細は上記リンク参照。
***ダイアログ [#k28f0023]
com.sun.star.awt.DialogProvider サービスを利用してダイア...
def dialog_test():
ctx = XSCRIPTCONTEXT.getComponentContext()
smgr = ctx.ServiceManager
dialogprovider = smgr.createInstanceWithContext(
"com.sun.star.awt.DialogProvider",ctx)
url = "vnd.sun.star.script:Standard.Dialog1?location=ap...
dialog = dialogprovider.createDialog(url)
dialog.execute()
ダイアログを指定する URL はダイアログライブラリの URI で...
***実行するスクリプト一覧に表示する関数名 [#h14f3d66]
OOo で python スクリプトをマクロとして実行するときは関数...
g_exportedScripts = calc_thread,
タプルで表示したい関数名を指定します。g_exportedScripts ...
***import [#r82840b6]
2.4 から Python の標準ライブラリ以外の独自のファイルイン...
実行するスクリプトファイルと同じディレクトリに pythonpath...
test.py (実行するスクリプトファイル)
--pythonpath/inc.py (インポートするファイル)
import inc
ディレクトリの代わりに pythonpath.zip ファイルを作成して...
Framework script でなく Py-UNO コンポーネントのときにも同...
2.4 で修正済み該当 issue [[i76281>http://www.openoffice.o...
import したファイルは拡張機能パッケージがインストールされ...
***メッセージボックス [#vda709dc]
簡単なメッセージボックスは 2.3 以降では次のように簡単に表...
#code(python){{
import uno
def test10():
message(XSCRIPTCONTEXT.getDesktop(),"message","title")
def message(desktop,msg="",title=""):
frame = desktop.getCurrentFrame()
win = frame.getContainerWindow()
toolkit = win.getToolkit()
rect = uno.createUnoStruct("com.sun.star.awt.Rectangle")
msgbox = toolkit.createMessageBox(
win,rect,"messbox",1,title,msg)
msgbox.execute()
}}
**例外 [#r5a8fe63]
基本的に Python の try、except、else、raise を使う。UNO ...
また、利用する例外は struct と同じように import しておき...
http://udk.openoffice.org/python/python-bridge.html#excep...
例外を raise で送出できます。
#code(python){{
import uno
import unohelper
from com.sun.star.lang import XServiceInfo
from com.sun.star.container import XIndexContainer
from com.sun.star.uno.TypeClass import STRING as TC_STRING
from com.sun.star.lang import \
IndexOutOfBoundsException as UNO_IndexOutOfBoundsExcepti...
IllegalArgumentException as UNO_IllegalArgumentException...
IndexOutOfBoundsException as UNO_IndexOutOfBoundsException
IMPLE_NAME = u'mytools.service.Test1'
class new_service(unohelper.Base, XIndexContainer, XServi...
"""This service implements container that retains string...
def __init__(self):
print "initialize..."
self.cont = []
def getImplementationName(self):
return IMPLE_NAME
def supportsService(self, name):
return name == IMPLE_NAME
def getSupportedServiceNames(self):
return (IMPLE_NAME,)
def insertByIndex(self, i, item):
if isinstance(item, basestring):
if 0 <= i <= len(self.cont):
self.cont.insert(i, item)
else:
uno_e = UNO_IndexOutOfBoundsException()
uno_e.Message = u'Index out of bounds. %s' % len(self...
raise uno_e
else:
uno_e = UNO_IllegalArgumentException()
uno_e.ArgumentPosition = 1
raise uno_e
def removeByIndex(self, i):
if 0 <= i < len(self.cont):
del self.cont[i]
else:
uno_e = UNO_IndexOutOfBoundsException()
uno_e.Message = u'Index out of bounds. %s' % len(self....
raise uno_e
def replaceByIndex(self, i, item):
if isinstance(item, basestring):
if 0 <= i < len(self.cont):
self.cont[i] = item
else:
uno_e = UNO_IndexOutOfBoundsException()
uno_e.Message = u'Index out of bounds. %s' % len(self...
raise uno_e
else:
uno_e = UNO_IllegalArgumentException()
uno_e.ArgumentPosition = 1
raise uno_e
def getCount(self):
return len(self.cont)
def getByIndex(self, i):
try:
return self.cont.get(i)
except ValueError,e:
uno_e = UNO_IndexOutOfBoundsException()
uno_e.Message = u'Index out of bounds. %s' % len(self....
raise uno_e
def getElementType(self):
return uno.Type(u'string', TC_STRING)
def hasElements(self):
if len(self.cont) > 0:
return True
return False
}}
**標準出力 [#rf334ae9]
Windows 環境では標準出力が利用できません。一方、Linux な...
sys.stdout を適当な I/O オブジェクトと置き換えると Window...
Page: