[Tutor] converting xls to csv

Nick Burgess burgess.nick at gmail.com
Sun May 31 06:51:06 CEST 2009


Hi list,

I am trying to make this code work.  I don't have any experience with
defining things and this is my second program.  The error returmed is
"SyntaxError: invalid syntax"

code:




#!/usr/bin/python
import cvs
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  to CSV!"

    convertXLS2CSV(r"F:\python\MasterList.xls")


More information about the Tutor mailing list