[GIS] How to change text legend orientation from right to left reading with arcpy

arcgis-10.3arcgis-desktoparcpylayoutslegend

I working on mxd with ArcMap 10.3 . The default's legend properties is- Layout – left to right reading (Marked with a red ellipse) :

enter image description here

when it unmarked i get this legend:
enter image description here

i want to know if there a way to mark the default box automatic from right to left reading option so the result will be like that (Very important is that all the legend items will be in straight line from left side):

enter image description here

Does anyone know of a way to do this with arcpy?

UPDATE:

I tried MrBubbles answer and the result was:

enter image description here

while i wanted result like that (i made it manually for the example):
enter image description here

The answer of MrBubbles didn't help me because it very important that all the words in the legend will be right to left configuration
How can i do it?

Best Answer

The trick to doing this is to first create a legend style item either in your user style file or in a custom style file. For the sake of simplicity, I'll show you how to do it using your user style.

  1. In ArcMap, go to Customize > Style Manager You should see a folder in the Style Manager which references the user style file ArcGIS auto-creates to store all your little styling preferences. Click on it so the the right window pane populates with a bunch of empty style folders. ArcGIS Style Manager

You need to create a custom Legend Style item. Specifically, to do what you want to do, you need to create a a Horizontal Legend Style Item.

  1. Right-Click on the Legend Folder in your User Style File and select New > Horizontal... from the context menu. Don't select Horizontal Bar. That's not what you want.

  2. Under the Arrangement tab, select the third radio button from the top, which displays the legend patch all the way to the right. Then press OK and give your legend style item a name. For the sake of simplicity, I named mine 'Justified-Left-Patch-Right' enter image description here

enter image description here

  1. Close the Style Manager.

  2. Next you need to just insert a plain old legend. In ArcMap, select Insert > Legend and just zip through the Legend Wizard. We need the Legend Element in order to update it with your preferred style. If you plan on creating maps dynamically, you would just keep a default legend in the MXD and then update it as needed prior to export. I don't know of a way to just automatically insert a default legend through arcpy.

  3. Once you've got a default legend and your preferred style item in place, we can update the legend with your preferred style through arcpy.

    # Reference the Current MXD
    mxd = arcpy.mapping.MapDocument("CURRENT")
    # Acquire the default legend item manually inserted into the MXD
    legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
    # Reference your custom style item from your user style
    # Note: "USER_STYLE" is a keyword in the ListStyleItems function which 
    # automatically references the custom style file ArcGIS auto-creates for 
    # your user profile
    styleItem = arcpy.mapping.ListStyleItems("USER_STYLE", "Legend Items", "Justified-Left-Patch-Right")[0] 
    
  4. At this point, you're ready to update your legend with your preferred style. As you can see, I have the normal default legend in my mxd. MXD prior to updating legend with preferred style

  5. All you need to do now, is iterate through the items in the Legend, and update them with your style item.

    for lyr in legend.listLegendItemLayers():
        legend.updateItem(lyr, styleItem)
    

    enter image description here

You can find more information about updating your legend style through arcpy as well as some additional workflows on with legends in the LegendElement documentation.

Note that while this is the method ESRI provides to update the legend through arcpy, I found it to be fairly unstable. I experienced several hard crashes of ArcMap when doing it. It seemed to occur any time I attempted to update the legend layers after immediately after making a configuration change to my style item in the style manager.

Hope this helps.

Related Question