前の関連記事:LibreOffice(42)UNOオブジェクトの属性1:正規表現パターンを作成
前回全属性を正規表現で個別に取り出すことに成功したので今度はそれを見やすくして表示します。
メッセージボックスに結果を表示する
#libreoffice43.py
import re
def libreoffice43():
regex = r"\((.+?)\)|(\w+?)=({)?([^}]+?)(?(3)}|,)" # 正規表現パターン。
test_string = str(XSCRIPTCONTEXT.getDesktop()) # デスクトップオブジェクトを文字列で取得。
match = re.findall(regex, test_string) # 正規表現で抽出した結果を取得。タプルのリスト。
output = list() # 出力内容を入れるリストを作成。
for i in match: # タプルmatchの要素の各リストに対して
if i[0]: #0番目の要素について
output.append("継承している型")
output.append(i[0])
elif i[1] == "implementationName":
output.append("実装名" + i[1])
output.append(i[3])
elif i[1] == "supportedServices":
output.append("使えるサービス"+i[1])
list1 = i[3].split(",") # ,を区切りにリストに変換。
list1.sort() # 昇順に並び替え。
output.append("\n".join(list1)) # 並び替えた要素を改行文字で結合。
elif i[1] == "supportedInterfaces":
output.append("使えるインターフェイス"+i[1])
list1 = i[3].split(",")
list1.sort()
output.append("\n".join(list1))
txt = "\n".join(output) # 各要素を改行文字で結合。
message("オブジェクトの属性", txt) # メッセージボックスに表示。
def message(title="", msg=""):
win = XSCRIPTCONTEXT.getDesktop().getCurrentFrame().getContainerWindow()
toolkit = win.getToolkit()
msgbox = toolkit.createMessageBox(win, "MESSAGEBOX", 1, title, msg)
msgbox.execute()
g_exportedScripts = libreoffice43, #マクロセレクターに表示させる関数をタプルで限定する。
if __name__ == "__main__": # これ以下はオートメーションでのみ必要。
import unopy
XSCRIPTCONTEXT = unopy.connect()
if not XSCRIPTCONTEXT:
print("Failed to connect.")
import sys
sys.exit(0)
libreoffice43()
26行目でCalc(2)課題2:選択範囲の行列番号をメッセージボックスに表示のメッセージ関数を使っています。LibreOffice4.2以降でcreateMessageBoxの引数が変更になっていますので4.1以前のときは変更が必要です。
(以下図中の「オブジェクトの型」は「継承している型」のことです。画像を作るときは「オブジェクトの型」と誤解していました。)
Calcで実行した結果です。ちょっとみにくいですね。
#libreoffice43.py
import re
def libreoffice43():
regex = r"\((.+?)\)|(\w+?)=({)?([^}]+?)(?(3)}|,)"
test_string = str(XSCRIPTCONTEXT.getDesktop())
match = re.findall(regex, test_string)
output = list()
for i in match:
if i[0]:
output.append("継承している型")
output.append(i[0])
else:
output.append(i[1].replace("implementationName", "実装名(" + i[1] + ")").replace("supportedServices", "使えるサービス(" + i[1] + ")").replace("supportedInterfaces", "使えるインターフェイス(" + i[1] + ")"))
if "," in i[3]: # 要素の中に,があるとき。つまり複数の項目があるとき。
list1 = i[3].split(",") # リストに変換。
list1.sort()
output.append("\n".join(list1))
else:
output.append(i[3])
txt = "\n".join(output)
message("オブジェクトの属性", txt) # メッセージボックスを表示。
def message(title="", msg=""):
win = XSCRIPTCONTEXT.getDesktop().getCurrentFrame().getContainerWindow()
toolkit = win.getToolkit()
msgbox = toolkit.createMessageBox(win, "MESSAGEBOX", 1, title, msg)
msgbox.execute()
g_exportedScripts = libreoffice43,
整理してみました。オートメーメーションの部分は省略しています。elifの羅列に変えてreplaceを連結して文字置換に変更して丸括弧も追加しました。
13行目をもう少し美しくしたいですね。
#libreoffice43.py
import re
def libreoffice43():
regex = r"\((.+?)\)|(\w+?)=({)?([^}]+?)(?(3)}|,)"
test_string = str(XSCRIPTCONTEXT.getDesktop())
match = re.findall(regex, test_string)
output = list() # 結果を入れるリストを生成。
for i in match:
if i[0]: # 0番目の要素が存在するとき。
list1 = ["継承している型", "\t"+i[0]]
else:
dic = {"implementationName": "実装名", "supportedServices": "使えるサービス", "supportedInterfaces": "使えるインターフェイス"} # 辞書の作成。
atr0 = "".join([dic[i[1]], "(", i[1], ")"]) # 辞書を使って文字列の置換をする。
if "," in i[3]: # 要素にカンマ,があるとき。
list0 = i[3].split(",") # カンマを区切りにしてリストを生成。
list0.sort() # リストの要素を昇順に並び替え。
list1 = [atr0]
list1.extend(["".join(["\t", j]) for j in list0]) # list0の各要素の前にタブ\tを追加。
else:
list1 = [atr0, "\t"+i[3]]
list1.append("") # 空白行の生成のために空の要素を最後に追加。
output.extend(list1)
txt = "\n".join(output) # リストoutputの全要素を改行文字\nでつなげる。
message("オブジェクトの属性", txt) # メッセージボックスを表示。
def message(title="", msg=""):
win = XSCRIPTCONTEXT.getDesktop().getCurrentFrame().getContainerWindow()
toolkit = win.getToolkit()
msgbox = toolkit.createMessageBox(win, "MESSAGEBOX", 1, title, msg)
msgbox.execute()
g_exportedScripts = libreoffice43,
辞書(4.10. マッピング型 5.5. 辞書)を使いました。list.append(x)が続くのでリストを生成してからlist.extend(L)で要素をまとめて追加するようにしました。(5.1. リスト型についてもう少し)
パフォーマンスがよくなるかは知りませんが、なるべく一行にまとめるのが好きなので、、、
空白行やタブをいれて見やすくしてみました。
フォントを太字にしてみたりもしたかったのですがやり方がわかりませんでした。
参考にしたサイト
4.10. マッピング型
Python 標準ライブラリの辞書の解説。
5.5. 辞書
Python チュートリアルの辞書の解説。
5.1. リスト型についてもう少し
リストオブジェクトのすべてのメソッドの解説。



0 件のコメント:
コメントを投稿