Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Running a Python script within Power Automate Desktop |
---|
How can you pass a value from Power Automate Desktop to a Python script, and then get the results back? This blog explains how. |
In this blog
This blog shows how to run a Python script to process a string of text held in a Power Automate Desktop (PAD) variable:
Our test variable holds a selection of 60s songs - we want to pick out the ones recorded by the Beatles.
Our Python script should pick out the ones by the Fab Four, and write these songs to a text file:
The final output of the program.
Adding a Python script task is easy enough in PAD:
The action to run a Python script.
Here's the start of the Python script:
You can surround the PAD variable with three double quotation marks. I've no idea if this is the only way to do this, but it certainly works!
This is what the result might look like:
The syntax looks a bit fussy - and is!
Here's my Python script to pick out the Beatles songs:
The script produces a Python list of Beatles songs stored in the variable BeatlesSongs.
Here's the code, should you wish to copy it:
# create a string variable containing a few lines
song_list = """%SongList%"""
# split this
songs = song_list.splitlines()
# create new list to pass back
beatles_songs = []
# build up list of words containing The Beatles
for num, song in enumerate(songs):
if "beatles" in song.lower():
beatles_songs.append(songs[num])
# return this to Power Automate Desktop
print(beatles_songs)
PAD doesn't recognise a Python list as a data table, so we need to do some work:
This may be a list to Python, but to PAD it's just a string of characters.
You could turn this into a PAD data table using this series of actions:
These actions remove any leading and trailing spaces, then extract the string of text starting at character number 3 (so omitting the leading [ and ' characters) and ending prematurely (hence omitting the ] and ' at the end). The Split text action then splits the text into separate lines.
Here are the parameters for the Get subtext action:
Use the Length property of the variable to find how many characters to extract.
And here are the parameters for the Split text action:
Create a new item wherever there's a comma (preceded by an apostrophe, and followed by a space and apostrophe).
Finally, you can loop over the songs, writing each to a text file:
The details of each action aren't shown here, but the commands should be straightforward.
If anybody knows of a better way to do the things shown in this blog, please let me (and hence everyone reading it) know!
Some other pages relevant to the above blog include:
From: | Duesenberger |
When: | 18 Jul 24 at 05:52 |
Thanks for this blog post. I tried to recreate the example, but you lost me here:
Translating the Python output to a data table
I run the Python program where the last line is this:
# return this to Power Automate Desktop
print(beatles_songs)
A variable is produced with the output: PythonScriptOutput3
But a DisplayMessge with the variable in it shows nothing.
From: | Andy B |
When: | 18 Jul 24 at 09:23 |
It's really hard to tell what the problem might be, but I've left this post up just in case any other reader can immediately spot the problem?
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.