Monday, February 06, 2006

xls2csv, using python win32com and excel

This python script change a bunch of excel files in cvs files, needs Excel installed on the computer.

#
# Save all Excel files in the current dir as csv files
# (CSV = comma separeted values)

from win32com.client import Dispatch
import os,time

xlCSV = 6 #a vba constant for this fileformat
xlApp = Dispatch("Excel.Application")
xlApp.Visible = 1

def saveAsCSV(filename):
xlApp.Workbooks.Open(filename)
xlApp.ActiveWorkbook.SaveAs(filename[:-3]+"csv",xlCSV)
xlApp.ActiveWorkbook.Close(SaveChanges=0)

for root, dirs, files in os.walk(''):
for file in files:
if ".xls" in file:
print file
saveAsCSV(os.path.abspath(file))
xlApp.Quit()
del xlApp

If you need to do this on linux, you can try that program: xls2csv.

No comments: