Skip to main content

Hi, I need an easy way to split a string at a desired lengt, but I want to avoid splitting words. There once was a WordWrapper but it's not working anymore. Any suggestions?

It seem you need "SubstringExtractor" transformer.


I posted this a long time ago, but it's not easily searchable and the code has been mangled while ported between the different versions of the forum software, so here's a slightly modified version of my original post.

You can insert the following code into a PythonCaller, it will read a string from the attribute "text_line_data" and wrap it at the position specified in a parameter "MaxWidth":

import fmeobjects
import textwrap
 
def TextWrapper(feature):
    text = feature.getAttribute("text_line_data") # modify as necessary
    maxLength = int(FME_MacroValues_'MaxWidth'])
    parts = textwrap.wrap(text, maxLength)
    feature.setAttribute('number_of_parts', len(parts))
    if parts:
        feature.setAttribute('parts{}', parts)

Expose the attributes "parts{}" and "number_of_parts" in the PythonCaller.

This outputs all parts to the list "parts{}", you can then use a ListExploder to get one feature per wrapped line, see attached sample workspace.

textwrapper.fmwt


I posted this a long time ago, but it's not easily searchable and the code has been mangled while ported between the different versions of the forum software, so here's a slightly modified version of my original post.

You can insert the following code into a PythonCaller, it will read a string from the attribute "text_line_data" and wrap it at the position specified in a parameter "MaxWidth":

import fmeobjects
import textwrap
 
def TextWrapper(feature):
    text = feature.getAttribute("text_line_data") # modify as necessary
    maxLength = int(FME_MacroValues_'MaxWidth'])
    parts = textwrap.wrap(text, maxLength)
    feature.setAttribute('number_of_parts', len(parts))
    if parts:
        feature.setAttribute('parts{}', parts)

Expose the attributes "parts{}" and "number_of_parts" in the PythonCaller.

This outputs all parts to the list "parts{}", you can then use a ListExploder to get one feature per wrapped line, see attached sample workspace.

textwrapper.fmwt

Blessed be the force of the Knowledge center! Thank you, David!

 

 


fyi I filed a request to get the WordWrapper fixed. It's PR#74183

 

 


fyi I filed a request to get the WordWrapper fixed. It's PR#74183

 

 

That's good to hear. Feel free to borrow bits of the code below if you decide to port it to Python.

 


It still there on the FME hub


It still there on the FME hub

Yes, but it doesn't work (as it should) in FME 2016.

 

 


I posted this a long time ago, but it's not easily searchable and the code has been mangled while ported between the different versions of the forum software, so here's a slightly modified version of my original post.

You can insert the following code into a PythonCaller, it will read a string from the attribute "text_line_data" and wrap it at the position specified in a parameter "MaxWidth":

import fmeobjects
import textwrap
 
def TextWrapper(feature):
    text = feature.getAttribute("text_line_data") # modify as necessary
    maxLength = int(FME_MacroValues_'MaxWidth'])
    parts = textwrap.wrap(text, maxLength)
    feature.setAttribute('number_of_parts', len(parts))
    if parts:
        feature.setAttribute('parts{}', parts)

Expose the attributes "parts{}" and "number_of_parts" in the PythonCaller.

This outputs all parts to the list "parts{}", you can then use a ListExploder to get one feature per wrapped line, see attached sample workspace.

textwrapper.fmwt

@david_r - Fantastic! I'm glad to see that you have found a fix for this custom transformer! This would be a major change in the behind-the-scenes functionality from when it was first created (and last updated) in FME 2012. 

 

 

I would like to encourage you to submit this as a new custom transformer to FME Hub, e.g. TextWrapper, and we can deprecate the WordWrapper.

 


It still there on the FME hub

Maybe this update works with FME 2016.1.3: b16709-wordwrapper-v2-beta.fmw

 

Hi @DanAtSafe, please update the WordWrapper in the Hub.
Maybe this update works with FME 2016.1.3: b16709-wordwrapper-v2-beta.fmw

 

Hi @DanAtSafe, please update the WordWrapper in the Hub.
oops, I overlooked @Mark2AtSafe's comment. Good to hear that the request has been filed already.

 


@david_r - Fantastic! I'm glad to see that you have found a fix for this custom transformer! This would be a major change in the behind-the-scenes functionality from when it was first created (and last updated) in FME 2012.

 

 

I would like to encourage you to submit this as a new custom transformer to FME Hub, e.g. TextWrapper, and we can deprecate the WordWrapper.

 

Done: https://hub.safe.com/transformers/textwrapper

 

 

Let me know if anything is unclear.

 


Done: https://hub.safe.com/transformers/textwrapper

 

 

Let me know if anything is unclear.

 

Perfect, will definately use the wrapper sometime again soon. Thanks!

 

 


I posted this a long time ago, but it's not easily searchable and the code has been mangled while ported between the different versions of the forum software, so here's a slightly modified version of my original post.

You can insert the following code into a PythonCaller, it will read a string from the attribute "text_line_data" and wrap it at the position specified in a parameter "MaxWidth":

import fmeobjects
import textwrap
 
def TextWrapper(feature):
    text = feature.getAttribute("text_line_data") # modify as necessary
    maxLength = int(FME_MacroValues_'MaxWidth'])
    parts = textwrap.wrap(text, maxLength)
    feature.setAttribute('number_of_parts', len(parts))
    if parts:
        feature.setAttribute('parts{}', parts)

Expose the attributes "parts{}" and "number_of_parts" in the PythonCaller.

This outputs all parts to the list "parts{}", you can then use a ListExploder to get one feature per wrapped line, see attached sample workspace.

textwrapper.fmwt

Awesome! This one worked like a charm, thanks!! 😁 


Awesome! This one worked like a charm, thanks!! 😁

Thanks for the feedback and the push to fix the formatting one more time :-)


Reply