Select to view content in your preferred language

Text Formatting in Python

3250
8
07-16-2012 12:31 PM
KateNewell
Deactivated User
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)
##NHName = "EDGEMOOR"

#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 elm.name =="LU_Legend_Num": LegNum = elm
  if elm.name =="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
arcpy.AddMessage(NHName)

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"
    
##mxd.save()  
arcpy.RefreshActiveView()
arcpy.AddMessage("PROCESS COMPLETED")
Tags (2)
8 Replies
BradPosthumus
Frequent Contributor
To count characters in a string, use the len() function. For example:

if len(LegText.text) > 32:
    # make adjustments to text properties here
0 Kudos
KateNewell
Deactivated User
Thanks!
...and sorry to be totally useless 😕
...but I also can't figure out the text formatting.
I only need the LegText to be adjusted...but I can't get the formatting correct either.  Also not sure if making the spacing negative is appropriate (as I want the spacing to go smaller)
Below is what I tried, and I am not finding much help on the resource center about formatting text in python...

#iteate through each row, update appropriate table/text
for row in NHRows:
  if len(LegText.text) > 32:
    LegText.text = '<CHR spacing="-25">'LegText'</CHR>'
    LegNum.text = LegNum.text + row.getValue("SUBAREA_CODE") + "\n"
    LegText.text = LegText.text + row.getValue("LAND_USE_DESIGNATION") + "\n"
  else:
    LegNum.text = LegNum.text + row.getValue("SUBAREA_CODE") + "\n"
    LegText.text = LegText.text + row.getValue("LAND_USE_DESIGNATION") + "\n"
0 Kudos
MathewCoyle
Honored Contributor
Thanks!
...and sorry to be totally useless 😕
...but I also can't figure out the text formatting.
I only need the LegText to be adjusted...but I can't get the formatting correct either.  Also not sure if making the spacing negative is appropriate (as I want the spacing to go smaller)
Below is what I tried, and I am not finding much help on the resource center about formatting text in python...

#iteate through each row, update appropriate table/text
for row in NHRows:
  if len(LegText.text) > 32:
    LegText.text = '<CHR spacing="-25">'LegText'</CHR>'
    LegNum.text = LegNum.text + row.getValue("SUBAREA_CODE") + "\n"
    LegText.text = LegText.text + row.getValue("LAND_USE_DESIGNATION") + "\n"
  else:
    LegNum.text = LegNum.text + row.getValue("SUBAREA_CODE") + "\n"
    LegText.text = LegText.text + row.getValue("LAND_USE_DESIGNATION") + "\n"


I believe what you are looking for can be found here under your local help files.
"C:\Program Files (x86)\ArcGIS\Desktop10.0\help\esri_csHTML_ArcMap.chm"

Also, here is the code I used to get a large comment field to print over multiple lines.
import string
import arcpy

mxd = arcpy.mapping.MapDocument("current")
elm = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","element_name")[0]
line = ""
entry = str()
for word in string.split(comments):
    if len(line) == 0:
        line = word
    else:
        line = line+" "+word
    if len(line)>50:
        print line
        entry = entry+"\r\n"+line
        line=""
print line
entry = entry+"\r\n"+line
elm.text = entry
0 Kudos
KateNewell
Deactivated User
The help document reference VB and from what I can tell, the syntax for text formatting in python is a bit different.
This seems like something that should be really easy...but for the life of me I am just not getting it 😞
0 Kudos
MathewCoyle
Honored Contributor
The help document reference VB and from what I can tell, the syntax for text formatting in python is a bit different.
This seems like something that should be really easy...but for the life of me I am just not getting it 😞


I am a little confused, it is the formatting style for map document elements. What text are you trying to format?
0 Kudos
curtvprice
MVP Alum
here is the code I used to get a large comment field to print over multiple lines.


FYI - there's a really handy standard python module for wrapping text: textwrap.
0 Kudos
KateNewell
Deactivated User
Not sure the text wrapping will work, as the "LegText" element is associated and needs to be aligned with the "LegNum" Element.
I have attached a screen shot of what my issue is and why I need to resize the "LegText" element, as it runs off the page after 33 characters.
This is a script tool that uses data driven pages to zoom to different neighborhoods and populate the two text elements in on the right of the page to serve as a legend.  The data is being pulled from a table using search cursors.
0 Kudos
KateNewell
Deactivated User
Thanks everyone for all the ideas....I finally figured out a solution that will work using string slicing and carriage returns.
Still not sure on the python syntax for formatting text, but ended up not going that route.
Below is the code I ended up using, and attached is an image of the text boxes that it spits out in my ArcMap project:

#iteate through each row, update appropriate table/text
for row in NHRows:
  if len(row.LAND_USE_DESIGNATION) > 32:
    LegNum.text = LegNum.text + row.SUBAREA_CODE + "\n" + " " + "\n"
    LegText.text = LegText.text + row.LAND_USE_DESIGNATION[0:32] + "\n" + row.LAND_USE_DESIGNATION[32:] + "\n"
  else:
    LegNum.text = LegNum.text + row.SUBAREA_CODE + "\n" 
    LegText.text = LegText.text + row.LAND_USE_DESIGNATION + "\n"    


[ATTACH=CONFIG]16177[/ATTACH]
0 Kudos