import arcpy
import os
# Set the path to the geodatabase and feature class
gdb_path = r"C:\Projects\Inf2024_1_1\Inf2024_1.gdb"
feature_class = "AsBuilt"
full_fc_path = os.path.join(gdb_path, feature_class)
# Define the field containing the image path
image_path_field = "ABPATH"
# Set up the path to the workspace and check out the necessary license for working with attachments
arcpy.env.workspace = gdb_path
arcpy.CheckOutExtension("Data")
# Create an attachment table if it doesn't exist
attachment_table = os.path.join(gdb_path, feature_class + "_ATTACH")
# Check if the feature class already has an attachment table
##if not arcpy.management.HasAttachments(full_fc_path):
## arcpy.management.CreateAttachmentTable(full_fc_path)
# Start an edit session
edit = arcpy.da.Editor(gdb_path)
edit.startEditing(False, True) # False means not in versioned, True means undo/redo enabled
edit.startOperation()
try:
# Start an update cursor to loop through the features
with arcpy.da.UpdateCursor(full_fc_path, ["OBJECTID", image_path_field]) as cursor:
for row in cursor:
object_id = row[0]
image_path = row[1] # The path to the image file
# Ensure the image path is valid
if image_path and os.path.exists(image_path):
try:
# Attach the image to the feature using the OBJECTID
#arcpy.management.AttachData(full_fc_path, "OBJECTID", object_id, attachment_table, image_path)
arcpy.AddAttachments_management(full_fc_path, "OBJECTID", object_id, attachment_table, "S:\e\e65-1.tif")
print(f"Image '{image_path}' attached to feature with OBJECTID {object_id}.")
except Exception as e:
print(f"Error attaching image for OBJECTID {object_id}: {e}")
else:
print(f"Invalid or missing image path for feature with OBJECTID {object_id}.")
# Commit the changes
edit.stopOperation()
edit.stopEditing(True) # Commit changes to the geodatabase
print("Changes committed.")
except Exception as e:
# If an error occurs, undo all changes in the edit session
edit.stopOperation()
edit.stopEditing(False) # Discard changes
print(f"Error: {e}")
# Release the Data extension and end the session
arcpy.CheckInExtension("Data")
print("Script completed.")
This is the error.
Error attaching image for OBJECTID 1: Failed to execute. Parameters are not valid.
ERROR 000732: Match Table: Dataset 1 does not exist or is not supported
Failed to execute (AddAttachments).
Hi @Jack_in_the_box, take a look at the attached tool. It will convert a hyperlink field to attachments.
Does it work if you change the line
from: "S:\e\e65-1.tif"
to: r"S:\e\e65-1.tif"