Working with Multi Line Texts in Arcpy

4192
9
01-29-2014 06:46 AM
JonathanMulder
New Contributor III
Greetings,

I want to automate printing out eight maps. In essence, I've got 4 Bookmarks of counties, and layers dealing with deep wells and layers dealing with shallow wells.

Each map will have a three-line Title that changes for each map. For example:

BUTTE COUNTY DOMESTIC WELL DEPTH SUMMARY
with Depth to Groundwater Contours for Wells Screened
at Depths Less Than 150 Feet

QUESTION -- How do I deal with a multi-line string in Python? I've seen some regerences to triple quotes but don't really understand.

Thanks!
0 Kudos
9 Replies
JoshuaChisholm
Occasional Contributor III
Hello Jon,

A few things to note. You can use triple quotes (single or double) to handle multiple lines in python:
title="""BUTTE COUNTY DOMESTIC WELL DEPTH SUMMARY
with Depth to Groundwater Contours for Wells Screened
at Depths Less Than 150 Feet"""

If you prefer you can also use "\n" which is the code for line break:
title="BUTTE COUNTY DOMESTIC WELL DEPTH SUMMARY\nwith Depth to Groundwater Contours for Wells Screened\nat Depths Less Than 150 Feet"


Also by the nature of your project, I'd seriously suggest using data driven pages. You can use your counties to drive your maps (use a Definition Query if you only want to map certain counties). You can even have the county name appear as part of your title by adding a text box in your layout like this:
<dyn type="page" property="attribute" field="CountyName" domainlookup="true"/>COUNTY DOMESTIC WELL DEPTH SUMMARY
with Depth to Groundwater Contours for Wells Screened
at Depths Less Than 150 Feet

[INDENT]Change "CountyName" to the name of the field containing names.[/INDENT]


Good luck! Let me know how it goes!
~Josh
0 Kudos
JonathanMulder
New Contributor III
Josh,

Thanks very much for the quick reply.  Now I see how the triple quotes are used!  I prefer the second method with embedding the line feed code directly into the test string.

Actually, my Bookmarks are not entire counties; only selected portions showing the Sacramento Valley floor in northern California, so data driven pages won't work for this (my first) attempt at automation.  I think it will work on my next one though.
0 Kudos
JonathanMulder
New Contributor III
Gosh!  I've tried both approaches, but it's not coming out quite right.  I put a space on both side of the "\n", and the attached image (MapTitleGlitch) is what I get.  So then I tried just a space AFTER "\n" with the same results.
So I went back to the original with NO spaces, and still get odd results (Image MapTitleGlitch002).

I also get the same weird results when I tried the triple quote method.

The text is supposed to be centered and the text box width is the whole length of the black rectangle.

Any ideas?
0 Kudos
JoshuaChisholm
Occasional Contributor III
Nice looking maps!

It's hard to tell exactly what's going on there, but it looks like you have overlapping text boxes. Is the text stored in multiple text boxes?

If so, go into "Size and Position" (under the text box properties) and play with the positions and anchor points to make sure they don't overlap.

Let us know how it goes!
0 Kudos
JonathanMulder
New Contributor III
No, it's all one big TextBox with no overlaps.  The testbox is the extent of the black rectangle.  Very odd!

In the meantime, I'll drive three stacked single-line textboxes and see what that does.

Glad you like the map!
0 Kudos
JoshuaChisholm
Occasional Contributor III
That's really odd then. I'm not sure exactly what it is, but there are some things you could try:

First, could you check the contents of the text box to make sure there's no weird formatting in the text.
Secondly, I'd make sure all the character and word spacing settings are not set to anything odd. Under text box properties there are some spacing settings. I'd also go further into the properties by clicking "Change Symbol..." then "Edit Symbol...". The "General" and "Formatted Text" tabs have settings that might be the problem.

Alternatively, you could create a new text box (start anew).

Keep me posted. Weird problem...
0 Kudos
JonathanMulder
New Contributor III
Thanks for those suggestions, hua17.

Actually, I've settled on a workaround: Simply break the TextBox into three seperate boxes with NO linefeeds.

It's working for me with that workaround and am currently automatically exporting eight maps to PDF.  Map Automation rocks!
0 Kudos
JoshuaChisholm
Occasional Contributor III
Good! I'm happy to hear! And yes, map automation is awesome.

Cheers,
~Josh
0 Kudos
JeffBarrette
Esri Regular Contributor
If the string is always 3 lines and you know exactly where to insert a new line then simply append each line of text with a newline escape character.

For example:
str = "line 1 \nline 2 \nline 3"  or
str = "line 1" + "\n" + "line 2" + "\n" + "line 3"  etc 


If you don't know where to break the lines you could try updating a paragraph text element.

Or you can try using the Python textwrap function.  http://docs.python.org/2/library/textwrap.html

Jeff
0 Kudos