web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Need Help Using Run Po...
Power Automate
Answered

Need Help Using Run PowerShell Script To Run a Python 3 file

(0) ShareShare
ReportReport
Posted on by 431

I need Python 3 because I'm using a PDF extraction package (PyMuPDF) that won't run in Python 2. This script runs on a standalone basis in PyCharm and retrieves an account number from a PDF document. The script and my flow are shown below.. I am not sure I am passing the variables correctly. I have never done this before. When I execute the script when I get to the display Message action I get a popup asking me to select an application to open a file called Steven (That's my first name) After I select Notepad a document with a lot of log info opens which I am not interested in. A dialog box does open that was supposed to display the result (account number) but it is empty.  I made a short (2-minute) screen capture video to demo what's happening.  There was no script error. Not sure how to proceed. TIA

https://youtu.be/lu-_xeXigaA

 

 

 

 

 

 

 

import fitz
import re
import sys

doc = sys.argv[3]
print(doc)

with fitz.open(doc) as pdf:
 text = ""
 for page in pdf:
 text += page.get_text()

pattern = r'(\d{3}[-]?\d{3}[-]?\d{3}[-]?\d{4}|NO\.\d{13};)'
matches = re.findall(pattern, text)

if matches:
 result = matches[0].replace("-", "").replace("NO.", "").replace(";", "")
 print(result)
else:
 print("No match found")

 

 

I have the same question (0)
  • Steve48 Profile Picture
    431 on at

    OP Note, I deleted the print(doc) line (it was just supposed to print the file path) but the behavior is still the same. I still get the log file,

  • Steve48 Profile Picture
    431 on at

    The flow doesnt seem to be visible

    Steve48_0-1694465908993.png

     

     

  • Verified answer
    Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    First of all, why can't you just do Extract text from PDF Parse text with regular expressions? That seems to be essentially what you're doing with your Python script. 

     

    Also, can you please share the PowerShell script, too? It seems like you're supposed to pass some arguments to the script, but you haven't really shared how you launch that script. Perhaps there's a syntax error there.

     

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • Steve48 Profile Picture
    431 on at

    Hi Agnius. I didn't know Extract Text From PDF was available so I was reinventing the wheel. I think I figured out how to get the run Powershell action to work so I will finish that off but will use Extract Text From PDF as it is obviously more straightforward. Thanks for responding

  • Steve48 Profile Picture
    431 on at

    Here is my Run PowerShell Action and my latest script. Still doesn't work. Please see if there are syntax errors. TIA.

    I am going to now try the Extract Text from PDF action

    action.png

     

     

    import fitz
    import re
    import sys
    
    doc = sys.argv[1]
    print(doc)
    
    
    with fitz.open(doc) as pdf:
     text = ""
     for page in pdf:
     text += page.get_text()
    
    pattern = r'(\d{3}[-]?\d{3}[-]?\d{3}[-]?\d{4}|NO\.\d{13};)'
    matches = re.findall(pattern, text)
    
    if matches:
     result = matches[0].replace("-", "").replace("NO.", "").replace(";", "")
     print(result)
    else:
     print("No match found")

     

     

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    You probably need to put the script file path and the target file path into quotes like this:

    $arg1 = "%path_targetPDF%"
    $script = "%path_script%"
    & python $script $arg1

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • Steve48 Profile Picture
    431 on at

    I tried it. The anying "log" popup didn't happen anymore but DisplayMessage was blank

    $arg1 =  "%path_targetPDF%"
    $script = "%path_script%"
    & python $script $arg1

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    I'm pretty sure this is because your PowerShell script does not have any kind of a return statement. It might be required to return something to PAD as an output variable. Try using the return or write-output cmdlet at the end of your script.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
11manish Profile Picture

11manish 223

#2
David_MA Profile Picture

David_MA 210 Super User 2026 Season 1

#3
Haque Profile Picture

Haque 175

Last 30 days Overall leaderboard