LoginSignup
0
0

Power Automate Desktopでpyファイルを実行

Posted at

書いた背景

Power Automate DesktopでWebスクレイピングや、文字列処理をしたいとき組み込みの関数では工数が多いor実現できないことが多い。
「pythonを実行する」アクションではpython2しか使用できず、モジュールのimport設定も面倒

解決策

DOSコマンド実行、からpyファイルを実行する

引数無し

シンプル

image.png

1.py
print("test")
  • .pyファイルとそのファイルが存在するフォルダを指定して実行
  • print文で記載した内容がCommandOutputに文字列として格納されます

image.png

複数print

2.py
print("test1")
print("test2")
print(3)

image.png

  • 複数print文を記載すると改行ありで内容がテキストとして取得できます

辞書型

3.py
data = {"a": 1, "b": 2, "c": 3, 4: "four"}
print(data)

image.png
image.png

  • 辞書型としてprint出力し、得られた値をJSON→オブジェクト変換することで、カスタムオブジェクトとして使用できます
  • 1対1のデータがあるときに重宝する

リスト

4.py
data = []
for i in range(10):
    data.append(i)
print(data)

image.png

  • 辞書型の際と同じくJSONをオブジェクト変換でリスト取得可能

引数あり

引数少なめ

5.py
import sys

args = sys.argv
print(args[0])
print(args[1])

image.png
image.png

  • args[0]に実行ファイル名(full path)
  • args[1]以降に空白区切りで実行時に記載した値がテキストとして表示される

引数がリスト

6.py
import sys

txt = sys.argv[1]
data = eval(txt)
print(data[0])

image.png

  • リストに文字列が入っている場合は全体をダブルクオーテーションで囲み、文字列自体はシングルクオーテーションで囲む

引数が辞書型

7.py
import json
import sys

txt = sys.argv[1]
jsonData = txt.replace("'", '"')
data = json.loads(jsonData)
print(data["a"])

image.png

  • 全体をダブルクオーテーションで囲み、Keyをシングルクオーテーションで囲む
  • windowsのdosコマンドでは"が消える仕様のため、一旦'で囲んでおいて"に置換することでJSON形式として認識させている

PADコマンド間でのデータの受け渡し

Excel→Python

image.png
image.png

8.py
import json
import sys

print(sys.argv)
txt = sys.argv[1]
jsonData = txt.replace("'", '"')
data = json.loads(jsonData)
print(data["ExcelData"][0]["name"])

4工程も含まれてしまって少々面倒ですが、実現自体は可能です。

  1. 変数の設定
    • あえて変数に{'ExcelData': %ExcelData%}を格納することでカスタムオブジェクトを作成します
  2. カスタムオブジェクトをJSONに変換
    • JSON(テキスト)に変換することで、DOSコマンド実行時に渡せる形に変更します
  3. テキストを置換する
    • "'に変更します
  4. DOSコマンドの実行
    • 8.py "%json%"
    • 上記のように'で囲って指定することで「引数が辞書型」の時に指定した形を実現します

Python→Python

image.png

PAD flow
@@copilotGeneratedAction: 'False'
Scripting.RunDOSCommand.RunDOSCommand DOSCommandOrApplication: $'''9.py''' WorkingDirectory: $'''C:\\Users\\xxxx\\programs\\Qiita\\pad''' StandardOutput=> CommandOutput StandardError=> CommandErrorOutput ExitCode=> CommandExitCode
Text.Replace Text: CommandOutput TextToFind: $'''\"''' IsRegEx: False IgnoreCase: False ReplaceWith: $'''\'''' ActivateEscapeSequences: False Result=> json
@@copilotGeneratedAction: 'False'
Scripting.RunDOSCommand.RunDOSCommand DOSCommandOrApplication: $'''10.py \"%json%\"''' WorkingDirectory: $'''C:\\Users\\xxxx\\programs\\Qiita\\pad''' StandardOutput=> outputdata StandardError=> CommandErrorOutput ExitCode=> CommandExitCode

9.py
data = {"a": 1, "b": 2, "3": "data"}
print(data)
10.py
import json
import sys

txt = sys.argv[1]
jsonData = txt.replace("'", '"')
data = json.loads(jsonData)
print(data["3"])
  • pythonを実行して返ってくるのはテキスト型→つまりJSON
  • シンプルに"'に置換して渡せばよい

Python→Excel

image.png

@@copilotGeneratedAction: 'False'
Scripting.RunDOSCommand.RunDOSCommand DOSCommandOrApplication: $'''11.py''' WorkingDirectory: $'''C:\\Users\\xxxx\\programs\\Qiita\\pad''' StandardOutput=> CommandOutput StandardError=> CommandErrorOutput ExitCode=> CommandExitCode
Variables.ConvertJsonToCustomObject Json: CommandOutput CustomObject=> object
Excel.LaunchExcel.LaunchUnderExistingProcess Visible: True Instance=> ExcelInstance
Excel.WriteToExcel.WriteCell Instance: ExcelInstance Value: object['area1'] Column: $'''A''' Row: 1
Excel.WriteToExcel.WriteCell Instance: ExcelInstance Value: object['area2'] Column: $'''B''' Row: 1
11.py
data = {"area1": [], "area2": []}
data["area1"] = [i for i in range(10)]
data["area2"] = [i * 2 for i in range(10)]

print(data)

image.png

  • Python側で、Excelに渡したいデータをリスト型で定義しておく
  • Pythonから返ってきたテキストをカスタムオブジェクトに変換して、そのままExcelに書き込めばOK
  • リストの値は縦に入力される

まとめ

ローカルでpyファイルを実行することで、モジュールの制限なし&Python3で実行することができました。
PADでのデータのやり取りも問題なくできそうなので、ぜひ活用してみてください!

0
0
0

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