Skip to main content

Notifications

Community site session details

Community site session details

Session Id : K8/MzoCiJ+NOL5Ff8sx0Gm
Power Automate - Power Automate Desktop
Unanswered

Getting syntax error or invalid value while running Run python script action in desktop flows

Like (0) ShareShare
ReportReport
Posted on 17 Oct 2023 20:03:04 by 2

this is the script I am running 

import datetime

# Get the current date
current_date = datetime.datetime.now()

# Initialize a counter
counter = 0

# Define the number of iterations
total_iterations = 730 # for 2 years

while counter < total_iterations:
# Calculate Date2 as Date1 - 15 days
date2 = current_date - datetime.timedelta(days=15)

# Print Date1 and Date2
print("Date1:", current_date.strftime("%Y-%m-%d"))
print("Date2:", date2.strftime("%Y-%m-%d"))

# Update Date1 for the next iteration
current_date = date2 - datetime.timedelta(days=1)

# Increment the counter
counter += 1

I have provide correct alligment to the script. Can someone help regarding this.

Categories:
  • SriniK Profile Picture
    3,025 Super User 2025 Season 1 on 18 Oct 2023 at 06:56:53
    Re: Getting syntax error or invalid value while running Run python script action in desktop flows

    Hi @Abhisht 

     

    Just set the indentation as below

     

    import datetime
    
    # Get the current date
    current_date = datetime.datetime.now()
    
    # Initialize a counter
    counter = 0
    
    # Define the number of iterations
    total_iterations = 730 # for 2 years
    
    while counter < total_iterations:
     # Calculate Date2 as Date1 - 15 days
     date2 = current_date - datetime.timedelta(days=15)
    
     # Print Date1 and Date2
     print "Date1:", current_date.strftime("%%Y-%%m-%%d")
     print "Date2:", date2.strftime("%%Y-%%m-%%d")
    
     # Update Date1 for the next iteration
     current_date = date2 - datetime.timedelta(days=1)
    
     # Increment the counter
     counter += 1
    

     

    Try as above and you are fine to get the results

     

    Srini84_0-1697612179845.png

     

    If you find this reply helpful, please consider giving it a LIKE AND

    If this reply answers your question or solves your issue, please ACCEPT AS SOLUTION

     

    Thanks,

    Srini

     

  • Agnius Bartninkas Profile Picture
    10,045 Most Valuable Professional on 18 Oct 2023 at 06:07:09
    Re: Getting syntax error or invalid value while running Run python script action in desktop flows

    Can you share a screenshot of your flow?

  • Abhisht Profile Picture
    2 on 18 Oct 2023 at 04:14:17
    Re: Getting syntax error or invalid value while running Run python script action in desktop flows

    its working but its showing empty in the display message 

  • Agnius Bartninkas Profile Picture
    10,045 Most Valuable Professional on 18 Oct 2023 at 03:15:36
    Re: Getting syntax error or invalid value while running Run python script action in desktop flows

    The syntax error is because of the percentage symbols. These are reserved by PAD for variable notation. To pass a literal % to the script, you need to escape it with another %. Try this:

    print("Date1:", current_date.strftime("%%Y-%%m-%%d"))
    print("Date2:", date2.strftime("%%Y-%%m-%%d"))

     

    Please note, however, that PAD can only return a single value from a Python script. When you have several print statements, you will not get the actual 730 x 2 strings printed out. If you want that, you should in fact append your values to a single string inside the loop and then use a single print statement at the end of the 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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,645 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,997 Most Valuable Professional

Leaderboard
Loading started