Replacing text programatically

Hi all

I’m new to LibreOffice, and trying out the product, to see if it may be a replacement for the Microsoft Office Suite.

Right now I’m looking into some PowerShell scripts, that replaces text in Word documents using the COM api Word.application. I’m having trouble with finding documentation on a similar API for Writer, so I’ve tried using simple PowerShell replace in a .fodt document instead - which is causing me trouble due to the UTF8 encoding.

Could someone please point me in the direction of some method / script, that replaces text in existing Writer documents?

Thanks in advance.

/Stickybit

Finally managed to modify the document … the problem was an script error in the powershell script, causing seach and replace to fail.

Andrew Pitonyak - Macro Guide
chapter 14.7.2. Searching and replacing

Or you can try the extension Altsearch

Hi KamilLanda

Thanks for the answer - but it’s not macro’s I’m searcking for. I need some means of altering documents using scripts - without using the actual Writer GUI.

I’m not sure if it is possible by simple way, maybe you will need more programs.

The ODT is the renamed ZIP file, you can rename the .odt to the zip and unpack it to your temp directory. For example I use Bandizip for un/packing via the command line or in .BAT files.

Next step is to edit the file content.xml in temp directory - it is file with the content of the document. I suppose you know the regular expressions, so you will indite (or find in web) the regular expression to find&replace outside the XML tags. I use the Find & Replace for find&replace from the .BAT files.

And final step is to pack the temp directory to the .zip and rename it to the .odt.

It’s not cosy :frowning: and probably it can be unsafe to do changes “handly” in the content.xml.

I saw you used .fodt files, but I think it will be similar.

It is possible, as you can command LibreOffice via a Port, If started appropriatly.

The following link shows an example for python:

But you should be warned: In most cases it will be easier to work from a macro inside LibreOffice.