How to create a match table that has a column which takes in an attribute field's IDs as input?

1124
5
Jump to solution
08-03-2016 02:41 PM
Sergio_Vazquez
New Contributor II

I am using ArcGIS Pro.

I have a folder with 198 separate PDF files that I want to link to a feature class (input the pathname into the field.

I have tried creating a Match Table (and enabled the Add Attachments Tool) - the result was a Table with two columns:

1) FAC_ID

2) ATTACHMENT

I am able to fill the attachment column with the file names and ".pdf" at the end. However, I only want the FAC_ID field entries to be filled with the first 6 characters of the PDF file name. This way I can join the table with the feature class based on the FAC_ID field.

Also, I just realized that the end goal is to create hyperlinks from the PDF file names in the attribute table but the attachment field is only taking in the PDF file name and not the pathname.

Below is the code I used:

import csv

import arcpy

import os

import sys

input = r"C:\CSF\CSF.gdb\FeatureClass"

inputField = "FAC_ID"

matchTable = r"C:\Arcpy Scripts\matchtable.csv"

matchField = "FAC_ID"

pathField = "Attachments"

picFolder = r"C:/PDF_Folder"

try:

    # create a new Match Table csv file

    writer = csv.writer(open(matchTable, "wt"), 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(".pdf") > -1:

            writer.writerow([str(file).replace(".pdf", ""), 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 Exception as err:

    print(err.args[0])

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

I will leave you to figure where you  want what to go, but parsing IF! there is a space in all cases is simply this

>>> a ="CSF001 - As-Built"

>>> a.split(" ")[0]

'CSF001'

split on the space, take the first piece (ie [0])

View solution in original post

5 Replies
DanPatterson_Retired
MVP Esteemed Contributor

are you just trying to join picFolder to the output here?

writer.writerow([str(file).replace(".pdf", ""), file])

                        picFolder+"/"+str(file).replace(".pdf","")....

or is it something else you are after?

Sergio_Vazquez
New Contributor II

Thanks for the quick response.

I think this is where the problem is - the section you called out is where I am trying to iterate through the folder where the PDFs are to fill in the match table.

Say I have a PDF file named "CSF001 - As-Built". I would want the FAC_ID column to only take in the "CSF001" and get rid of the rest.

Then, for the Attachment column, I would want the path name where the file is and the file name at the end so:

"C:\PDF_Folder\CSF001 - As-Built.pdf"

Then iterate through all 198 files and do the above ^

The table would look like this

FAC_ID                                       ATTACHMENT

CSF001                                      C:\PDF_Folder\CSF001 - As-Built.pdf

CSF002                                      C:\PDF_Folder\CSF002 - As-Built.pdf 

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

I will leave you to figure where you  want what to go, but parsing IF! there is a space in all cases is simply this

>>> a ="CSF001 - As-Built"

>>> a.split(" ")[0]

'CSF001'

split on the space, take the first piece (ie [0])

Sergio_Vazquez
New Contributor II

Awesome, I was able to concatenate everything the way I wanted and parse out the things I didn’t. Thanks for the pointers, Dan. This workflow will save me a ton of time.

Cheers,

Sergio

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

no problem... python study time

0 Kudos