Skip to main content
Solved

WordWrapper 2.0?

  • November 28, 2016
  • 14 replies
  • 40 views

peteralstorp
Contributor
Forum|alt.badge.img+7

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?

Best answer by david_r

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

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

14 replies

fmelizard
Safer
Forum|alt.badge.img+20
  • Safer
  • 3719 replies
  • November 28, 2016

It seem you need "SubstringExtractor" transformer.


david_r
Celebrity
  • 8391 replies
  • Best Answer
  • November 28, 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


peteralstorp
Contributor
Forum|alt.badge.img+7
  • Author
  • Contributor
  • 91 replies
  • November 28, 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

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

 

 


mark2atsafe
Safer
Forum|alt.badge.img+56
  • Safer
  • 2554 replies
  • November 28, 2016
fyi I filed a request to get the WordWrapper fixed. It's PR#74183

 

 


david_r
Celebrity
  • 8391 replies
  • November 28, 2016
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.

 


itay
Supporter
Forum|alt.badge.img+18
  • Supporter
  • 1442 replies
  • November 28, 2016

It still there on the FME hub


david_r
Celebrity
  • 8391 replies
  • November 28, 2016

It still there on the FME hub

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

 

 


rylanatsafe
Safer
Forum|alt.badge.img+14
  • Safer
  • 671 replies
  • November 28, 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.

 


takashi
Celebrity
  • 7843 replies
  • November 29, 2016

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.

takashi
Celebrity
  • 7843 replies
  • November 29, 2016
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
Celebrity
  • 8391 replies
  • November 29, 2016
@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.

 


peteralstorp
Contributor
Forum|alt.badge.img+7
  • Author
  • Contributor
  • 91 replies
  • November 29, 2016
Done: https://hub.safe.com/transformers/textwrapper

 

 

Let me know if anything is unclear.

 

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

 

 


fme4ever
Forum|alt.badge.img
  • 10 replies
  • April 20, 2022

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!! 😁 


david_r
Celebrity
  • 8391 replies
  • April 20, 2022

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

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