Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm struggling with a formatting issue that comes from inserting text into a Text Element object in ArcGIS using Python. I want to print a table of values of which all but the last column are numbers. My python script loops through a list and applies formatting to each element like this:

#Text element is referenced:
tableTextQuad1 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "tableTextQuad1")[0]
#All of the work occurs within a function that takes the table, x postion, y position, and text element reference (txtBox1) as its arguments)
#A clone of the element is created and used by the newCellText
newCellText = txtBox1.clone("_clone")  
#A for loop is used to loop through lines in a table, and each line is formatted like so:
reformat1 = '{:>3}      {:>6.2f}      {:>3}      {:>7.2f}      {:>7.2f}     {:<10}'.format(t[0], t[1], t[2], t[3], t[4], t[5])
myList.append(reformat1)
myList.append('\n')
#To get rid of commas I do the following:   
alpha = "', '".join(myList).replace("', '", "")
#Then the alpha string is sent to the cloned text object    
newCellText.text = '{:}'.format(alpha)
newCellText.elementPositionX = x1
newCellText.elementPositionY = y1

When I don't include the last element (t[5]) the table looks great...like this: enter image description here

But as soon as I try adding the string t[5] (which are directions like North, South...) to the line everything in the line gets shifted and seems to ignore the width and character spacing allocation I set in .format(). The result looks like this: enter image description here

I've tried many different formatting parameters for t[5] but nothing seems to work the way I want it. Can anyone tell me why all the numbers would adhere to formatting rules like right/left justify and width but the inclusion of letters on the same line messes it all up? What is the correct formatting I should use? Many thanks in advance for help!

*Update - I have also tried adding a carriage return to the \n, so that '\r\n' is appended to each line, but this does not fix my problem. Could it be a bug?

share|improve this question
    
Can you provide a little more of your code to show how you are updating the text element? Also, can you specify which method you used to create that text element, please? With that I should be able to quickly test. –  PolyGeo Aug 7 '13 at 22:02
add comment

1 Answer 1

up vote 2 down vote accepted

Try making the text element a little bit wider and saving it again. Looks like you have the thing set to justify, so it's trying to fit everything in the line width as best it can. If you switch that off it might lay out better. And if possible, set it to a fixed-width font like Consolas or Courier New, trying to line things up with spaces with a proportional font without settable tab stops is an exercise in futility.

share|improve this answer
    
Wow, the font was the issue! Using Arial was causing the shifting, but I've tried Consolas and it looks great. Thank you! –  user18412 Aug 7 '13 at 23:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.