lalalala
lalalala

Reputation: 563

convert multiple excel files to csv files with python

Is there a way to convert multiple files? Can I use glob.glob?

import sys

sys.path.insert(0,'D:/apera/Python27/xlrd-0.9.3')

import xlrd

import csv

ExcelFile = "D:/apera/Workspace/Sounding/sounding010.xls"
CSVFile = "D:/apera/Workspace/Sounding/sounding010.csv"

def Convert(ExcelFile, SheetName, CSVFile):
    wb = xlrd.open_workbook(ExcelFile)
    ws = wb.sheet_by_name(SheetName)
    csvfile = open(CSVFile, 'wb')
    wr = csv.writer(csvfile, quoting=csv.QUOTE_ALL, delimiter=';')

    for rownum in xrange(ws.nrows):
         wr.writerow(
         list(x.encode('latin1')
              for x in ws.row_values(rownum)))

    csvfile.close()

Convert(ExcelFile, "INPUT", CSVFile)

Upvotes: 1

Views: 873

Answers (1)

Bob Haffner
Bob Haffner

Reputation: 8483

Yes, glob combined with os will work

import os
import glob

os.chdir("yourfolder")
for f in glob.glob("*.xls"):
    #call your conversion function

Upvotes: 1

Related Questions