A script to export attachments from ArcGIS Pro 3, based on a "CleanID" field on the base (non-attach) table. Will export files in the passed directory, named like `ATT{CLEAN_ID}.jpg`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from functools import lru_cache | |
import arcpy | |
from arcpy import da | |
base_table = arcpy.GetParameterAsText(0) | |
files_dir = arcpy.GetParameterAsText(1) | |
if not (base_table and files_dir): | |
arcpy.AddError("Expected two arguments: the base table, and a directory to save files") | |
if base_table.endswith("__ATTACH"): | |
arcpy.AddWarning("Expected a base table as first parameter, does it really end in __ATTACH already?") | |
attach_table = base_table + "__ATTACH" | |
@lru_cache(maxsize=1024) | |
def get_clean_id(global_id) -> str: | |
global_id = str(global_id) | |
field = arcpy.AddFieldDelimiters(base_table, "GlobalID") | |
where_clause = "{} = '{}'".format(field, global_id) | |
with da.SearchCursor(base_table, ["GlobalID", "CleanID"], where_clause) as cursor: | |
items = list(cursor) | |
if len(items) != 1: | |
raise Exception("Expected a single item in the base table with global id") | |
item = items[0] | |
return str(item[1]) | |
with da.SearchCursor(attach_table, ['DATA', "REL_GLOBALID", "ATT_NAME"]) as cursor: | |
for item in cursor: | |
data, rel_globalid, att_name = item | |
att_name = str(att_name) | |
if "." not in att_name: | |
# Take a guess | |
att_name += ".jpg" | |
clean_id = get_clean_id(rel_globalid) | |
# Modify this line to change the file name, the first `{}` will be replaced with the clean_id, | |
# the second `{}` will be replaced by the "ATT_NAME` field from the attach table ("Photo 1.jpg") | |
filename = "ATT_{}_{}".format(clean_id, att_name) | |
with open(files_dir + os.sep + filename, "wb") as f: | |
f.write(data.tobytes()) | |
del data | |
del item |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment