0

I'm trying to write a script in Python 2.7 that would convert all .xls and .xlsx files in the current directory into .csv with preserving their original file names.

With help from other similar questions here (sadly, not sure who to credit for the pieces of code I borrowed), here's what I've got so far:

import xlrd
import csv
import os

def csv_from_excel(xlfile):
     wb = xlrd.open_workbook(xlfile)
     sh = wb.sheet_by_index(0)
     your_csv_file = open(os.path.splitext(sxlfile)[0], 'wb')
     wr = csv.writer(your_csv_file, dialect='excel', quoting=csv.QUOTE_ALL)
     for rownum in xrange(sh.nrows):
         wr.writerow(sh.row_values(rownum))
     your_csv_file.close()

 for file in os.listdir(os.getcwd()):
      if file.lower().endswith(('.xls','.xlsx')):
         csv_from_excel(file)

I have two questions:

1) I can't figure out why the program when run, only converts one file and doesn't iterate through all files in the current directory.

2) I can't figure out how to keep the original filename through the conversion. I.e. that an output file has the same name as an input.

Thank you

1 Answer 1

5

One possible solution would be using glob and pandas.

excel_files = glob('*xls*')

 for excel in excel_files:
    out = excel.split('.')[0]+'.csv'
    df = pd.read_excel(excel, 'Sheet1')
    df.to_csv(out) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.