Using Custom Activities in Power Automate Desktop Flow

Power Automate Desktop (PAD) is a powerful tool that enables the automation of repetitive tasks and processes across various applications and services. While PAD offers a wide range of built-in actions, sometimes you need to perform tasks that are not covered by the default options. This is where custom activities come into play. Custom activities allow you to extend the functionality of your workflows, making them more flexible and tailored to your specific needs.

In this blog post, we'll explore how to create and use custom activities in Power Automate Desktop. We'll cover the basics of custom activities, how to develop them, and provide examples to help you get started.

Understanding Custom Activities

Custom activities in Power Automate Desktop are user-defined actions that can be integrated into your automation workflows. These activities are created using programming languages like Python, C#, or JavaScript, and can be used to perform tasks that are not available in the default set of actions. Custom activities can interact with applications, manipulate data, and perform complex calculations.

Why Use Custom Activities?

  1. Extend Functionality: Add capabilities not provided by built-in actions.

  2. Reuse Code: Encapsulate and reuse complex logic across multiple workflows.

  3. Integration: Integrate with external systems and services that are not natively supported.

Creating Custom Activities

Creating custom activities involves several steps:

  1. Define the Custom Activity: Decide what the activity will do and which programming language to use.

  2. Develop the Activity: Write the code for the custom activity.

  3. Package the Activity: Package the custom activity so it can be imported into Power Automate Desktop.

  4. Import and Use the Activity: Import the custom activity into PAD and use it in your workflows.

Step 1: Define the Custom Activity

Before you start coding, define the purpose and scope of your custom activity. For this example, let's create a custom activity that extracts text from an image using Optical Character Recognition (OCR).

Step 2: Develop the Activity

We’ll use Python to develop our custom OCR activity. Here’s a simple example using the pytesseract library:

pythonCopy codeimport pytesseract
from PIL import Image
import sys

def extract_text_from_image(image_path):
    try:
        # Open the image file
        img = Image.open(image_path)
        # Use pytesseract to do OCR on the image
        text = pytesseract.image_to_string(img)
        return text
    except Exception as e:
        return str(e)

if __name__ == "__main__":
    # The image path will be passed as a command-line argument
    image_path = sys.argv[1]
    result = extract_text_from_image(image_path)
    print(result)

Step 3: Package the Activity

Package the Python script and any dependencies into a format that can be executed from Power Automate Desktop. This can be done by creating a batch file or a PowerShell script to run the Python script.

Example batch file (run_ocr.bat):

batchCopy code@echo off
python path\to\your\script.py %1

Step 4: Import and Use the Activity in PAD

  1. Import the Custom Activity: In Power Automate Desktop, use the "Run Python script" action to import and run your custom activity.

  2. Configure the Action: Provide the path to your batch file and any necessary parameters.

Example PAD steps:

  1. Add the "Run Python script" action.

  2. Set the "Script" field to the path of your batch file (run_ocr.bat).

  3. Set the "Arguments" field to the image path.

Example Workflow

Let's create a workflow that uses our custom OCR activity to extract text from an image and save it to a text file.

  1. Launch Power Automate Desktop: Open Power Automate Desktop and create a new flow.

  2. Get Image Path: Use the "Get file path" action to select the image file.

  3. Run Custom Activity: Use the "Run Python script" action to execute the run_ocr.bat file with the image path as an argument.

  4. Save Output: Capture the output of the Python script and save it to a text file using the "Write text to file" action.

Sample Flow Steps

  1. Get file path: imagePath

  2. Run Python script:

    • Script: C:\path\to\run_ocr.bat

    • Arguments: imagePath

    • Output: ocrResult

  3. Write text to file:

    • File path: C:\path\to\output.txt

    • Text to write: ocrResult

Conclusion

Custom activities in Power Automate Desktop provide a powerful way to extend the capabilities of your workflows. By leveraging programming languages like Python, you can create custom actions that perform tasks not covered by built-in actions. This flexibility allows you to automate more complex and unique processes, enhancing the efficiency and effectiveness of your automation solutions.