Skip to content

Instantly share code, notes, and snippets.

@nickrsan
Last active October 26, 2019 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickrsan/33a685c5f85c1846fb8d437773f98e47 to your computer and use it in GitHub Desktop.
Save nickrsan/33a685c5f85c1846fb8d437773f98e47 to your computer and use it in GitHub Desktop.
Converts all ArcMap documents (.mxd) in the script's folder into ArcGIS Pro Projects - requires amaptor (available via pip and at https://github.com/ucd-cws/amaptor). Has infrastructure to convert everything in a folder into the same project, but would need some retooling for it. Must be run from an ArcGIS Pro python environmental w/amaptor inst…
import os
import amaptor
base_folder = os.path.split(os.path.abspath(__file__))[0]
def make_project(item, base_folder=base_folder, skip_items=()):
"""
Makes the .mxd in item into an ArcGIS Pro project in the same folder with the same name
:param item: a full path to an ArcMap Document
"""
if type(item) is not list:
full_path = os.path.join(base_folder, item)
new_path = full_path.replace(".mxd", ".aprx")
if not os.path.exists(new_path) and item not in skip_items:
pro_version = amaptor.Project(full_path)
pro_version.active_map.name = new_name(item) # rename the map from "layers" to match the name of the document
new_path = full_path.replace(".mxd", ".aprx")
pro_version.save_a_copy(new_path)
else: # the else statement covers a legacy, where items were manually passed in by name - if you passed
# a list of items, you could have it convert every item in that list into the same project.
# Leaving it in case it helps someone using os.walk to make everything within a folder into the same project
base_item = item[0]
print(base_item)
full_path = os.path.join(base_folder, base_item)
pro_version = amaptor.Project(full_path)
for sub_item in item[1:]:
print(sub_item)
full_sub_path = os.path.join(base_folder, sub_item)
added_map = pro_version.new_map(os.path.split(full_sub_path)[1], full_sub_path, template_df_name="Layers")
added_map.name = os.path.splitext(sub_item)[0]
new_path = full_path.replace(".mxd", ".aprx")
pro_version.save_a_copy(new_path)
def new_name(item):
if "\\" in item:
item = item.split("\\")[-1] # get the last item in the path - could also use os.path.split here
return os.path.splitext(item)[0] # then strip the extension
if __name__ == "__main__":
items = [item for item in os.listdir(base_folder) if item.endswith(".mxd")]
skip_items = ["",] # list of filenames (in the current folder) to skip converting
for item in items:
print(item)
make_project(item, base_folder, skip_items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment