Splitting text between two text elements

805
9
09-11-2013 07:26 AM
JeremyRogers
New Contributor
I have a text string that is longer than my what my text element will display. I need to figure out a way to have the non-displayed text carry over to a second text element. Any thoughts on how to script that?
Tags (2)
0 Kudos
9 Replies
MathewCoyle
Frequent Contributor
Do you want it on a separate text element or a different line in the same text element?

You can do some simple string splicing to get at most a specific length. Here is an older post about word wrapping. You can use a similar method just moving to the next text element instead of the next line if you truly want to separate them.

http://forums.arcgis.com/threads/62418-Text-Formatting-in-Python?p=215891&viewfull=1#post215891
0 Kudos
JeremyRogers
New Contributor
I truly need it to continue to the next text element. I did see that previous thread but I wasn't sure how to extrapolate what I needed. I'll play around with it and see what I can get.

Thanks
0 Kudos
MathewCoyle
Frequent Contributor
Here is another thread that goes through splitting field values between text elements.

http://forums.arcgis.com/threads/90554-Text-Element-Questions

You'll probably want to create a list of your text separated into the chunks you want and then iterate through them putting them in the required text elements.
0 Kudos
JeremyRogers
New Contributor
I'm still struggling. The catch is that the text elements are populated with text from concatenated fields from a table.

Here's a simplified version:

cursor = arcpy.SearchCursor(Project)
for row in cursor:
    for elm in arcpy.mapping.ListLayoutElements(map, "TEXT_ELEMENT"):
       if elm.name == "Text1":
           elm.text = textwrap.dedent(str(row.Overview).strip()+str(row.Project).strip())

So the text populated from these fields exceeds the boundaries of the text element and I need it to carry over to the next text element titled "Text2".

I thought dividing it based on a character count would be the way to go but I've been unsuccessful.

Any help is appreciated.

Thanks
0 Kudos
MathewCoyle
Frequent Contributor
How I would do it is take all the text out into on variable then split it up however I'd need to. Something like this.

text = ''
cursor = arcpy.SearchCursor(Project)
for row in cursor:
    text = '{0} {1} {2} '.format(text, str(row.Overview).strip(), str(row.Project).strip())


It would depend on how your text is organized, how much there is, and how you want to display it.
0 Kudos
JeremyRogers
New Contributor
What does     text = '{0} {1} {2}    denote?
0 Kudos
MathewCoyle
Frequent Contributor
What does     text = '{0} {1} {2}    denote?


That is the preferred method of string concatenation. Each number marks the index of the value being inserted into the string ordered by the format call. It would be helpful if you provided an example of the text you are working with, where you want to split it, and how many text elements you want to span.
0 Kudos
JeremyRogers
New Contributor
[ATTACH=CONFIG]27392[/ATTACH]

The attachment shows the layout of the page. The idea is to have the text run down the left side of the page in the 1st text element, and then continue along the bottom into the 2nd text element.

I'm trying to automate this process to iterate through every record in the table and produce a seperate png for each.

The data is simply text from multiple fields that I am concatenating so that all text will be together. The issue is that depending on the record the text will vary in length from field to field. This is why I can't specify certain fields to go to certain text elements. It needs to be able to "flex" and I can't have any gaps between text; hence the use of textwrap.dedent and .strip().
I can provide you with examples of the data but I didn't think you would after me explanation.

Thank you for your time and patience.
0 Kudos
JeremyRogers
New Contributor
So I figured out a way for it to work. I took your advice and set all of my text as a variable. I set up the script as follows:

for row in cursor:
    text = textwrap.dedent(str(row.Overview).strip()+str(row.Project).strip())
    for elm in aprcpy.mapping.ListLayoutElements(map, "TEXT_ELEMENT"):
       if len(text)==len(text):
          if elm.name == "Text1":
              elm.text = text[0:1000]
          if elm.name == "Text2":
              elm.text = text[1001:]
0 Kudos