LibreOffice マクロを Ubuntu + Python で作る
拡張を作るための基礎知識
拡張功能是以.oxt文件形式存在的,并支援以下語言。
-
- Basic
-
- Python
-
- JavaScript
-
- Java
-
- BeanShell
- C++
今回は、python で試してみます。
环境
-
- Ubuntu 22.04 LTS
- LibreOffice Ubuntu package version: 1:7.3.7-0ubuntu0.22.04.3
Python バインディングのインストール
$ sudo apt install libreoffice-script-provider-python
当重新启动LibreOffice时,可以在宏管理中看到Python。

Python宏的存储位置是确定的,对于Ubuntu系统来说,看起来是这样的。
-
- ユーザごとの設定
~/.config/libreoffice/4/user/Scripts/python
ユーザ全員用の設定
/usr/lib64/libreoffice/share/Scripts/python
本次将按照用户进行设置。
- ~/.config/libreoffice/4/user/Scripts/python
然而,在文件中所述
- ~/.libreoffice/4/user/Scripts/python
在撰写这篇文章之前的实验中,我实际上已经有了一个名为~/.libreoffice的文件夹,但在实验过程中,我不知不觉中删除了它。
- ~/.config/libreoffice
生活已经发生了变化。真奇怪。
现在,虽然~/.config/libreoffice/4/user目录存在,但Scripts/python目录却不存在,我们需要创建它。
$ mkdir -p ~/.config/libreoffice/4/user/Scripts/python
示例如下的程式
「Write Your First Python Macro in LibreOffice」
我将尝试直接运行程序。
# HelloWorld python script for LibreOffice Calc
#
# This file is part of the DebugPoint.com tutorial series for Python Macros in LibreOffice
#
#
import uno
def HelloWorldPythonCalc():
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet =oDoc.getSheets().getByIndex(0)
oCell = oSheet.getCellByPosition(0,0)
oCell.String = 'Hello World via Python'
return None
请将上述内容保存为HelloWorldCalc.py文件。
考试
LibreOffice の表計算を起動し、「ツール」-「マクロ」-「マクロを実行」とすると以下のように出てきます。

「 HelloWorldPythonCalc」を選んで実行すると下記のように表示されます。

接下来
暫時驗證了一下操作,接下來按照以下方式進行製作。
「LibreOffice マクロ:Pythonでのプログラム例集」
https://qiita.com/nanbuwks/items/1f25e8839089eaefd6d4
「Python で LibreOffice 拡張をステップバイステップで作ってみる」
https://qiita.com/nanbuwks/items/53861bac26c521c4b549