Add Attachments (Data Management)
Summary
Adds file attachments to the records of a geodatabase feature class or table. The attachments are stored internally in the geodatabase in a separate attachment table that maintains linkage to the target dataset. Attachments are added to the target dataset using a match table that dictates for each input record (or an attribute group of records) the path to a file to add as an attachment to that record.
Learn more about geodatabase attachments
Learn more about working with the attachments geoprocessing tools
Illustration
Usage
Before attachments can be added using this tool, they must first be enabled using the Enable Attachments tool.
Attachments added using this tool will be copied internally to the geodatabase. The original attachment files will not be affected in any way. If the original files are modified, these changes will not be automatically made to the geodatabase attachment; to synchronize changes to the geodatabase, remove the affected attachments using the Remove Attachments tool, then add the modified files back as new attachments.
If your Input Dataset already contains a field that is the path to the attachments to add, and you do not want to use a separate Match Table, specify the same dataset for both the Input Dataset and Match Table. The tool will automatically select the Object ID field for both join fields, and you can specify which field from the input contains the paths to the attachment files.
-
Multiple files can be attached to a single feature class or table record. To accomplish this, the Match Table should contain multiple records for that input ID (for example, record 1 has an InputID of 1 and a pathname pic1a.jpg, and record 2 has an InputID of 1 and a pathname pic1b.jpg).
Syntax
Parameter | Explanation | Data Type |
in_dataset |
Geodatabase table or feature class to add attachments to. Attachments are not added directly to this table, but rather to a related attachment table that maintains linkage to the input dataset. The input dataset must be stored in a version 10.0 or later geodatabase, and the table must have attachments enabled. | Table View |
in_join_field |
Field from the Input Dataset that has values that match the values in the Match Join Field. Records that have join field values that match between the Input Dataset and the Match Table will have attachments added. This field can be an Object ID field or any other identifying attribute. | Field |
in_match_table |
Table that identifies which input records will have attachments added and the paths to those attachments. | Table View |
in_match_join_field |
Field from the match table that indicates which records in the Input Dataset will have specified attachments added. This field can have values that match Input Dataset Object IDs or some other identifying attribute. | Field |
in_match_path_field |
Field from the match table that contains paths to the attachments to add to Input Dataset records. | Field |
in_working_folder (Optional) |
Folder or workspace where attachment files are centralized. By specifying a working folder, the paths in the Match Path Field can be the short names of files relative to the working folder. For example, if loading attachments with paths like C:\MyPictures\image1.jpg, C:\MyPictures\image2.jpg, set the Working Folder to C:\MyPictures, then paths in the Match Path Field can be the short names such as image1.jpg and image2.jpg, instead of the longer full paths. | Folder |
Code Sample
The following code snippet illustrates how to use the AddAttachments tool in the Python window.
import arcpy
arcpy.AddAttachments_management(r"C:\Data\City.gdb\Parcels", "ParcelID", r"C:\Data\matchtable.csv", "ParcelID","Picture" , r"C:\Pictures")
The following script illustrates how to use the AddAttachments tool in a stand-alone script.
"""
Example: we have a folder of digital photographs of vacant homes; the photos
are named according to the ParcelID of the house in the picture. Let's add
these photos to a parcel feature class as attachments.
"""
import csv
import arcpy
import os
import sys
input = r"C:\Data\City.gdb\Parcels"
inputField = "ParcelID"
matchTable = r"C:\Data\matchtable.csv"
matchField = "ParcelID"
pathField = "Picture"
picFolder = r"C:\Pictures"
try:
# create a new Match Table csv file
writer = csv.writer(open(matchTable, "wb"), delimiter=",")
# write a header row (the table will have two columns: ParcelID and Picture)
writer.writerow([matchField, pathField])
# iterate through each picture in the directory and write a row to the table
for file in os.listdir(picFolder):
if str(file).find(".jpg") > -1:
writer.writerow([str(file).replace(".jpg", ""), file])
del writer
# the input feature class must first be GDB attachments enabled
arcpy.EnableAttachments_management(input)
# use the match table with the Add Attachments tool
arcpy.AddAttachments_management(input, inputField, matchTable, matchField, pathField, picFolder)
except:
print arcpy.GetMessages(2)