ArcPy: Batch export of attachments from geodatabase feature class with custom naming

If you have worked with attachments within an ArGIS geodatabase, you might have needed to take the attachments out of the geodatabase to use in another system or to process them separately. Unfortunately, there is no geoprocessing tool that would allow you to export all attachments to files, however this workflow is supported with arcpy and Python. There is a KB article at Esri site that shows the code necessary to export attachments of a feature class into an output folder.

This workflow is based, however, on using the attributes from the attachments table – %featureclassname%__ATTACH. But what if you would like to export the attachments naming output files using attributes of the source feature that has attachments associated with it? In this case, you would need to find out what feature every attachment is associated to (this is exposed via a relationship class) and then use this value to name the output file. To get all the information needed regarding the relationship class properties a Describe object for the relationship class can be used. Of course when exporting the attachments and naming output files using a source feature attribute, you have to make sure it is unique, otherwise the later exported attachment files will overwrite the previous ones.

This tool will handle Unicode characters in the attribute field you would like to use for naming output files, so it will work for users working in various locales, however, there is no validation for the output file name in terms of operating system restrictions on legal file names, but its easy to fix, for instance, using regex.

The code that will do this with some extra comments to help you follow along:

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s