Text Formatting in Python

Discussion created by newell.kate on Jul 16, 2012
Latest reply on Jul 17, 2012 by newell.kate
Does anyone know the python syntax to count the number of characters in a field and format the text based on the number of characters?
I have a script that populates a text element in my map with the values of a field based on a data driven query.
Problem is, my text element is at times not wide enough to hold all the text that I need to display (and the width of the text element is not able to be changed).
There are just a handful of row values that don't fit the text element width (basically anything that is greater than 32 characters).
I am thinking if I knew the syntax to count the characters, that I could format the character and word spacing (reduce them).
Below is the entire code block.  I think where I could run this if/else based on character count is in the lower portion of the code where I iterate through the rows.
Any help greatly appreciated.

#Date: 8/1/2011
#Zoom to Neighborhood

#This tool runs from a script tool.  Choose a Neighborhood from a pick list
# generated from the script tools validation script.  Click OK and the
# Data Driven Page will update the layer with the appropriate dynamic text and
# tabular information.

#Note - this script tool uses CURRENT and must be run from within ArcMap.

import arcpy, os, sys

#Reference current MXD 
mxd = arcpy.mapping.MapDocument("CURRENT")
##mxd = arcpy.mapping.MapDocument(R"N:\Users\ke\NeighborhoodMapSeries\TestLegend.mxd")

#Get input parameter
NHName = arcpy.GetParameterAsText(0)

#Reference appropriate data frames
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

#Reference appropriate layers
NHLabels = arcpy.mapping.ListLayers(mxd, "Neighborhood Labels", df)[0]
ZoningSA = arcpy.mapping.ListLayers(mxd, "Zoning Sub-Area", df)[0]
NHAnno = arcpy.mapping.ListLayers(mxd, "NH_Anno",df)[0]

#Reference layout elements by calling ListLayoutElements only once - get better performance
for elm in arcpy.mapping.ListLayoutElements(mxd):
  if =="LU_Legend_Num": LegNum = elm
  if =="LU_Legend_Text": LegText = elm

#Reference the Data Driven Page object
ddp = mxd.dataDrivenPages

#Set the current page to be the one selected in the script tool

pageID = mxd.dataDrivenPages.getPageIDFromName(str(NHName))
mxd.dataDrivenPages.currentPageID = pageID

#Set the appropriate definition queries
NHLabels.definitionQuery = "NEIGHBORHOOD_NAME <> '" + NHName +  "'"
ZoningSA.definitionQuery = "NEIGHBORHOOD_NAME = '" + NHName +  "'AND GENERAL_USE_TYPE <> 'WATER '"
NHAnno.definitionQuery = "NEIGHBORHOOD_NAME = '" + NHName +  "'"
print "def query worked"

#Reference RevisionText table and select appropriate records
revTable = arcpy.mapping.ListTableViews(mxd, "NH_Legends")[0]

#Build query and create search cursor to loop through rows
NHFieldValue = NHName
queryExp = "\"NEIGHBORHOOD_NAME\" = '" + NHFieldValue + "'"  #e.g., "Neighborhood_Name" = 'CORNWALL PARK'
NHRows = arcpy.SearchCursor(revTable.dataSource, queryExp, "", "", "SA_NUM")

#Clear all table text values
LegNum.text = " "; LegText.text = " "

#iterate through each row, update appropiate table/text
for row in NHRows:
  LegNum.text = LegNum.text + row.getValue("SUBAREA_CODE") + "\n"
  LegText.text = LegText.text + row.getValue("LAND_USE_DESIGNATION") + "\n"  
arcpy.AddMessage("PROCESS COMPLETED")