Convert XLSX to CSV correctly using python

PythonExcelPython 2.7Csv

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongeogeekView Question on Stackoverflow
Solution 1 - PythonHemantView Answer on Stackoverflow