"""
Example: We have a folder of .jpg photographs and .pdf files;
We want to add these files as attachments to our input dataset.
Create a match table, enable input dataset if needed, add attachments.
"""
import arcpy
input_dataset = r"C:\Data\City.gdb\Parcels"
input_path = arcpy.Describe(input_dataset).path
# GenerateAttachmentsMatchtable parameters
# Required
match_table = r"C:\Data\City.gdb\parcelsMatchtable"
key_field = 'PARCELS'
# Optional
data_filter ='*.jpg; *.pdf' # Remove *.pdf to load only the .jpg images as another option.
rel_path = "RELATIVE"
match_pattern = 'ANY'
# pic_folder required for match table, optional for add attachments if rel_path is ABSOLUTE
pic_folder = r"C:\Pictures\Parcels"
arcpy.management.GenerateAttachmentMatchTable(input_dataset, pic_folder, match_table, key_field,
data_filter, rel_path, match_pattern)
# Check if input dataset is enabled for attachments
rel_class = arcpy.Describe(input_dataset).relationshipClassNames
rel_class_path = (f"{input_path}\\{relclass[0]}")
rel_class_att = arcpy.Describe(relclassPath).isAttachmentRelationship
if rel_class_att:
pass
else:
arcpy.management.EnableAttachments(input_dataset)
# AddAttachments parameters
add_match_table = match_table
input_join_fld = 'ObjectID'
match_join_fld = 'MatchID'
match_path_fld = 'Filename'
# Add attachments from generated match table to input data ATTACH table
arcpy.management.AddAttachments(input_dataset, input_join_fld, add_match_table,
match_join_fld, match_path_fld, pic_folder)