56

I am looking for a python library or any help to convert .XLSX files to .CSV files.

2
  • 14
    Another option is using pandas: df = pd.read_excel("./data.xlsx") and then df.to_csv("./data.csv", sep=",") Commented May 9, 2019 at 17:01
  • Pandas actually uses xlrd under the hood. Commented Nov 22, 2019 at 18:45

1 Answer 1

84

Read your excel using the xlrd module and then you can use the csv module to create your own csv.

Install the xlrd module in your command line:

pip install xlrd

Python script:

import xlrd
import csv

def csv_from_excel():
    wb = xlrd.open_workbook('excel.xlsx')
    sh = wb.sheet_by_name('Sheet1')
    your_csv_file = open('your_csv_file.csv', 'w')
    wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)

    for rownum in range(sh.nrows):
        wr.writerow(sh.row_values(rownum))

    your_csv_file.close()

# runs the csv_from_excel function:
csv_from_excel()
Sign up to request clarification or add additional context in comments.

6 Comments

at least use xlsx in your example(sort of yours at least it looks like it came from elsewhere :P) since thats what was asked :P
@JoranBeasley this is not my answer. It came from the stackoverflow only. I tried to paste the hyperlink but somehow it was not coming in my comment. if for some reason I've violated any rule or policy of forum I'm really sorry for that.
Sometimes if the excel contains unicode characters you may need to specify the encoding, when creating/openning the destination file.
this solution no longer works. xlrd cannot handle xlsx anymore.
[pandas library] (geeksforgeeks.org/convert-excel-to-csv-in-python) can convert xlsx to csv
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.