<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Text Formatting in Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27299#M2064</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;To count characters in a string, use the len() function. For example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
if len(LegText.text) &amp;gt; 32:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # make adjustments to text properties here
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 21:06:27 GMT</pubDate>
    <dc:creator>BradPosthumus</dc:creator>
    <dc:date>2021-12-10T21:06:27Z</dc:date>
    <item>
      <title>Text Formatting in Python</title>
      <link>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27298#M2063</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a script that populates a text element in my map with the values of a field based on a data driven query.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;There are just a handful of row values that don't fit the text element width (basically anything that is greater than 32 characters).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am thinking if I knew the syntax to count the characters, that I could format the character and word spacing (reduce them).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Below is the entire code block.&amp;nbsp; 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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help greatly appreciated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;#Date: 8/1/2011
#Zoom to Neighborhood

#This tool runs from a script tool.&amp;nbsp; Choose a Neighborhood from a pick list
# generated from the script tools validation script.&amp;nbsp; 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):
&amp;nbsp; if elm.name =="LU_Legend_Num": LegNum = elm
&amp;nbsp; 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 &amp;lt;&amp;gt; '" + NHName +&amp;nbsp; "'"
ZoningSA.definitionQuery = "NEIGHBORHOOD_NAME = '" + NHName +&amp;nbsp; "'AND GENERAL_USE_TYPE &amp;lt;&amp;gt; 'WATER '"
NHAnno.definitionQuery = "NEIGHBORHOOD_NAME = '" + NHName +&amp;nbsp; "'"
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 + "'"&amp;nbsp; #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:
&amp;nbsp; LegNum.text = LegNum.text + row.getValue("SUBAREA_CODE") + "\n"
&amp;nbsp; LegText.text = LegText.text + row.getValue("LAND_USE_DESIGNATION") + "\n"
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
##mxd.save()&amp;nbsp; 
arcpy.RefreshActiveView()
arcpy.AddMessage("PROCESS COMPLETED")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 16 Jul 2012 19:31:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27298#M2063</guid>
      <dc:creator>KateNewell</dc:creator>
      <dc:date>2012-07-16T19:31:02Z</dc:date>
    </item>
    <item>
      <title>Re: Text Formatting in Python</title>
      <link>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27299#M2064</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;To count characters in a string, use the len() function. For example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
if len(LegText.text) &amp;gt; 32:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # make adjustments to text properties here
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:06:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27299#M2064</guid>
      <dc:creator>BradPosthumus</dc:creator>
      <dc:date>2021-12-10T21:06:27Z</dc:date>
    </item>
    <item>
      <title>Re: Text Formatting in Python</title>
      <link>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27300#M2065</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...and sorry to be totally useless &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...but I also can't figure out the text formatting.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I only need the LegText to be adjusted...but I can't get the formatting correct either.&amp;nbsp; Also not sure if making the spacing negative is appropriate (as I want the spacing to go smaller)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Below is what I tried, and I am not finding much help on the resource center about formatting text in python...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#iteate through each row, update appropriate table/text
for row in NHRows:
&amp;nbsp; if len(LegText.text) &amp;gt; 32:
&amp;nbsp;&amp;nbsp;&amp;nbsp; LegText.text = '&amp;lt;CHR spacing="-25"&amp;gt;'LegText'&amp;lt;/CHR&amp;gt;'
&amp;nbsp;&amp;nbsp;&amp;nbsp; LegNum.text = LegNum.text + row.getValue("SUBAREA_CODE") + "\n"
&amp;nbsp;&amp;nbsp;&amp;nbsp; LegText.text = LegText.text + row.getValue("LAND_USE_DESIGNATION") + "\n"
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; LegNum.text = LegNum.text + row.getValue("SUBAREA_CODE") + "\n"
&amp;nbsp;&amp;nbsp;&amp;nbsp; LegText.text = LegText.text + row.getValue("LAND_USE_DESIGNATION") + "\n"&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:06:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27300#M2065</guid>
      <dc:creator>KateNewell</dc:creator>
      <dc:date>2021-12-10T21:06:30Z</dc:date>
    </item>
    <item>
      <title>Re: Text Formatting in Python</title>
      <link>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27301#M2066</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Thanks!&lt;BR /&gt;...and sorry to be totally useless &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;BR /&gt;...but I also can't figure out the text formatting.&lt;BR /&gt;I only need the LegText to be adjusted...but I can't get the formatting correct either.&amp;nbsp; Also not sure if making the spacing negative is appropriate (as I want the spacing to go smaller)&lt;BR /&gt;Below is what I tried, and I am not finding much help on the resource center about formatting text in python...&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#iteate through each row, update appropriate table/text
for row in NHRows:
&amp;nbsp; if len(LegText.text) &amp;gt; 32:
&amp;nbsp;&amp;nbsp;&amp;nbsp; LegText.text = '&amp;lt;CHR spacing="-25"&amp;gt;'LegText'&amp;lt;/CHR&amp;gt;'
&amp;nbsp;&amp;nbsp;&amp;nbsp; LegNum.text = LegNum.text + row.getValue("SUBAREA_CODE") + "\n"
&amp;nbsp;&amp;nbsp;&amp;nbsp; LegText.text = LegText.text + row.getValue("LAND_USE_DESIGNATION") + "\n"
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; LegNum.text = LegNum.text + row.getValue("SUBAREA_CODE") + "\n"
&amp;nbsp;&amp;nbsp;&amp;nbsp; LegText.text = LegText.text + row.getValue("LAND_USE_DESIGNATION") + "\n"&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I believe what you are looking for can be found here under your local help files.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"C:\Program Files (x86)\ArcGIS\Desktop10.0\help\esri_csHTML_ArcMap.chm"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, here is the code I used to get a large comment field to print over multiple lines.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;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):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(line) == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; line = word
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; line = line+" "+word
&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(line)&amp;gt;50:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print line
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; entry = entry+"\r\n"+line
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; line=""
print line
entry = entry+"\r\n"+line
elm.text = entry&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:06:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27301#M2066</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-10T21:06:33Z</dc:date>
    </item>
    <item>
      <title>Re: Text Formatting in Python</title>
      <link>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27302#M2067</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The help document reference VB and from what I can tell, the syntax for text formatting in python is a bit different.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This seems like something that should be really easy...but for the life of me I am just not getting it &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Jul 2012 15:27:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27302#M2067</guid>
      <dc:creator>KateNewell</dc:creator>
      <dc:date>2012-07-17T15:27:07Z</dc:date>
    </item>
    <item>
      <title>Re: Text Formatting in Python</title>
      <link>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27303#M2068</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;The help document reference VB and from what I can tell, the syntax for text formatting in python is a bit different.&lt;BR /&gt;This seems like something that should be really easy...but for the life of me I am just not getting it &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am a little confused, it is the formatting style for map document elements. What text are you trying to format?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Jul 2012 16:55:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27303#M2068</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-07-17T16:55:13Z</dc:date>
    </item>
    <item>
      <title>Re: Text Formatting in Python</title>
      <link>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27304#M2069</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;here is the code I used to get a large comment field to print over multiple lines.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;FYI - there's a really handy standard python module for wrapping text: &lt;/SPAN&gt;&lt;A href="http://docs.python.org/library/textwrap.html"&gt;textwrap.&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Jul 2012 17:15:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27304#M2069</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2012-07-17T17:15:38Z</dc:date>
    </item>
    <item>
      <title>Re: Text Formatting in Python</title>
      <link>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27305#M2070</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Not sure the text wrapping will work, as the "LegText" element is associated and needs to be aligned with the "LegNum" Element.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&amp;nbsp; The data is being pulled from a table using search cursors.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Jul 2012 19:00:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27305#M2070</guid>
      <dc:creator>KateNewell</dc:creator>
      <dc:date>2012-07-17T19:00:41Z</dc:date>
    </item>
    <item>
      <title>Re: Text Formatting in Python</title>
      <link>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27306#M2071</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks everyone for all the ideas....I finally figured out a solution that will work using string slicing and carriage returns.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Still not sure on the python syntax for formatting text, but ended up not going that route. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#iteate through each row, update appropriate table/text
for row in NHRows:
&amp;nbsp; if len(row.LAND_USE_DESIGNATION) &amp;gt; 32:
&amp;nbsp;&amp;nbsp;&amp;nbsp; LegNum.text = LegNum.text + row.SUBAREA_CODE + "\n" + " " + "\n"
&amp;nbsp;&amp;nbsp;&amp;nbsp; LegText.text = LegText.text + row.LAND_USE_DESIGNATION[0:32] + "\n" + row.LAND_USE_DESIGNATION[32:] + "\n"
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; LegNum.text = LegNum.text + row.SUBAREA_CODE + "\n" 
&amp;nbsp;&amp;nbsp;&amp;nbsp; LegText.text = LegText.text + row.LAND_USE_DESIGNATION + "\n"&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]16177[/ATTACH]&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:06:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/text-formatting-in-python/m-p/27306#M2071</guid>
      <dc:creator>KateNewell</dc:creator>
      <dc:date>2021-12-10T21:06:36Z</dc:date>
    </item>
  </channel>
</rss>

