564 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls 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. |
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:
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 2023. All Rights Reserved.