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.
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.
It still there on the FME hub
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.
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
Hi @DanAtSafe, please update the WordWrapper in the Hub.
Hi @DanAtSafe, please update the WordWrapper in the Hub.
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.
Let me know if anything is unclear.
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.
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 :-)