TransWikia.com

Export Attachments using Python script to modify file name [ArcGIS Desktop 10.8.1]

Geographic Information Systems Asked by Kathryn Wesson on January 18, 2021

I’m trying to export photo attachments from a railroad crossings dataset taken with ArcGIS Collector, and would like to customize the naming convention so that the file name contains the crossing number. I stumbled across a script from @Midavalo that accomplishes this, however the files exported are no longer JPEG format. I have almost no knowledge of Python.

Feature table:

enter image description here

Attachment table:

enter image description here

Modified script:

import arcpy, os
from collections import defaultdict

inFC = r'C:UsersDesktopscratchCustomExportAttachmentsCopyFeatures.gdbCrossings'
inTable = r'C:UsersDesktopscratchCustomExportAttachmentsCopyFeatures.gdbCrossings__ATTACH'
fileLocation = r'C:UsersDesktopscratchCustomExportAttachmentsAttachments'

# Get dictionary of ObjectID and associated field value
myFeatures = dict()
with arcpy.da.SearchCursor(inFC, ['OID@', 'CROSSING']) as cursor:
    for row in cursor:
        myFeatures[row[0]] = row[1]

# Create dictionary to count usage of the field value (to increment files)
valueUsage = defaultdict(int)

# Loop through attachments, incrementing field value usage, and using that
# increment value in the filename
with arcpy.da.SearchCursor(inTable, ['ATTACHMENTID', 'ATT_NAME', 'REL_OBJECTID']) as cursor:
    for row in cursor:
        if row[3] in myFeatures:
            attachment = row[0]
            fieldValue = myFeatures[row[3]] # Value of specified field in feature class
            valueUsage[fieldValue] += 1 # Increment value
            filename = "{0}_{1}".format(fieldValue, valueUsage[fieldValue]) # filename = FieldValue_1
            output = os.path.join(fileLocation, filename) # Create output filepath
            open(output, 'wb').write(attachment.tobytes()) # Output attachment to file

Output folder:

enter image description here

Why are the photo attachments exporting as "File" instead of JPEG, and how should I change the script in order to make this happen?

One Answer

Best guess: the filename is only the name, no extension. You could probably do a quick addition in the code if you know all the files are JPEG.

Change the 2nd to last line to:

output = os.path.join(fileLocation, filename +".jpg")

This should get your JPEG extension back. Obviously this solution could be a problem if you have different file types you're exporting.

Correct answer by KHibma on January 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP