Select to view content in your preferred language

Write multiple lines to a text element

2234
2
Jump to solution
08-29-2012 12:30 PM
DaveJordan1
Regular Contributor
I am writing a script that will produce mailing labels in a layout.  Basiclally, I have 30 text elements (3x10) on a page that I want to populate with the contents from a text file, which is pipe delimited.  For each line in the text file I am breaking out each line for the label using string.split('|').  Now I am stumped on how to write multiple lines of text to a text element in the layout. 

My Question: How can I create new lines to a text element without overwriting the previous line of text?

here is a sample of the first 5 lines of the text file.  The first line is the header, so it gets ignored, the next two lines aren't actual labels but they get printed as if they are.  the last two lines are the actual label material.

OWNER1|OWNER2|MAIL1|MAIL2|CITY_STATE_ZIP5_ZIP4
Dave_Jordan|Page1| | |
|||
BAYVIEW LOAN SERVICING LLC| |895 SW 30 AVE STE 202| |POMPANO BCH FL 33064 0
BUCKSHIN, ROBERT| |4052 FOUNTAIN PALM RD| |COCOA FL 32926 311

# Import arcpy module import arcpy  #Get user supplied parameters fName = arcpy.GetParameterAsText(0) #Dist = arcpy.GetParameterAsText(1) #AppName = arcpy.GetParameterAsText(2)  # Local variables: mxd = arcpy.mapping.MapDocument("Current") mxd.activeView='PAGE_LAYOUT'  arcpy.RefreshActiveView() # Process: arcpy.AddMessage("==============================================") arcpy.AddMessage("Generating mailing labels from "+fName) #if arcpy.Exists(fName): try:  arcpy.AddMessage("Processing file...")  f1 = open(fName,'r')  lines = f1.readlines()  cnt = 0  #Start the count at zero to skip the header line in the text file  for line in lines:   arcpy.AddMessage("----Label "+str(cnt))   for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):    if elm.name == "lbl"+str(cnt):     for val in line.split('|'):      if val != ' ': #Here is where I get stumped.  The AddMessage will print to the screen the way I want to brint to the label...       elm.text = ?????       #arcpy.AddMessage(val)     #elm.text = line   cnt = cnt + 1  f1.close()  arcpy.RefreshActiveView() except Exception, e:  import traceback  f1.close()  map(arcpy.AddError, traceback.format_exc().split("\n"))  arcpy.AddError(str(e))
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
\r\n inserts a new line command.

var = "OWNER1|OWNER2|MAIL1|MAIL2|CITY_STATE_ZIP5_ZIP4 \r\nDave_Jordan|Page1| | | \r\n|||\r\nBAYVIEW LOAN SERVICING LLC| |895 SW 30 AVE STE 202| |POMPANO BCH FL 33064 0\r\nBUCKSHIN, ROBERT| |4052 FOUNTAIN PALM RD| |COCOA FL 32926 311" elm.text = var

View solution in original post

0 Kudos
2 Replies
MathewCoyle
Honored Contributor
\r\n inserts a new line command.

var = "OWNER1|OWNER2|MAIL1|MAIL2|CITY_STATE_ZIP5_ZIP4 \r\nDave_Jordan|Page1| | | \r\n|||\r\nBAYVIEW LOAN SERVICING LLC| |895 SW 30 AVE STE 202| |POMPANO BCH FL 33064 0\r\nBUCKSHIN, ROBERT| |4052 FOUNTAIN PALM RD| |COCOA FL 32926 311" elm.text = var
0 Kudos
DaveJordan1
Regular Contributor
Thank you - That works.  I used the string.replace to replace the delimiters (|) with Newline Returns (\n\r):

 for line in lines:
  for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
   if elm.name == "lbl"+str(cnt):
    elm.text = line.replace('|','\r\n')
  cnt = cnt + 1
0 Kudos