Read a Text and Count its Occurrence From a Docx File in Power Automate

I recently worked with a client who needed to check how often a specific word or phrase appeared in Word documents stored in OneDrive. Instead of going through each document manually, we used Power Automate to read the text and count its occurrences automatically.

In this tutorial, I will explain how to read a text and count its occurrence from a DOCX file in Power Automate, both without using premium connectors and with premium connectors.

Read a Text and Count its Occurrence From a Docx File in Power Automate [Without Using Premium Connector]

For this example, I have a DOCX file named ‘Power Automate’, which contains information about Power Automate, and this document is present in OneDrive. The file has three pages.

power automate combine word documents

Here, I want to search for the occurrence of the word ‘Power Automate’, and I found it appears 12 times.

power automate Occurrence word in documents

Now, I want to achieve this in Power Automate.

Before proceeding, ensure you have a ZIP file in your OneDrive.

To do this, follow the steps below.

1. In Power Automate, create an Instant Cloud Flow and name it. Use the Manually Trigger a Flow trigger.

How to Read a Text From a docx File in Power Automate

2. To select the file document file, add the Extract archive to folder action from OneDrive and provide below parameters:

  • Source Archive File Path: First, select the ZIP file, then replace the file name with your document file name along with its extension.
  • Destination Folder Path: Select any folder of your choice.

First, we select the ZIP file and then replace its name because the Source Archive File Path only allows selecting ZIP files.

Read a Text From a docx File in Power Automate

3. Add Filter array action and provide the below parameter:

  • From: Select body from dynamic content of the Extract archive to folder action.
  • Filter Query: Provide the below expression in advance mode:
equals(item()['Name'],'document.xml')
Read a Text From a docx File using Power Automate

4. Add a Get file content action and, in the File parameter, provide the following expression:

first(body('Filter_array'))['Id']
How to read a text and count its occurrence from a docx file in Power automate

5. Then add a compose action and provide the below expression in the input parameters:

xpath(xml(outputs('Get_file_content')?['body']), '//*[name()=''w:t'']/text()')
How to count occurences of a specific word in Power automate

This Compose action gives us the Word document content in an array format, but we need it as a string.

6. I will add a Join action, which converts the array into a string by adding a comma delimiter.

How to count occurrences of a text string in multiple Word in Power Automate

7. Now add the Initialize variable action, then provide the below parameters:

  • Name: Provide the name as varCount
  • Type: select the type as Integer.
  • Value: provide the value as -1.
count occurrences of a text string in multiple Word in Power Automate

6. Add compose action to split the Power Automate word using the below expression:

split(body('Join'), 'Power Automate')
power automate merge word documents

7. Then add Apply to each action and provide the above compose action output in Select an output from previous steps parameters:

How do I extract text from a Word document in power automate

8. Inside the Apply to each action, add an Increment variable action. Select the name varCount from the dropdown, and set the value to 1.

Read a Text and Count its Occurrence From a docx File using Power Automate

9. Add another Compose action to see how many times the word ‘Power Automate’ occurs in the DOCX file by adding varCount in the input parameters.

Read a Text and Count its Occurrence From a Docx File in Power Automate[Without using Premium Connector]

Now save and run the flow. Once your flow runs successfully, you can see the Count of Power Automate word Occurrence in the compose action:

Read a Text and Count its Occurrence From a Docx File in Power Automate Without using Premium Connector

It shows 12, which matches the manual count of the word ‘Power Automate’ appearing 12 times in the document.

Read a Text and Count its Occurrence From a Docx File in Power Automate [Using Premium Connector]

I have a Word file with the content below in the SharePoint folder for this example. We will read the text and count the occurrence of the word ‘example‘ using Power Automate.

How to read the text and its occurrence from the docx file in Power automate

To create this flow, we will use a third-party action, Convert file from Encodian, to convert the file to text. To use Encodian actions, you need an API key. To get the API key, go to this link and fill out the form to activate the free trial. It will generate an API key for you.

Now follow the below steps:

1. Create an automated cloud flow. Give the flow name and select the trigger When a file is created in a folder. Also, provide the Site Address and Folder Path.

How to read the text and its occurrence from the docx file in Power Automate flow

2. Then add the Convert word action from Encodian and provide the below parameters:

How to read the text and its occurrence from the docx file in flow

3. Add a compose action and provide the below expression:

base64ToString(outputs('Convert_-_Word')?['body/FileContent'])
read the text and its occurrence from the docx file in Microsoft flow

4. Now add the Initialize variable action, then provide the below parameters:

  • Name: Provide the name as varCount
  • Type: select the type as Integer.
  • Value: provide the value as -1.
read the text and its occurrence from the docx file in Power Automate flow

5. Add compose action to split the Power Automate word using the below expression:

split(outputs('Compose'), 'example')
read the text and its occurrence from the docx file in Power Automate

6. Then add Apply to each action and provide the above compose action output in Select an output from previous steps parameters:

read the text and its occurrence from the docx file using Power Automate

7. Inside the Apply to each action, add an Increment variable action. Select the name varCount from the dropdown, and set the value to 1.

Read a Text and Count its Occurrence From a Docx File in Power Automate using Premium Connector

8. Add another Compose action to see how many times the word ‘example’ occurs in the DOCX file by adding varCount in the input parameters.

Power Automate Read a Text and Count its Occurrence From a Docx File using Premium Connector

Now, it’s time to save the flow. Click on Test -> Select Manually. Then, add the file to the folder. After the flow runs successfully, you will see that the word ‘example’ appears 3 times in the Word document.

How to read the text and its occurrence from the docx file in Microsoft Power automate

Conclusion

In this tutorial, I explained how to use Power Automate to count the occurrences of a specific word or phrase in a DOCX file stored in OneDrive or SharePoint. I covered two methods: one without using premium connectors.

>

Build a High-Performance Project Management Site in SharePoint Online

User registration Power Apps canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial FREE PDF Download

FREE Power Platform Tutorial PDF

Download 135 Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…