LoginSignup
8
8

More than 1 year has passed since last update.

Power Automate Desktopがなんとなく使える講座 番外編 Pythonのライブラリと値の受け渡し

Last updated at Posted at 2021-05-10

(執筆 2021/5/10)
 Power Automate DesktopでPythonが使えるようになっていますが、どのような環境なのか確認します。

Python環境

Pythonのバージョン

Pythnスクリプトの実行
import sys
print(sys.version)
出力
2.7.9 (IronPython 2.7.9 (2.7.9.0) on .NET 4.0.30319.42000 (64-bit))
Pythonスクリプトの実行
import sys
print(sys.platform)
出力
cli

 IronPython2.7.9は2018年10月9日にリリースされました。Python2のサポートは終了しており、IronPython3.4が2021年4月20日にリリースされているので、近々メジャーバージョンが変わると思います。

ビルトインモジュール

 コンパイル時に組み込まれたPythonモジュールです。

Pythonスクリプトの実行
import sys
print(sys.builtin_module_names)
モジュール名
imp
sys
unicodedata
_ast
clr
exceptions
future_builtins
array
binascii
bz2
cmath
copy_reg
cPickle
cStringIO
datetime
errno
gc
_md5
_sha
_sha256
_sha512
itertools
marshal
math
mmap
msvcrt
nt
operator
pyexpat
re
select
signal
thread
time
winsound
xxsubtype
zipimport
zlib
_bisect
_codecs
_collections
_csv
_ctypes
_ctypes_test
_functools
_heapq
_io
_locale
_random
_socket
_sre
_ssl
_struct
_subprocess
_warnings
_weakref
_winreg)

Pythonのインストールディレクトリ

 Python のプラットフォーム依存なファイルがインストールされているディレクトリ名です。

Pythonスクリプト実行
import sys
print(sys.exec_prefix)
出力
file:///C:/Program Files (x86)/Power Automate Desktop/IronPython.DLL

Pythonの外部モジュール使用

 組み込まれていないモジュールを使いたい場合は、IronPythonのサイトから IronPython.2.7.11.zip をダウンロードして解凍します。
 Pythonスクリプトの実行で、モジュールフォルダパスに、解凍した中にあるLibフォルダの場所を入れれば、モジュールを使うことができます。
image.png
 jsonライブラリが使用できると、Pythonとの値の受け渡しがjsonでできるようになりそうです。

ironpkg は使えない

 その他、Numpyなどのパッケージをインストールするには ironpkg-1.0.0.py を使って下記のように行うという情報が多数あります。(ここ
 IronPython.2.7.11\net45 のフォルダにironpkg-1.0.0.pyをセットし、以下のコマンドを実行します。

ipy -V PythonContext 2.7.0.40 on .NET 4.0.30319.225
ipy ironpkg-1.0.0.py --install
..\ironpkg scipy numpy-2.0.0b2-1.egg

 以下のメッセージが出て、成功しません。無くなってるサイトのフィルを見に行こうとしてるためだと思います。
image.png

pipコマンドをインストール

 Pip in IronPython 2.7.5によると、以下のコマンドでpipがインストールできます。

ipy -X:Frames -m ensurepip

 ライブラリとしては、html5libがインストールできます。

ipy -X:Frames -m pip install html5lib

 それ以外のNumPyとか、Pandasなどはインストールできませんでした。

IronPython 2.7 のライブラリを使う

 Pythonスクリプトの実行で指定するモジュールパスは、1つだけなら、フォルダ名をそのまま入力すればよいのですが、複数を使いたい場合は、リストにして渡します。
image.png
image.png
image.png
image.png
 

Pythonへの値の渡し方

 値が1つの場合、Pythonのコードの中で %変数名% を記述すれば、値を受け取ることができます。

image.png

 値がリストの場合は改行を含む文字列として渡されますので、"""%変数名%""" として split() で分割してリストに戻します。

image.png

Pythonからの値の受け取り方

 Pythonのprintで出力した値は、Power Automate Desktopの変数でテキスト値として受け取ることができます。

 リスト値を受け取る場合は、まずPython側で値を改行で区切って出力します。
image.png
 Pythonからの出力は、最後に余分な改行があるので、トリミングした後にテキストの分割を行うと、リスト値になります。
image.png
image.png

VBScriptのバージョン

VBScriptの実行
WScript.Echo "Major: " & ScriptEngineMajorVersion & vbCrLf & _
             "Minor: " & ScriptEngineMinorVersion & vbCrLf
Major: 5 Minor: 8
8
8
2

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
8
8