# import what we need
import lxml
from lxml import etree
import csv
import os
from os.path import join

# where are the eads?
ead_path = 'path/to/EADs' # <-- you have to change this

# where is the output csv?
output_csv = 'path/to/output.csv' # <-- you have to change this

# "top level" extents xpath
extents_xpath = '//ead/archdesc/did//physdesc/extent'
# component extents xpath
component_extents_xpath = '//ead/archdesc/dsc//physdesc/extent'
# all extents xpath
all_extents = '//extent'

# open and write header row of csv
with open(output_csv, 'ab') as csv_file:
    writer = csv.writer(csv_file, dialect='excel')
    writer.writerow(['Filename', 'XPath', 'Original Extent'])

# creates a function to get extents
def getextents(xpath):
    # go through those files
    for filename in os.listdir(ead_path):
        tree = etree.parse(join(ead_path, filename))
        # keep up with where we are
        print "Processing ", filename
        # parse and go through all component extents
        extents = tree.xpath(xpath)
        for i in extents:
            # identify blank extents
            extent = i.text
            extent_path = tree.getpath(i)
            with open(output_csv, 'ab') as csvfile:
                writer = csv.writer(csvfile, dialect='excel')
                try:
                    writer.writerow([filename, extent_path, extent])
                except:
                    writer.writerow([filename, extent_path, 'ISSUE EXTENT'])

# close the csv
csvfile.close()
                
# get extents       
getextents(all_extents) # <-- you'll have to change this to get the extents you want, "top level," component level or all (i want all)
@@ -19,20 +19,8 @@
 eed%0A
-import lxml%0A
 from
@@ -896,17 +896,17 @@
    print
- 
+(
 %22Process
@@ -910,17 +910,18 @@
 essing %22
-,
+ +
  filenam
@@ -921,16 +921,17 @@
 filename
+)
 %0A       
@@ -1024,17 +1024,22 @@
     for 
-i
+extent
  in exte
@@ -1102,12 +1102,22 @@
 tent
- = i
+_text = extent
 .tex
@@ -1157,17 +1157,22 @@
 getpath(
-i
+extent
 )%0A      
@@ -1366,16 +1366,21 @@
 , extent
+_text
 %5D)%0A     
@@ -1478,41 +1478,8 @@
 T'%5D)
-%0A%0A# close the csv%0Acsvfile.close()
 %0A   
@@ -1513,16 +1513,16 @@
        %0A
-
 getexten
@@ -1647,8 +1647,9 @@
 ant all)
+%0A

Fixes to get code running