下記URLで LibreOffice(calc) で Python マクロが使えるのを知りました。
そこで、
awscli の describe した json データを LibreOffice に読み込んでみることにしました。
LibreOffice は Windows10 にインストールしたものを使用します。
LibreOffice のバージョンは 5.1.5 です。
Python は LibreOffice同梱のもを使用します。
1.JSONデータの準備
まず、試験用にAMIの一覧を describe して、json データを取得します。
全部だと多いので、 AMI名に "centos6" を含むAMIだけに絞り込みます。
$ aws ec2 describe-images --filters 'Names=name,Values=centos6' --output json > describe-images.json
この json ファイルを、LibreOfficeがインストールされた Windowsマシン上に保管します。
2. Python マクロの準備
JSONデータは、"C:\data\describe^images.json" に格納されていることとします。
一覧を作成するシートの名称は、"Sheet1" とします。
ソースコードは以下のとおり。
#coding:utf-8 import json def amiList(): f = open(r"C:\data\describe-images.json","r") data = json.load(f) f.close() doc = XSCRIPTCONTEXT.getDocument() sheet = doc.getSheets().getByName('Sheet1') for i,e in enumerate(data['Images']): p = i + 2 sheet.getCellRangeByName('A'+str(p)).Value = i + 1 sheet.getCellRangeByName('B'+str(p)).String = str(e['VirtualizationType']) sheet.getCellRangeByName('C'+str(p)).String = str(e['Name']) sheet.getCellRangeByName('D'+str(p)).String = str(e['Hypervisor']) sheet.getCellRangeByName('E'+str(p)).String = str(e['ImageId']) sheet.getCellRangeByName('F'+str(p)).String = str(e['RootDeviceType']) sheet.getCellRangeByName('G'+str(p)).String = str(e['Architecture']) sheet.getCellRangeByName('H'+str(p)).String = str(e['BlockDeviceMappings'][0]['DeviceName']) sheet.getCellRangeByName('I'+str(p)).String = str(e['BlockDeviceMappings'][0]['Ebs']['DeleteOnTermination']) sheet.getCellRangeByName('J'+str(p)).String = str(e['BlockDeviceMappings'][0]['Ebs']['VolumeSize']) sheet.getCellRangeByName('K'+str(p)).String = str(e['BlockDeviceMappings'][0]['Ebs']['VolumeType']) sheet.getCellRangeByName('L'+str(p)).String = str(e['BlockDeviceMappings'][0]['Ebs']['Encrypted'])このソースをLibreOfficeから実行するには、ファイルに保存してPythonマクロ用のフォルダに格納します。
C:\Users\<ユーザ名>\AppData\Roaming\LibreOffice\4\user\Scripts\python以下のファイル名で保存します。
C:\Users\<ユーザ名>\AppData\Roaming\LibreOffice\4\user\Scripts\python\TestScript.py
3. Python マクロの実行
LibreOffice(calc) を起動します。
[ツール] > [マクロ] > [マクロの実行] を選択します。
下図の[マクロセレクター]が表示されます。
[マクロ] をクリックして、展開します。
ライブラリ欄の[TestScript]を選択すると、マクロ名欄に [amiList]が表示されるので選択して、[実行]ボタンをクリックします。
下図のように JSONデータが一覧表示されます。
エクセルのマクロで Jsonデータを入力したい場合は、下記URLが参考になります。