LoginSignup
3
1
新規開発や新技術の検証、導入にまつわる記事を投稿しよう!

LibreOffice マクロを Ubuntu + Python で作る

Last updated at Posted at 2023-07-14

拡張を作るための基礎知識

拡張は .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が現れます。

image.png

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」
https://www.debugpoint.com/first-python-macro-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 の表計算を起動し、「ツール」-「マクロ」-「マクロを実行」とすると以下のように出てきます。

image.png

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

image.png

続き

とりあえず動作確認はできたので、以下のように作っていく

「LibreOffice マクロ:Pythonでのプログラム例集」
https://qiita.com/nanbuwks/items/1f25e8839089eaefd6d4

「Python で LibreOffice 拡張をステップバイステップで作ってみる」
https://qiita.com/nanbuwks/items/53861bac26c521c4b549

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1