Select to view content in your preferred language

Add a stylex file to a project using Python as part of a script.

394
1
02-23-2023 11:56 PM
MattHowe
Occasional Contributor

Is it possible to programmatically add a stylex file to a project?

I'm thinking about using the 'applySymbolFroGallery' method shown here but not all users would have the style file in their projects to choose the correct symbology.

My workflow would be to check if the style file is there, else add it. Then add the layer to map then change the symbology from a selected one in the gallery.

0 Kudos
1 Reply
by Anonymous User
Not applicable

This is untested, but I think the closest you can get is checking if its in the favorites, add it if not and then set it to add to new projects by setting the persistFavorite to true. This Add a style to all new projects reads like this will let you have access to the style from the project.

You can use python to find and update the Favorites.json file for your users- this is a section from my script to batch update the connections, works the same for favorited styles or if you set it as part of your scrip tyou can change it to get the current users AppData path.

 

pc_dict = {"username": "pc name or id",
           "username1": "pc name or id",
           "username2": "pc name or id"}

fav_file_json = """{
        "Items": [
                <<COPY WHAT IS IN THE FAVORITES HERE, ADDING THE STYLE AT THE END - you could also do this dynamically incase there are differences in favorites... >>
                }, {
            "TypeId": "file_stylx",
            "Path": "X:\\Styles\\thestyle.stylx",
            "Id": "",
            "url": null,
            "name": "Style Name",
            "persistFavorite": true
        }
              ]
            }"""


# ------------------------------------------------------------------------------
def set_user_favorites():
    for usr, cpuName in pc_dict.items():
        json_path = fr'\\{cpuName}\c$\Users\{usr}\AppData\Roaming\ESRI\ArcGISPro\Favorites'
        with open(os.path.join(json_path, 'Favorites.json'), "w") as outfile:
            try:
                print(f'Copying json to {json_path}')
                outfile.write(fav_file_json)
            except:
                print(f'Could not create json at {json_path}')

 

From there, I think you can use the

applySymbolFromGallery("Extent Transparent Wide Gray")

method.

0 Kudos