2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LibreOfficeのPythonマクロを使ってPythonからLibreOfficeを操作する

Last updated at Posted at 2018-02-26
環境

  Ubuntu Studio 17.10
  LibreOffice6.0

LibreOfficeの起動方法を変更しました。

LibreOfficeを通信状態で起動するのに

$ libreoffice6.0 --calc --norestore --accept=socket,host=localhost,port=2002';'urp;

で起動していましたが、「libreoffice6.0」は「soffice」のスクリプトで通信に不具合が起きる可能性があると、何処かのHPで見ました。

/opt/libreoffice6.0/program/soffice --calc --norestore --accept=socket,host=localhost,port=2002';'urp;

と「soffice」で起動した方が良いらしいです。
下記の表記を変更しました。(パスはVer等でことなるので変更してください。)

この記事では、「unopy.py」が必要ですが、一番下に記載したコードでは必要無いので、先に一番下のコードで試してください。
http://aaabbb-200904.hatenablog.jp/entry/20110212/1297519086

aaabbb_200904の日記

さんのブログの中の ●使い方 の6行下にあるコードを「適当な名前.py」で
/home/ユーザ名/.config/libreoffice/4/user/Scripts/python/ (Ubuntuの場合)
C:\Users\ユーザ名\AppData\Roaming\LibreOffice\4\user\Scripts\python (Windowsの場合)
に保存します。

ただし、1行目の「$ cat hello.py」は削除します。

これを、「マクロを実行」で実行します。
結果:A!セルに「Test」と入力されます。

Pythonからこのマクロを実行します

/opt/libreoffice6.0/program/python 適当な名前.py
で実行します。(Ver等によりPathが違う場合があるので確認してください。)
上記は「適当な名前.py」が保存されているフォルダから実行する場合です。
その他のフォルダから実行する場合はパスを追加してください。

多分、「import unopy」でエラーになると思います。

P--Q さんのブログ

このブログに
「unopy.py」のコードがありますので、
/opt/libreoffice6.0/program/python-core-3.5.4/lib/python3.5/site-packages
か、このフォルダがなければ
/opt/libreoffice6.0/program/python-core-3.5.4/lib/site-packages
に保存してください。
(PathはVer等により違いますので変更してください。)

このフォルダはスーパーユーザー(Windowsで言うアドミニストレータ)しか操作出来ません。

私は/homeで作成し、sudo mv ./unopy.py /opt/libreoffice6.0/program/python-core-3.5.4/lib/site-packages/
で移動しています。(sudoがスーパーユーザーのコマンドです。)

これで、エラーは出なくなると思いますがLibreOfficeには反応が無いと思います。
外部から、LibreOfficeに通信出来る状態で起動していないからです。

端末(Windowsでいうコマンドプロンプト)から、

$ /opt/libreoffice6.0/program/soffice --calc --norestore --accept=socket,host=localhost,port=2002';'urp;StarOffice.ServiceManager

(Calcの場合。--calcを削除するとLibreOfficeのメニュー画面が開きます。)

この状態で、先程のPythonから実行するとマクロが実行されてA1セルに「Test」と入力されます。

LibreOfficeを起動するショートカットの作成方法

いちいち、端末で入力するのが面倒な場合はショートカットを作成します。

テキストエディタで下記のコードを「適当な名前.desktop」で保存します。
「適当な名前.desktop」に実行権限を与えます。
Ubuntu Studioの場合はファイルマネージャーで
プロパティ ー> アクセス権 ー> プログラムにチェックを入れる

これが出来ない場合は、端末から
$ chmod +x 適当な名前.desktop

desktop
[Desktop Entry]
Name=Python2Libre
Comment=PythonからLibreOfficeを操作する
Exec=/opt/libreoffice6.0/program/soffice --calc --norestore --accept=socket,host=localhost,port=2002';'urp;StarOffice.ServiceManager

Icon=/home/ty/bin/icon/application.png
Terminal=false
Type=Application

Nameはショートカットに表示される名前です
Commentはコメントです。
Iconはシステムのどこかにあるアイコンを/homeにコピーして使っています。
パス・ファイル名は適当に変更してください。

海外のサイトで上記と同じことをやっているのですが、Pythonのコードが違うので参考程度で

上記では「unopy.py」が必要ですが、この記事ではLibreOfficeのUNOを使用するので、「unopy.py」は不要だと思います。

HelloWorld.py
import socket  # only needed on win32-OOo3.0.0
import uno

# get the uno component context from the PyUNO runtime
localContext = uno.getComponentContext()

# create the UnoUrlResolver
resolver = localContext.ServiceManager.createInstanceWithContext(
				"com.sun.star.bridge.UnoUrlResolver", localContext )

# connect to the running office
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager

# get the central desktop object
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)

# access the current writer document
model = desktop.getCurrentComponent()

上記以外に下記のコードも追加で必要です。
Lalc用

Calc
# access the active sheet
active_sheet = model.CurrentController.ActiveSheet

# access cell C4
cell1 = active_sheet.getCellRangeByName("C4")

# set text inside
cell1.String = "Hello world"

# other example with a value
cell2 = active_sheet.getCellRangeByName("E6")
cell2.Value = cell2.Value + 1

Writer用

Writer
# access the document's text property
text = model.Text

# create a cursor
cursor = text.createTextCursor()

# insert the text into the document
text.insertString( cursor, "Hello World", 0 )
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?