マクロ/Impress/004
TDF LibreOffice Document Liberation Project Community Blogs Weblate Nextcloud Redmine Ask LibreOffice Donate
説明
現在選択されているシェイプまたはオブジェクトの属性をクリップボードにコピーできると便利です。そのためには、Python を使用すると非常に便利です。なぜなら、文字列をクリップボードにコピーするための一般的なライブラリを簡単に見つけてインポートできるからです。これは、LibreOffice Basic の関数やライブラリを使用して行うのは簡単ではありません(Andrew Pitonyak's OOME book v3[1] には、p.267 のLO Basicでクリップボードにコピーする例が含まれています)。
以下のマクロでは、現在選択されているオブジェクトは、XSCRIPTCONTEXT.getDocument().CurrentSelection[0]
、つまり選択されたオブジェクトの配列の 0 番目の要素を使用してアクセスされます。com.sun.star. drawing.RectangleShape などのシェイプには、FillColor[2] というアトリビュートがあります。色は、次の数学的定義を持つ単一の長整数です:[3][4][5]
- カラー値 [math]\displaystyle{ =R*2^{16} + G*2^8 + B*2^0 }[/math]
ここで、[math]\displaystyle{ R,G,B }[/math] は、赤、緑、青 の8ビット値(0~255)です。したがって、モジュライやその他の数学的演算を使用して、より一般的な RGB 標準に抽出し、次に16進数のカラーコードに抽出する関数を作成する必要があります。
FillColor アトリビュートにアクセスした後、スクリプトはクリップボードにコピーするために pyperclip[6] モジュールを使用します。pyperclipをインストールするには、実行時に
pip3 install --user pyperclip
また、LibreOffice が pyperclip のインストールを見つけられない場合は、パッケージのパスを追加する必要がある場合があります。たとえば、次のようになります。
import sys
sys.path.append('~/.local/lib/python3.8/site-packages/')
import pyperclip
Code
Python
# -*- coding: utf-8 -*-
import uno
from com.sun.star.awt import MessageBoxButtons as MSG_BUTTONS
# Adjust and uncomment the following if the path of your packages does not load:
#import sys
#sys.path.append('~/.local/lib/python3.8/site-packages/')
import pyperclip
def copy_hex_color(*args): # 関数定義に '*args' を含める必要があります
'''選択した図形のFillColor属性の16進数の色をクリップボードに抽出するLibreOfficeマクロ。'''
thisComponent = XSCRIPTCONTEXT.getDocument()
try:
sel = thisComponent.CurrentSelection[0] # 現在選択されているシェイプを取得
hex_color = cval2hex(sel.FillColor)
pyperclip.copy(hex_color)
#msgbox( 'Copied "' + str(sel.FillColor) + '" as "' + pyperclip.paste() + '" !' ) # テスト用のコメント
except:
msgbox('Exception occured!')
def cval2hex(c):
'''カラー値を16進数に変換'''
# カラー値から8ビット(0-255)RGBを抽出
red = ( c - c % 2**16 ) /2**16;
green = ( c % 2**16 - c % 2**8 ) /2**8;
blue = ( c % 2**8 ) /2**0;
h = '#%02x%02x%02x' % (int(red), int(green), int(blue)) # RGB to hex
return h[1:] # 返された文字列内のハッシュ記号を取り除く
def rgb2cval(r,g,b):
'''8ビット(0~255)のRGBコンポーネントからカラー値を返します'''
return (r * 2**16) + (g * 2**8) + (b * 2**0)
def msgbox(message, title='LibreOffice', buttons=MSG_BUTTONS.BUTTONS_OK, type_msg='infobox'):
'''https://wiki.documentfoundation.org/Macros/Python_Guide/Useful_functions#MsgBox'''
'''Create message box
type_msg: infobox, warningbox, errorbox, querybox, messbox
https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1awt_1_1XMessageBoxFactory.html
'''
toolkit = create_instance('com.sun.star.awt.Toolkit')
parent = toolkit.getDesktopWindow()
mb = toolkit.createMessageBox(parent, type_msg, buttons, title, str(message))
return mb.execute()
def create_instance(name, with_context=False):
'''https://wiki.documentfoundation.org/Macros/Python_Guide/Useful_functions#Create_instances'''
CTX = uno.getComponentContext()
SM = CTX.getServiceManager()
if with_context:
instance = SM.createInstanceWithContext(name, CTX)
else:
instance = SM.createInstance(name)
return instance
# LibreOfficeマクロのナビゲーションツリーにcopy_hex_color関数のみが表示されるようにします。
g_exportedScripts = (copy_hex_color,)
LO Basic
tbc
ODP ファイルのダウンロード
メモ
- ↑ https://www.pitonyak.org/oo.php
- ↑ https://api.libreoffice.org/docs/idl/ref/servicecom_1_1sun_1_1star_1_1drawing_1_1FillProperties.html#aba11c9f14e66ff6ff10fa57c907d178d
- ↑ https://help.libreoffice.org/latest/en-US/text/sbasic/shared/00000003.html
- ↑ https://help.libreoffice.org/latest/en-US/text/sbasic/shared/03010305.html
- ↑ https://wiki.documentfoundation.org/Macros/Impress/001#Python
- ↑ https://pypi.org/project/pyperclip