I'm working with an ArcGIS tool that converts data such as shapefiles to excel files. Basically, I need to get this ArcGIS tool to run in IDLE by replacing the GetParameterAsText part with a string(I was told this) to bring in a dataset, set a place to send the converted data, and to set the format(CSV). I'm not really certain how to do this as Python is a real challenge for me and I'm running very short on time. I would greatly appreciate it if someone could create the string and then explain how to fit my data in it but I would also be happy with a very strong explanation of how to do it. Essentially, treat it as if you're talking to a five year old(sounds silly, I know) The following is the data but I will also include the .py file as an attachment. Please and thank you!
# -*- coding: utf-8 -*- """ This script will convert a table to an excel spreadsheet. If the third-party module xlwt is available, it will use that. Otherwise, it will fall back to CSV. """ import arcpy import os #function def header_and_iterator(dataset_name): """Returns a list of column names and an iterator over the same columns"""
data_description = arcpy.Describe(dataset_name) fieldnames = [f.name for f in data_description.fields if f.type not in ["Geometry", "Raster", "Blob"]] def iterator_for_feature(): cursor = arcpy.SearchCursor(dataset_name) row = cursor.next() while row: yield [getattr(row, col) for col in fieldnames] row = cursor.next() del row, cursor return fieldnames, iterator_for_feature() #function def export_to_csv(dataset, output): """Output the data to a CSV file""" import csv
def _encode(x): if isinstance(x, unicode): return x.encode("utf-8") else: return str(x)
def _encodeHeader(x): return _encode(x.replace(".","_")) out_writer = csv.writer(open(output, 'wb')) header, rows = header_and_iterator(dataset) out_writer.writerow(map(_encodeHeader, header)) for row in rows: out_writer.writerow(map(_encode, row)) #function that exports the file to an xls file. #The following is xlwt functionality(its a module) def export_to_xls(dataset, output): """ Attempt to output to an XLS file. If xmlwt is not available, fall back to CSV. #CSV means "comma-separated values" XLWT can be downloaded from http://pypi.python.org/pypi/xlwt""" #xls is a file extension used on computers running Microsoft Windows to identify files created in Microsoft Excel until 2003. Replaced by xlsx for 2007. #XLWT is a library to create spreadsheet files compatible with MS Excel try: import xlwt except ImportError: arcpy.AddError("import of xlwt module failed") return header, rows = header_and_iterator(dataset)
# Make spreadsheet. workbook = xlwt.Workbook() #creates the workbook worksheet = workbook.add_sheet(os.path.split(dataset)[1]) # creates the worksheet in the workbook and splits the path
#Set up header row, freeze panes header_style = xlwt.easyxf("font: bold on; align: horiz center") for index, colheader in enumerate(header): worksheet.write(0, index, colheader.replace(".","_")) worksheet.set_panes_frozen(True) worksheet.set_horz_split_pos(1) worksheet.set_remove_splits(True)
# Write rows for rowidx, row in enumerate(rows): for colindex, col in enumerate(row): worksheet.write(rowidx+1, colindex, col)
# All done. This line saves our data to the output location workbook.save(output) if __name__ == "__main__": dataset_name = arcpy.GetParameterAsText(0) output_file = arcpy.GetParameterAsText(1) format = arcpy.GetParameterAsText(2) #Determines where to export the data based on file type. if format == "CSV": export_to_csv(dataset_name, output_file) elif format == "XLS": try: export_to_xls(dataset_name, output_file) except: import traceback arcpy.AddError(traceback.format_exc()) else: raise ValueError("Don't know how to export to %r" % format) print "FINISHED"