Home arrow GIS Data & Resources arrow Scripts and Code arrow Python arrow PYTHON! convert MS Excel to CSV...
PYTHON! convert MS Excel to CSV... PDF Print E-mail

Written by Kevin Bell,

Do you have to get flat files from MS Excel and push the data around in your GIS?  If so, this sample is for you!  I don't typically want the fancy formatting stuff in Excel, and converting to comma seperated values can simplify the data. 

In order to read the CSV file, you can use Python's CSV module: 

http://docs.python.org/lib/csv-examples.html

def convertXLS2CSV(aFile): 
    '''converts a MS Excel file to csv w/ the same name in the same directory'''
 
     print "------ beginning to convert XLS to CSV ------"

     try:
          import win32com.client, os
          excel = win32com.client.Dispatch('Excel.Application')
 
         fileDir, fileName = os.path.split(aFile)
         nameOnly = os.path.splitext(fileName)
         newName = nameOnly[0] + ".csv"
         outCSV = os.path.join(fileDir, newName)
         workbook = excel.Workbooks.Open(aFile)
         workbook.SaveAs(outCSV, FileFormat=24) # 24 represents xlCSVMSDOS
         workbook.Close(False)
         excel.Quit()
         del excel
  
         print "...Converted " + nameOnly + " to CSV"
    except:
         print ">>>>>>> FAILED to convert " + aFile + " to CSV!"

 convertXLS2CSV(r"C:\temp\cropRotation.xls")

 


Users' Comments  
 

No comment posted

Add your comment

15, Feb. 2008
Last Updated ( 15, Feb. 2008 )
 
< Prev   Next >

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for