0

I'm having a tough time aligning text in my text box using arcpy in ArcGIS 10.3. This is my initial text:

Now I want to insert some more text to each line and align them vertically. Normally, I would insert a tab (or two, if necessary), and basically it works, but in the exported PDF i get an annoying ? for each tab I inserted.

I did it like this:

string.join(['UTM Zone:', '\t', utm_zone], sep='')

I also tried using the unicode sign for tab u'\u0009 with the same result. Formatting tags available in ArcMap does not help here, nor does Using text formatting tags. I guess there is some proper way of using HTML here, but I can't figure it out.

  • Instead of using string.join() I just concatenate string parts with + and it works fine: text_element.text = string1 + "\t " + string2 – GISGe Mar 20 '18 at 9:10
  • No, same result. string.join() is not the problem, it is the actual notation of tab I need to figure out. – s6hebern Mar 20 '18 at 9:21
  • What if you print to PDF instead of exporting? – GISGe Mar 20 '18 at 9:41
  • If you use Arial, try with another font, see this bug – GISGe Mar 20 '18 at 9:42
  • As a workaround you could have 2 separate text elements, one with the fixed text and the second one with the variable text (with the anchor point on the left) – GISGe Mar 20 '18 at 9:45
2

You need to use a combination of str.format() and str.ljust() functions to make it work. It requires some patience, and it depends on the font of the text. If you changed the font, you need also to change the settings of ljust().

In this example, I used Calibri font as you mentioned in the comment.

Here is the code that worked for me in your case:

elem.text = "UTM Zone:{0}\nDatum:{1}\nEPSG code: {2}".format("38N".rjust(4),"WGS84".rjust(13),"32638".ljust(9))

Or you can use right-alignment '>' without ljust() and it will give you the same result:

elem.text = "UTM Zone:{:>4}\nDatum:{:>13}\nEPSG code:{:>7}".format("38N","WGS84","32638")

and here is the output in ArcGIS:

enter image description here

and in PDF after exporting:

enter image description here

  • You're right, it needs some patience, but the rjust() and ljust() functions look pretty to me. Thanks – s6hebern Mar 20 '18 at 13:04
  • You are welcome. Glad it worked :) – ahmadhanb Mar 20 '18 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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