dashton

Python Scritp Help Moving from 9.3 to 10

Discussion created by dashton on Jun 5, 2011
Latest reply on Jun 10, 2011 by jskinner-esristaff
I have a python script that a co-worker wrote and she is no longer with company.  It works great but we just need to update it.  We no longer have a programmer for python on staff so I was hoping the community could help us do two things to this script:

1.  Convert it from using 9.3 (gp) to 10 (arcpy)

2.  Currently this script is using win32com.client to write a dbf table to an excel file.  It is writing to an .xls (windows Xcel version 97 -2003) and we would like to convert this write to version 2007 - 2010. 

Can you help me with either steps?



# ---------------------------------------------------------------------------
# TableToExcel.py
# ---------------------------------------------------------------------------

import sys, string, os, arcgisscripting, win32com.client
gp = arcgisscripting.create(9.3)

tablePath = gp.GetParameterAsText(0)
filePath = gp.GetParameterAsText(1)

xlApp = win32com.client.Dispatch("Excel.Application")
xlApp.Workbooks.Add()

fields = gp.ListFields(tablePath)

fieldNames = []
for field in fields:
    if (field.type <> "Geometry" and field.type <> "BLOB"):
        fieldNames.append(field.name)
        
rowNum = 1
columnNum = 1

for fieldName in fieldNames:
    xlApp.ActiveSheet.Cells(rowNum, columnNum).Value = fieldName
    columnNum = columnNum + 1

cur = gp.SearchCursor(tablePath)
row = cur.Next()
while row:
    rowNum = rowNum + 1
    columnNum = 1
    for fieldName in fieldNames:
        xlApp.ActiveSheet.Cells(rowNum, columnNum).Value = row.GetValue(fieldName)
        columnNum = columnNum + 1
            
    row = cur.Next()
    
del cur

xlApp.ActiveWorkbook.SaveAs(filePath)
xlApp.ActiveWorkbook.Close(SaveChanges=0)
xlApp.Quit()
del xlApp


Outcomes