Wednesday 21 June 2017

python - Dynamic Text in ArcMap 10.0 and arcpy - getting user name in specific format


I need to put in a specific text into a text element. The initials of the user who built the map.


With arcpy I am trying to set the text element with dynamic text. If I put



 

in the user text element it gives me, correctly, the user's user name. I want to use the user name in a python dictionary to put the users initials instead.


dictUserElmt = { "jperez" : "JP" }


Using arcpy to read the text of my text element, textElement.text, returns (again correctly)


u''.  

How do I get the actual output of the dynamic text "jperez" so I can use my dictionary to have the text element set to "JP".


I'm sure my wording is confusing because I am confused on how to approach. Thanks.



Answer




Is something like below what you are looking to do? For a given username look up and return their initials from the dictionary?


>>> d = {"jperez":"JP", "cooperc":"CC"}
>>> user = "jperez"
>>> if user in d.keys():
... inits = d[user]
...
>>> inits
'JP'

Now, that said, are you getting the author name from the MXD properties?



>>> import arcpy
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> a = mxd.author
>>> print a
>>> a
u''
>>> a = mxd.author
>>> print a
Chad Cooper


If so, that has to be set by the user (you can see I got u'' on my trial before I explicitly set my name as the MXD author) and saved in the MXD I believe. In your code, you have your name as jperez, which looks like a Windows login maybe? I wonder how using the getpass module to get the current login name would work? That way you could go by those, which pretty much stay the same.


>>> import getpass
>>> getpass.getuser()
'chad'
>>>

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...