LoginSignup
6
10

More than 3 years have passed since last update.

Power Automate Desktop 「Pythonスクリプトの実行」アクションで最大値 最小値を求める

Last updated at Posted at 2021-01-24

概要

Power Automate Desktopの「Pythonスクリプトの実行」アクションを試してみます。ちょっとした配列の処理は、外部のPythonを呼び出さなくてもこのアクションから実行できると利便性が上がると思い試しました。しかし2021年1月時点ではMicrosoftのDocsやlearnにあまり情報がなくハマりポイントがあったので記録しておきます。

環境など

Windows10 20H2
Power Automate Desktop 2.2.20339.22608
組込みされているPython
image.png
2021年1月時点の情報です。

試したこと

PADの配列から最大値、最小値を求めます。

使ってみる

最初に「Pythonスクリプトの実行」アクションにPythonスクリプトを書いて試してみます。
配列も直接スクリプト内に書いてみます。
image.png
配列が表示されます。
image.png

最大値を求めてみます。
image.png
image.png
問題なく実行出来そうです。

次に、PADで作成した配列を「Pythonスクリプトの実行」に渡して試します。
アクション1行目に「変数の設定」を利用してリストを作成します。
「新しいのリスト作成」アクションを使わなくても1行で配列作成が可能です。
image.png
宛先:の内容です。
%[155, 168, 5456, 551, 156, 599, 256, 636, 266, 4894, 282, 522, 266]%
image.png

「Pythonスクリプトの実行」の変数listbをPADの変数に変えてみます。
image.png
ダメです。ここでハマりました。
image.png
TypeError: int is not iterable 数値は反復できません。??
関数maxを外して実行してみると
image.png
配列ごとを渡したつもりなのですが単体の値になっているようです。

回避策

配列が単体値になってしまうなら、先に配列ごと単体テキストにしてからpythonに渡すことにしました。
カンマ区切りでテキストの結合を行います。
image.png
pythonスクリプトを修正します。
image.png
上手くいきそうです。
image.png
max関数を追加します。
image.png
PADの配列から最大値を取り出すことができました。

image.png
min関数を使えば最小値を取得できます。

PowerAutomateDesktop
SET List TO [155, 168, 5456, 551, 156, 599, 256, 636, 266, 4894, 282, 522, 266]
Text.JoinWithCustomDelimiter List: List CustomDelimiter: $''',''' Result=> JoinedText
System.RunPythonScript PythonCode: $'''listb = [%JoinedText%]
print max(listb)''' ScriptOutput=> PythonScriptOutput ScriptError=> ScriptError
Display.ShowMessage Title: $'''PAD''' Message: $'''%PythonScriptOutput%
%ScriptError%''' Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: True ButtonPressed=> ButtonPressed

まとめ

現時点では直接配列をPythonスクリプトの実行に渡せませんでした。
配列を渡せることで利便性が上がります。

参考

Microsoft learn :Power Automate Desktop でのスクリプトの作成
https://docs.microsoft.com/ja-jp/learn/modules/pad-scripting/

追記

PythonScriptアクションから出力される%PythonScriptOutput%はテキスト型なのですが「テキストを数値に変換」では数値に戻せません。
調べてみるとpython2のprintでの出力は不可視部分に改行コードを含みます。
Screenshot 2021-01-28 110017.jpg
そこでpython scriptにprintを使わずに「sys.stdout.write」を使用しました。

Python2
import sys
listb = [JoinedText]
m = maxlistb
sys.stdout.writestrm))

しかし、改行コードを含まない状態で出力しても数値に変換できませんでした。
Screenshot 2021-01-28 121207.jpg

結局、あまりスマートではありませんが正規表現を使って数値を抽出することにしました。
Screenshot 2021-01-28 122257.jpg

SET List TO [155, 168, 5456, 551, 156, 599, 256, 636, 266, 4894, 282, 522, 266]
Text.JoinWithCustomDelimiter List: List CustomDelimiter: $''',''' Result=> JoinedText
System.RunPythonScript PythonCode: $'''import sys
listb = [%JoinedText%]
m=max(listb)
sys.stdout.write(str(m))''' ScriptOutput=> PythonScriptOutput ScriptError=> ScriptError
Text.RegexParseForFirstOccurrence Text: PythonScriptOutput TextToFind: $'''[0-9]+''' StartingPosition: 0 IgnoreCase: False OccurrencePosition=> Position Match=> Match
Text.ToNumber Text: Match Number=> TextAsNumber

PythonScriptアクションへの入出力は2021年1月現在では少し手間が掛かる方法しか自分は思いつきませんでした。

6
10
1

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
6
10