Skip to content

Instantly share code, notes, and snippets.

@alexfriant
Created April 3, 2016 01:44
Show Gist options
  • Save alexfriant/343159882ffded91a451a7fd31986781 to your computer and use it in GitHub Desktop.
Save alexfriant/343159882ffded91a451a7fd31986781 to your computer and use it in GitHub Desktop.
Extract attachments from ArcGIS file geodatabase
#############################################################################
# Requirements: ArcGIS 10.0 or higher, Python 2.7 or higher
#
# Description: Give this script an attachment table in a file geodatabase,
# and it will extract all the files and place them into the
# folder you designate.
#
# Note: Since attachments can have the same file name (i.e. Image00001.jpg)
# this script appends the attachment id to the file name so that every
# file is uniquely named in the folder you designate.
#
# License: Public Domain
##############################################################################
import arcpy
from arcpy import da
import os
inTable = arcpy.GetParameterAsText(0)
fileLocation = arcpy.GetParameterAsText(1)
with da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID']) as cursor:
for item in cursor:
attachment = item[0]
filenum = str(item[2]) + "_"
filename = filenum + str(item[1])
open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())
del item
del filenum
del filename
del attachment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment