Writer: read through the all text from Python macro

Hello,

I want to write a Pyhon macro to match regular expressions through an entire Writer document.
In other words, I got this small program I’d like to translate into a LO Writer macro:

import sys
import re

inputFile = sys.argv[1]

fdInput=open(inputFile,'ro')

reExp=re.compile('\n\n\n(\w.*)\n')    
reResults=reExp.findall(fdInput.read())

fdInput.close()

for i in reResults:
  print i

I did not have much luck with LO documentation so, far, any guidance?

Hello DarioDD, to get the Text from the current Writer document you could write:

desktop = XSCRIPTCONTEXT.getDesktop()
thisComponent = desktop.getCurrentComponent()
if hasattr( thisComponent, "Text" ):
	import re
	reExp = re.compile('\n\n\n(\w.*)\n')    
	reResults = reExp.findall( thisComponent.Text.String )
	for i in reResults:
		print( i )

That brought me forward! Thanks a lot @librebel!

yw, glad i could be of help :slight_smile: