Word automation

Hi,

Iam new to Robo corp i need one help.
I need to replace some text in the word document but values are dynamic.
Thanks in advance.

Hi Prasanth,

  1. Do you mean you need to update a custom document property, like this:
    How to use a custom document property to repeat content in Microsoft Word | TechRepublic

  2. Or do you mean that you have value in your word document that you need to find and replace with a dynamic value in your robot script?

The second option is probably easier, it can easily be achieved with Replace Text, but with out a clear understanding of what you want to acheive we could easily send you in the wrong direction.

Dave.

I need a solution for second option
i need to replace the value which is dynamic.
refer the sample file the highlighted value need to replace.

Hi Prasanth,

So as I mentioned you just need Replace Text, see the link in my previous post.

you will need to do something like:

  • Open File
  • Replace Text
  • Save Document or Save Document As
  • Close Document

Refer to the example and the keyword documentation and you should able to do that.

Dave.

1 Like

Hello

I haven’t got the issue.

I need to replace some text in the word document but values are dynamic.

Have you tried the Words Lib :

https://rpaframework.org/libraries/word_application/

It has a get All text Key word and an API :slight_smile:
https://rpaframework.org/libraries/word_application/python.html

You might wanna take a look at this method : it replace text in active document

replace_text(find: str, replace: str ) → None

Hope that helps,

Regards,
TextSolver34761.

Sorry @damies13 , I didn’t saw your answer.

1 Like

in replace text how can i mention the dynamic value that need to be replaced with some other values

Hi Prasanth,

For example you want to replace the second item highlighted in blue you would do something like:

    Replace Text     $390.00    ${your_dynamic_variable}

Ideally you’d want to control the original “template” document used by the test, to put a unique value in each location that needs to be replaced to make your test cases easier.

Dave.

Here $390.00 not fixed its also dynamic

In that case the RPA libraries are not going to be enough as they seem quite limited in what they can do with a word document.

I don’t know of a robot framework library that will do what you need with a word document.

There is the python-docx module that can probably do what you need, but you will need to be able to code it in python.

I expect what you’d want to do is:

  • open the document
  • find all tables
  • iterate over each table and check cells in each table to identify you have the one you want
  • address the cell by row/column number
    • read the current value if you need to
    • replace cell contents with new value
  • save and close document

I’ll suggest trying to make generic python keywords for robot framework that you can reuse, e.g.

  • A Get Tables keyword that returns a list of tables objects
  • A Get Cell Value that takes a tables object, row number and column number and returns the value of the cell
  • A Set Cell Value that takes a tables object, row number, column number, and new value, that updates the cell

Unfortunately unless someone else knows of a robot framework library that can edit word documents at this level, you’ve got some coding ahead of you.

Dave.

1 Like

Thanks for your help.

1 Like