LibreOffice 6.2では日本語入力に問題あり LibreOffice最新版 | LibreOffice - オフィススイートのルネサンス と LibreOffice Fresh : “LibreOffice Packaging” team いずれからインスト...
pq@pq - VirtualBox:~$ python3 Python 3.6 . 8 (default, Jan 14 2019 , 11 : 02 : 34 ) [GCC 8.0 . 1 20180414 (experimental) [trunk revision 259383 ]] on linux Type "help" , "copyright" , "credits" or "license" for more information. >>> import sys >>> sys.path [' ', ' / usr / lib / python36. zip ', ' / usr / lib / python3. 6 ', ' / usr / lib / python3. 6 / lib - dynload ', ' / home / pq / .local / lib / python3. 6 / site - packages ', ' / usr / local / lib / python3. 6 / dist - packages ', ' / usr / lib / python3 / dist - packages ', ' / usr / lib / libreoffice / program'] |
LibreOffice最新版 | LibreOffice - オフィススイートのルネサンスからダウンロードしてインストールした場合は、バンドルPythonがインタープリターになっているので、Jupyter NotebookをバンドルPythonで起動する(linuxBean14.04(88)LibreOffice5をIPython Notebookから操作する-p--q)か、sites.pthファイルを作ってunoへのパスを通しておかないといけません(linuxBean14.04(38)LibreOfficeバンドルPythonにパッケージを追加-p--q)。
XSCRIPTCONTEXTを取得する
マクロと同じ挙動にしたいのでXSCRIPTCONTEXTを取得します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import os os.environ[ "UNO_PATH" ] = "/usr/lib/libreoffice/program" import unohelper import officehelper from com.sun.star.script.provider import XScriptContext ctx = officehelper.bootstrap() # コンポーネントコンテクストの取得。 class ScriptContext(unohelper.Base, XScriptContext): def __init__( self , ctx): self .ctx = ctx def getComponentContext( self ): return self .ctx def getDesktop( self ): return ctx.getByName( '/singletons/com.sun.star.frame.theDesktop' ) # com.sun.star.frame.Desktopはdeprecatedになっている。 def getDocument( self ): return self .getDesktop().getCurrentComponent() XSCRIPTCONTEXT = ScriptContext(ctx) |
officehelperはインタープリターをUNO_PATHのパスで探すので、soffice.binのあるディレクトリのパスを渡しています。
WindowsでもLibreOffice5(1)officehelper.bootstrap()を使う-p--qのように短い形式の名前でパスを渡せばうまくいきそうです(未確認)。
Windowsの場合はパンドルPythonになるのでそのインタープリターでJupyter Notebookを起動するかunoへのパスを通しておかないといけません。
Snipettsに登録する
拡張機能のSnipettsにLibreOfficeのXSCRIPTCONTEXTを取得するコードを登録します。
~/.local/share/jupyter/nbextensions/snippetsにあるsnippets.jsonを編集します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
{ "snippets" : [ { "name" : "LibreoOffice" , "code" : [ "import os" , "os.environ['UNO_PATH'] = '/usr/lib/libreoffice/program'" , "import unohelper" , "import officehelper" , "from com.sun.star.script.provider import XScriptContext" , "ctx = officehelper.bootstrap() # コンポーネントコンテクストの取得。" , "class ScriptContext(unohelper.Base, XScriptContext):" , " def __init__(self, ctx):" , " self.ctx = ctx" , " def getComponentContext(self):" , " return self.ctx" , " def getDesktop(self):" , " return ctx.getByName('/singletons/com.sun.star.frame.theDesktop') # com.sun.star.frame.Desktopはdeprecatedになっている。" , " def getDocument(self):" , " return self.getDesktop().getCurrentComponent()" , "XSCRIPTCONTEXT = ScriptContext(ctx)" ] } ] } |
Windows環境ですが、この記事主体にいろいろ参考にして、
返信削除主要モジュール全入り WinPython の Jupyter Lab から接続できました。
ありがとうございます。