LibreOffice CalcをPythonマクロで動かしてみる(情報少ない)

LibreOfficeをPythonで操作

LibreOfficeのマクロはBasicが有名ですが、「Python」でも動かすことできます。
情報は非常に少ないです。

LibreOffice 入手先:

まず、おまけでついてるPythonを読む

入ってる場所は:~\LibreOffice\share\Scripts\python\HelloWorld.py

LibreOfficeをインストールしたところに入っています。

  desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()

    # Check whether there's already an opened document.
    # Otherwise, create a new one
    if not hasattr(model, "Text"):
        model = desktop.loadComponentFromURL(
            "private:factory/swriter", "_blank", 0, ())

    # get the XText interface
    text = model.Text

    # create an XTextRange at the end of the document
    tRange = text.End

    # and set the string
    tRange.String = "Hello World (in Python)"

    return None

見てると頭痛くなります。

Pythonは自分でフォルダーの中に入れる?

pythonの実行方法:

タブメニューの「ツール」>「マクロ」>「マクロ管理」>「python」と開きます。

pythonスクリプトを動かす

作ったマクロの表示と保存場所

python保存場所

赤枠内は、次のフォルダのなかに入れたものです。

C:\Users\~~\AppData\Roaming\LibreOffice\4\user\
上記フォルダの下に「Scripts\python\」を作ってください。最初フォルダーはありません。
その中にテストの「~.py」を入れます。
 

サンプルコード (Calc)セルに文字書き込み

import uno

def my_first_macro_calc():
    doc = XSCRIPTCONTEXT.getDocument()
    cell = doc.Sheets[0]['A1']  # com.sun.star.sheet.XSpreadsheetDocument
    cell.setString('Hello World in Python in Calc')
  return

説明サイト:

 
とにかく情報が少ない
 

MsgBoxの表示 WEB上での拾い物

読んでも、ちんぷんかんぷん
 

Pythonマクロ説明サイト

コメント

タイトルとURLをコピーしました