LibreOffice CalcのPythonマクロをsofficeコマンドで実行できないパターン
BASICと同じやり方で動かそうとしても失敗する。
成果物
LibreOffice.Calc.Macro.Python.Terminal.Failed
Pythonマクロ
~/.config/libreoffice/4/user/Scripts/python/hello_msgbox.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # https://qiita.com/ty21ky/items/eb4bdb71570d1866b35f import uno class Bridge(object): def __init__(self): self._context = XSCRIPTCONTEXT.getComponentContext() self._desktop = XSCRIPTCONTEXT.getDesktop() self._frame = self._desktop.CurrentFrame self._window = self._frame.ContainerWindow self._toolkit = self._window.Toolkit def run_infodialog(self, title='', message=''): msgbox = self._toolkit.createMessageBox(self._window, 'infobox', 1, title, message) msgbox.execute() msgbox.dispose() def hello(): pyuno = Bridge() pyuno.run_infodialog(title='Hello MessageBox', message='LibreOfficeのPythonマクロによる\nメッセージボックス表示テストです。') g_exportedScripts = (hello,)
動作確認
まずはLibreOfficeから実行して、マクロが正常に動作することを確認する。
- LibreOffice Calc を起動する
- メニュー→
マクロ
→マクロを実行
マイマクロ
→hello_msgbox
→hello
実行
ボタンをクリックする
バッチ実行できない
前回のBASIC版と同じように実行しようとしてみたが失敗した。
soffice macro:///Standard.hello_msgbox.hello soffice macro:///hello_msgbox.hello_msgbox.hello soffice macro:///hello_msgbox.hello soffice macro:///MyMacro.hello_msgbox.hello soffice macro:///mymacro.hello_msgbox.hello soffice macro:///Mymacro.hello_msgbox.hello soffice macro:///myMacro.hello_msgbox.hello soffice macro:///マイマクロ.hello_msgbox.hello
vnd.sun.star.script
さらにググると別の方法があるっぽい。だがエラー。
- https://qiita.com/VinceHeart/items/b340a19debcc9e891231
- https://stackoverflow.com/questions/29417113/how-to-call-an-existing-libreoffice-python-macro-from-a-python-script
soffice 'vnd.sun.star.script:hello_msbgox.py$hello?language=Python&location=user'
Python のスクリプト vnd.sun.star.script:hello_msbgox.py$hello?language=Python&location=user の実行中に Scripting Framework エラーが発生しました。 メッセージ: <class 'pythonscript.com.sun.star.ucb.InteractiveAugmentedIOException'>: an error occurred during file opening File "/usr/lib/libreoffice/program/pythonscript.py", line 1062, in getScript mod = self.provCtx.getModuleByUrl( fileUri ) File "/usr/lib/libreoffice/program/pythonscript.py", line 476, in getModuleByUrl lastRead = self.sfa.getDateTimeModified( url )
前回まで
LibreOfficeが参照するPythonマクロの配置パスは以下。
/home/pi/.config/libreoffice/4/user/Scripts/python
端末からPythonを実行するコマンドは以下。
soffice --calc --norestore "--accept=pipe,name=librepipe;urp;" & { sleep 3 ./new_doc.py jobs kill %% }
所感
結局、これまでの長ったらしいコマンドで実行するしかない。
対象環境
- Raspbierry pi 4 Model B
- Raspbian buster 10.0 2019-09-26 ※
- bash 5.0.3(1)-release 学習まとめ
- LibreOffice 6.1.5.2 ※
$ uname -a Linux raspberrypi 4.19.97-v7l+ #1294 SMP Thu Jan 30 13:21:14 GMT 2020 armv7l GNU/Linux