0

Here is I want: I have a .doc file which consists of many lines. In the file I need to find a string and replace it with another string. Up to this fine. But how can I replace the string with another string along with format. I mean like Bold, Italic, Underline, etc. for new string.

2
  • @Radu Thanks you. ( I cant type your name completely.those spec char are not in my KB)
    – Raja G
    Oct 24, 2013 at 6:47
  • My documents will be opened with ms word libre word processors
    – Raja G
    Oct 24, 2013 at 9:59

1 Answer 1

2

The only way I would know how to automate .doc files is with a very elaborate and extremely time consuming Python script. You would need to automate [Libre|Open]Office using PyUNO.

This is the best way to do this because you can use just about every function in Office without having to do dodgy hacks...


A less ideal method would be using unoconv to convert the .doc to something human-readable like .fodt, then sed and the convert it back to .doc.

unoconv -o test.fodt -f fodt test.doc
sed -i 's/string/newstring/g'
unoconv -o test.doc -f doc test.doc

You have to be careful that your string isn't going to interfere with the document tags. If it is, you'll need to use something a lot more industrial to make sure you're only editing things in <text:...> tags.

So instead of using .fodt, you can use html

unoconv -o test.html -f html test.doc
sed -i 's/string/<B>newstring</B>/g'
unoconv -o test.doc -f doc test.html

However, there is every chance the conversion will completely munch the document formatting.

2
  • 1
    Master one doubt ..what about special symbols and table's data if convertion to be done
    – Raja G
    Oct 24, 2013 at 9:58
  • 1
    Then all bets are off. If it's any consolation, you can take a look at one of the fodt/html pages to see exactly what you're dealing with but documents are trickier things than most people give them credit.
    – Oli
    Oct 24, 2013 at 10:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .