Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert XLSX to CSV correctly using python [closed]

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

like image 912
geogeek Avatar asked Nov 20 '13 19:11

geogeek


People also ask

Can openpyxl write to CSV?

Use the xlrd or openpyxl module to read xls or xlsx documents respectively, and the csv module to write. Alternately, if using Jython, you can use the Apache POI library to read either . xls or . xlsx , and the native CSV module will still be available.


1 Answers

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() 
like image 95
Hemant Avatar answered Oct 15 '22 00:10

Hemant