save xls to csv/dbf without Excel/win32com.client

Tim Chase python.list at tim.thechases.com
Sat Jun 5 21:31:23 EDT 2010


On 06/05/2010 06:47 PM, noydb wrote:
> Is there a way to save a .xls file (the first worksheet) as a .dbf
> or .csv without opening an instance of Excel with win32com.client
> (been awhile, is this the best module these days for v2.5)?  In case a
> computer does not have Excel (2007) installed.

Use the "xlrd" module[1]

############################
import csv
import xlrd

FILE_NAME = 'example.xls'
wb = xlrd.open_workbook(FILE_NAME)

for name in wb.sheet_names():
   out = file('%s.csv' % name, 'wb')
   writer = csv.writer(out)
   sheet = wb.sheet_by_name(name)
   for row in xrange(sheet.nrows):
     writer.writerow([
       sheet.cell_value(row, col)
       for col in xrange(sheet.ncols)
       ])
   out.close()
#############################

You say you only want the first sheet, so adjust accordingly.

-tkc

[1]
http://pypi.python.org/pypi/xlrd/







More information about the Python-list mailing list