I’ve been working with Excel files for a while now, and one way I find useful to convert xlsx to csv python is by using the xlrd and csv modules. It’s a straightforward approach, especially if you’re handling older .xlsx files. First, make sure you have xlrd installed:
pip install xlrd
Then, here’s the Python script to do the job:
import xlrd
import csv
def csv_from_excel():
wb = xlrd.open_workbook('excel.xlsx')
sh = wb.sheet_by_name('Sheet1')
with open('your_csv_file.csv', 'w', newline='') as your_csv_file:
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in range(sh.nrows):
wr.writerow(sh.row_values(rownum))
# runs the csv_from_excel function:
csv_from_excel()
This will open the .xlsx file and write its contents to a .csv file. It’s simple, and works well for me!