Python from excel to csv

In this article, we will be dealing with the conversion of Excel (.xlsx) file into .csv.  There are two formats mostly used in Excel :

  1. (*.xlsx) : Excel Microsoft Office Open XML Format Spreadsheet file.
  2. (*.xls) : Excel Spreadsheet (Excel 97-2003 workbook).

Let’s Consider a dataset of a shopping store having data about Customer Serial Number, Customer Name, Customer ID, and Product Cost stored in Excel file. 

check all used files here.

Python3

import pandas as pd

df = pd.DataFrame(pd.read_excel("Test.xlsx"))

df

Output : 

shopping dataframe

Now, let’s see different ways to convert an Excel file into a CSV file :

Method 1: Convert Excel file to CSV file using the pandas library.

Pandas is an open-source software library built for data manipulation and analysis for Python programming language. It offers various functionality in terms of data structures and operations for manipulating numerical tables and time series. It can read, filter, and re-arrange small and large datasets and output them in a range of formats including Excel, JSON, CSV.

For reading an excel file, using the read_excel() method and convert the data frame into the CSV file, use to_csv() method of pandas.

Code:

Python3

import pandas as pd

read_file = pd.read_excel ("Test.xlsx")

read_file.to_csv ("Test.csv"

                  index = None,

                  header=True)

df = pd.DataFrame(pd.read_csv("Test.csv"))

df

 Output: 

shopping dataframefile show

Method 2: Convert Excel file to CSV file using xlrd and CSV library.

xlrd is a library with the main purpose to read an excel file. 

csv is a library with the main purpose to read and write a csv file.

Code:

Python3

import xlrd 

import csv

import pandas as pd

sheet = xlrd.open_workbook("Test.xlsx").sheet_by_index(0)

col = csv.writer(open("T.csv"

                      'w'

                      newline=""))

for row in range(sheet.nrows):

    col.writerow(sheet.row_values(row))

df = pd.DataFrame(pd.read_csv("T.csv"))

df

 Output: 

shopping dataframefile show

Method 3: Convert Excel file to CSV file using openpyxl and CSV library.

openpyxl is a library to read/write Excel 2010 xlsx/xlsm/xltx/xltm files.It was born from lack of existing library to read/write natively from Python the Office Open XML format.

Code:

Python3

import openpyxl

import csv

import pandas as pd

excel = openpyxl.load_workbook("Test.xlsx")

sheet = excel.active

col = csv.writer(open("tt.csv",

                      'w'

                      newline=""))

for r in sheet.rows:

    col.writerow([cell.value for cell in r])

df = pd.DataFrame(pd.read_csv("tt.csv"))

df

 Output: 

shopping dataframefiles show

I want to write a Python script that reads in an Excel spreadsheet and saves some of its worksheets as CSV files.

How can I do this?

I have found third-party modules for reading and writing Excel files from Python, but as far as I can tell, they can only save files in Excel (i.e. *.xls) format. If I’m wrong here, some example code showing how to do what I’m trying to do with these modules would be appreciated.

I also came across one solution that I can’t quite understand, but seems to be Windows-specific, and therefore would not help me anyway, since I want to do this in Unix. At any rate, it’s not clear to me that this solution can be extended to do what I want to do, even under Windows.

Mel's user avatar

Mel

5,72710 gold badges40 silver badges42 bronze badges

asked May 29, 2012 at 15:44

kjo's user avatar

The most basic examples using the two libraries described line by line:

  1. Open the xls workbook
  2. Reference the first spreadsheet
  3. Open in binary write the target csv file
  4. Create the default csv writer object
  5. Loop over all the rows of the first spreadsheet
  6. Dump the rows into the csv

import xlrd
import csv

with xlrd.open_workbook('a_file.xls') as wb:
    sh = wb.sheet_by_index(0)  # or wb.sheet_by_name('name_of_the_sheet_here')
    with open('a_file.csv', 'wb') as f:   # open('a_file.csv', 'w', newline="") for python 3
        c = csv.writer(f)
        for r in range(sh.nrows):
            c.writerow(sh.row_values(r))

import openpyxl
import csv

wb = openpyxl.load_workbook('test.xlsx')
sh = wb.active
with open('test.csv', 'wb') as f:  # open('test.csv', 'w', newline="") for python 3
    c = csv.writer(f)
    for r in sh.rows:
        c.writerow([cell.value for cell in r])

Sayyor Y's user avatar

Sayyor Y

1,0102 gold badges15 silver badges27 bronze badges

answered May 29, 2012 at 16:36

Zeugma's user avatar

ZeugmaZeugma

30.8k8 gold badges67 silver badges80 bronze badges

2

Using pandas will be a bit shorter:

import pandas as pd

df = pd.read_excel('my_file', sheetname='my_sheet_name')  # sheetname is optional
df.to_csv('output_file_name', index=False)  # index=False prevents pandas to write row index

# oneliner
pd.read_excel('my_file', sheetname='my_sheet_name').to_csv('output_file_name', index=False)

answered Jul 25, 2017 at 8:09

FabienP's user avatar

FabienPFabienP

2,9701 gold badge21 silver badges24 bronze badges

4

As of December 2021 and Python 3:

The openpyxl API has changed sufficiently (see https://openpyxl.readthedocs.io/en/stable/usage.html) that I have updated this part of the answer by @Boud (now @Zeugma?), as follows:

import openpyxl
import csv

wb = openpyxl.load_workbook('test.xlsx')
sh = wb.active # was .get_active_sheet()
with open('test.csv', 'w', newline="") as file_handle:
    csv_writer = csv.writer(file_handle)
    for row in sh.iter_rows(): # generator; was sh.rows
        csv_writer.writerow([cell.value for cell in row])

@Leonid made some helpful comments — in particular:

csv.writer provides some additional options e.g. custom delimiter:

csv_writer = csv.writer(fout, delimiter='|', quotechar='"', quoting=csv.QUOTE_MINIMAL)

HTH

answered Sep 28, 2020 at 9:32

jtlz2's user avatar

jtlz2jtlz2

7,2987 gold badges61 silver badges111 bronze badges

4

Use the xlrd or openpyxlmodule 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.

answered May 29, 2012 at 15:47

Charles Duffy's user avatar

Charles DuffyCharles Duffy

275k43 gold badges377 silver badges432 bronze badges

2

First read your Excel spreadsheet into Pandas. The code below will import your Excel spreadsheet into Pandas as an OrderedDict which contains all of your worksheets as DataFrames. Then, simply use the worksheet_name as a key to access specific worksheet as a DataFrame and save only the required worksheet as a csv file by using df.to_csv(). Hope this will work in your case.

import pandas as pd
df = pd.read_excel('YourExcel.xlsx', sheet_name=None)
df['worksheet_name'].to_csv('output.csv')  

akshayk07's user avatar

akshayk07

1,9841 gold badge20 silver badges30 bronze badges

answered Aug 1, 2019 at 17:30

Ashu007's user avatar

Ashu007Ashu007

7351 gold badge9 silver badges13 bronze badges

Need to convert an Excel file to a CSV file using Python?

If so, you may use the following template to convert your file:

import pandas as pd

read_file = pd.read_excel (r'Path where the Excel file is storedFile name.xlsx')
read_file.to_csv (r'Path to store the CSV fileFile name.csv', index = None, header=True)

And if you have a specific Excel sheet that you’d like to convert, you may then use this template:

import pandas as pd

read_file = pd.read_excel (r'Path where the Excel file is storedFile name.xlsx', sheet_name='Your Excel sheet name')
read_file.to_csv (r'Path to store the CSV fileFile name.csv', index = None, header=True)

In the next section, you’ll see the complete steps to convert your Excel file to a CSV file using Python.

Step 1: Install the Pandas Package

If you haven’t already done so, install the Pandas package. You may use the following command to install Pandas (under Windows):

pip install pandas

Step 2: Capture the Path where the Excel File is Stored

Next, capture the path where the Excel file is stored on your computer.

Here is an example of a path where an Excel file is stored:

C:UsersRonDesktopTestProduct_List.xlsx

Where ‘Product_List‘ is the Excel file name, and ‘xlsx‘ is the file extension.

Step 3: Specify the Path where the New CSV File will be Stored

Now you’ll need to specify the path where the new CSV file will be stored. For example:

C:UsersRonDesktopTestNew_Products.csv

Where ‘New_Products‘ is the new file name, and ‘csv‘ is the file extension.

Step 4: Convert the Excel to CSV using Python

For the final part, use the following template to assist you in the conversion of Excel to CSV:

import pandas as pd

read_file = pd.read_excel (r'Path where the Excel file is storedFile name.xlsx')
read_file.to_csv (r'Path to store the CSV fileFile name.csv', index = None, header=True)

This is how the code would look like in the context of our example (you’ll need to modify the paths to reflect the location where the files will be stored on your computer):

import pandas as pd

read_file = pd.read_excel (r'C:UsersRonDesktopTestProduct_List.xlsx')
read_file.to_csv (r'C:UsersRonDesktopTestNew_Products.csv', index = None, header=True)

Once you run the code (adjusted to you paths), you’ll get the new CSV file at your specified location.

You may also want to check the following source for the steps to convert CSV to Excel using Python.

In this article, we will show you how to convert an excel file to the CSV File (Comma Separated Values) using python.

Assume we have taken an excel file with the name sampleTutorialsPoint.xlsx containing some random text. We will return a CSV File after converting the given excel file into a CSV file.

sampleTutorialsPoint.xlsx

Player Name Age Type Country Team Runs Wickets
Virat Kohli 33 Batsman India Royal Challengers Bangalore 6300 20
Bhuvaneshwar Kumar 34 Batsman India Sun Risers Hyderabad 333 140
Mahendra Singh Dhoni 39 Batsman India Chennai Super Kings 4500 0
Rashid Khan 28 Bowler Afghanistan Gujarat Titans 500 130
Hardik Pandya 29 All rounder India Gujarat Titans 2400 85
David Warner 34 Batsman Australia Delhi Capitals 5500 12
Kieron Pollard 35 All rounder West Indies Mumbai Indians 3000 67
Rohit Sharma 33 Batsman India Mumbai Indians 5456 20
Kane Williamson 33 Batsman New Zealand Sun Risers Hyderabad 3222 5
Kagiso Rabada 29 Bowler South Africa Lucknow Capitals 335 111

Method 1: Converting Excel to CSV using Pandas Module

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Import the pandas module (Pandas is a Python open-source data manipulation and analysis package)

  • Create a variable to store the path of the input excel file.

  • Read the given excel file content using the pandas read_excel() function(reads an excel file object into a data frame object).

  • Convert the excel file into a CSV file using the to_csv() function(converts object into a CSV file) by passing the output excel file name, index as None, and header as true as arguments.

  • Read the output CSV file with the read_csv() function(loads a CSV file as a pandas data frame) and convert it to a data frame object with the pandas module’s DataFrame() function.

  • Show/display the data frame object.

Example

The following program converts an excel file into a CSV file and returns a new CSV file

import pandas as pd inputExcelFile ="sampleTutorialsPoint.xlsx" excelFile = pd.read_excel (inputExcelFile) excelFile.to_csv ("ResultCsvFile.csv", index = None, header=True) dataframeObject = pd.DataFrame(pd.read_csv("ResultCsvFile.csv")) dataframeObject

Output

On executing, the above program will generate the following output −

|  index | Player Name         | Age | Type      | Country          | Team                      |Runs  | Wickets |
|--------|---------------------|-----|-----------|------------------|---------------------------|----- |---------|
|   0    |Virat Kohli          |   33|Batsman    |   India          |Royal Challengers Bangalore| 6300 |   20    |
|   1    |Bhuvaneshwar Kumar   |   34|Batsman    |   India          |Sun Risers Hyderabad       | 333  |   140   |
|   2    |Mahendra Singh Dhoni |   39|Batsman    |   India          |Chennai Super Kings        | 4500 |    0    |
|   3    |Rashid Khan          |   28|Bowler     |   Afghanistan    |Gujarat Titans             | 500  |   130   |
|   4    |Hardik Pandya        |   29|All rounder|   India          |Gujarat Titans             | 2400 |    85   |
|   5    |David Warner         |   34|Batsman    |   Australia      |Delhi Capitals             | 5500 |    12   |
|   6    |Kieron Pollard       |   35|All rounder|   West Indies    |Mumbai Indians             | 3000 |    67   | 
|   7    |Rohit Sharma         |   33|Batsman    |   India          |Mumbai Indians             | 5456 |    20   |
|   8    |Kane Williamson      |   33|Batsman    |   New Zealand    |Sun Risers Hyderabad       | 3222 |     5   |
|   9    |Kagiso Rabada        |   29|Bowler     |   South Africa   |Lucknow Capitals           | 335  |    111  |

In this program, we use the pandas read_excel() function to read an excel file containing some random dummy data, and then we use the to csv() function to convert the excel file to csv. If we pass the index as a false argument, the final CSV file does not contain the index row at the beginning. Then we converted the CSV to a data frame to see if the values from the excel file were copied into the CSV file.

Method 2: Converting Excel to CSV using openpyxl and CSV Modules

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword, to import the openpyxl(Openpyxl is a Python package for interacting with and managing Excel files. Excel 2010 and later files with the xlsx/xlsm/xltx/xltm extensions are supported. Data scientists use Openpyxl for data analysis, data copying, data mining, drawing charts, styling sheets, formula addition, and other operations) and CSV modules.

pip install openpyxl
  • Create a variable to store the path of the input excel file.

  • To create/load a workbook object, pass the input excel file to the openpyxl module’s load_workbook() function (loads a workbook).

  • Opening an output CSV file in write mode with open() and writer() functions to convert an input excel file into a CSV file.

  • Using the for loop, traverse each row of the worksheet.

  • Use the writerow() function, to write cell data of the excel file into the result CSV file row-by-row.

Example

The following program converts an excel file into a CSV file and returns a new CSV file −

import openpyxl import csv inputExcelFile = 'sampleTutorialsPoint.xlsx' newWorkbook = openpyxl.load_workbook(inputExcelFile) firstWorksheet = newWorkbook.active OutputCsvFile = csv.writer(open("ResultCsvFile.csv", 'w'), delimiter=",") for eachrow in firstWorksheet.rows: OutputCsvFile.writerow([cell.value for cell in eachrow])

Output

On executing, the above program a new CSV file (ResultCsvFile.csv) will be created with data of Excel.

In this program, we have an excel file with some random dummy data, which we load as an openpyxl work and set to use using the active attribute. Then we made a new CSV file and opened it in writing mode, then we went through the excel file row by row and copied the data into the newly created CSV file.

Conclusion

In this tutorial, we learned how to read an excel file and convert it to an openpyxl workbook, then how to convert it to a CSV file and remove the index, and finally how to convert the CSV file to a pandas data frame.

Convert Excel to CSV in Python

In this article, we will be dealing with the conversion of Excel (.xlsx) file into .csv.  There are two formats mostly used in Excel :

  1. (*.xlsx) : Excel Microsoft Office Open XML Format Spreadsheet file.
  2. (*.xls) : Excel Spreadsheet (Excel 97-2003 workbook).

Let’s Consider a dataset of a shopping store having data about Customer Serial Number, Customer Name, Customer ID, and Product Cost stored in Excel file.

check all used files here.

import pandas as pd

df = pd.DataFrame(pd.read_excel("Test.xlsx"))

df

Output : 

shopping dataframe

Now, let’s see different ways to convert an Excel file into a CSV file :

Method 1: Convert Excel file to CSV file using the pandas library.

Pandas is an open-source software library built for data manipulation and analysis for Python programming language. It offers various functionality in terms of data structures and operations for manipulating numerical tables and time series. It can read, filter, and re-arrange small and large datasets and output them in a range of formats including Excel, JSON, CSV.

For reading an excel file, using the read_excel() method and convert the data frame into the CSV file, use to_csv() method of pandas.

Code:

import pandas as pd

read_file = pd.read_excel ("Test.xlsx")

read_file.to_csv ("Test.csv"

                  index = None,

                  header=True)

df = pd.DataFrame(pd.read_csv("Test.csv"))

df

Output: 

shopping dataframefile show

Method 2: Convert Excel file to CSV file using xlrd and CSV library.

xlrd is a library with the main purpose to read an excel file.

csv is a library with the main purpose to read and write a csv file.

Code:

import xlrd 

import csv

import pandas as pd

sheet = xlrd.open_workbook("Test.xlsx").sheet_by_index(0)

col = csv.writer(open("T.csv"

                      'w'

                      newline=""))

for row in range(sheet.nrows):

    col.writerow(sheet.row_values(row))

df = pd.DataFrame(pd.read_csv("T.csv"))

df

shopping dataframefile show

Method 3: Convert Excel file to CSV file using openpyxl and CSV library.

openpyxl is a library to read/write Excel 2010 xlsx/xlsm/xltx/xltm files.It was born from lack of existing library to read/write natively from Python the Office Open XML format.

Code:

import openpyxl

import csv

import pandas as pd

excel = openpyxl.load_workbook("Test.xlsx")

sheet = excel.active

col = csv.writer(open("tt.csv",

                      'w'

                      newline=""))

for r in sheet.rows:

    col.writerow([cell.value for cell in r])

df = pd.DataFrame(pd.read_csv("tt.csv"))

df

Output: 

shopping dataframefiles show

In this article, you will learn how to convert Excel to CSV using Python Pandas.

Pandas is open source, fast, flexible, powerful, easy-to-use tools and are widely used for data manipulation and data analysis. It provides functionality to read data from various file formats, such as CSV, MS Excel, etc. As we know, Microsoft Excel has been used in many different applications and spreadsheet representations. We can easily organize a large amount of data by using this. It is also rich in features like calculation, graphing tools, pivot tables, producing graphs and charts, and much more. CSV is a widely used file format that stores data in a tabular format. Most popular programming languages have tools or applications to support the CSV file format.

CSV files have some advantages over Excel files. The CSV files are faster and consume less memory space, whereas Excel consumes more memory space while importing data. That’s why it’s important to convert excel to CSV.

Install Pandas in Python

Before starting work on Python Pandas, we need to install this module. So, open your command prompt, activate your virtual environment, and install it using the following command.

pip install pandas

On successful installation, it returns something like this-

Installing collected packages: pandas
Successfully installed pandas-1.0.1

Suppose we have the following excel file containing school program participant data.

Convert Excel to CSV

Reading Excel file using Python Pandas

Here, we have first imported the Pandas module and passed the excel sheet file as a parameter in read_excel() method. This method reads the data into a Pandas DataFrame. It accepts filename in the first parameter and sheet name in the second parameter. DataFrame is the key data structure of Pandas.

import pandas as pd

df = pd.read_excel("school_event.xlsx") 

Convert Excel to CSV Python

Pandas DataFrame provides the to_csv() method to convert data from a dataframe to a CSV. If we provide the file location as an argument, then data is written to the file otherwise, CSV string is returned. This method provides many options, like we can provide a custom delimiter, specify the columns to be used, ignore the index column, and much more to the generated CSV file. Here is the complete code to convert Excel to CSV.

import pandas as pd

df = pd.read_excel("school_event.xlsx")

df.to_csv ("school.csv", index = None,  header=True) 

Python Pandas Excel to CSV

You can see how we can simply achieve more work from just 2-3 lines of code. This is one of the big advantages of Python Pandas.

Related Articles


How to read data from excel file using Python Pandas

Python program to check leap year

Django Export Model Data to CSV

Python Converting a CSV File to a MySQL Table

Write Python Pandas Dataframe to CSV

Convert JSON to CSV using Python

Generate and download a CSV file in Django

Python Pandas CSV to Dataframe

How to read data from excel file in Python

Python read JSON from URL requests

Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler — Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml

python-ways-to-convert-excel-to-csv-file-python-3-feature-image

In this tutorial, we’re gonna look at 3 ways to convert an Excel file to CSV file in Python 3. With each way, we use one of these module: xlrd, openpyxl and pandas.

Related Posts:
– How to read/write CSV files in Python
– How to read/write Excel files in Python
– Node.js Extract MySQL Data to Excel(.xlsx) File – using exceljs

Convert Excel file to CSV file using xlrd

With this method, we:
– use xlrd module to open and read Excel file
– then use csv module to write and save CSV file

Example code:

import xlrd
import csv

with xlrd.open_workbook('ozenero.xlsx') as wb:
    sh = wb.sheet_by_index(0)  # wb.sheet_by_name('sheet_name')
    with open('ozenero.csv', 'w', newline="") as f:
        col = csv.writer(f)
        for row in range(sh.nrows):
            col.writerow(sh.row_values(row))

Convert Excel file to CSV file using openpyxl

With this method, we:
– use openpyxl module to open and read Excel file
– then use csv module to write and save CSV file

Example code:

import openpyxl
import csv

wb = openpyxl.load_workbook('ozenero.xlsx')
sh = wb.active
with open('ozenero.csv', 'w', newline="") as f:
    col = csv.writer(f)
    for row in sh.rows:
        col.writerow([cell.value for cell in row])

Convert Excel file to CSV file using pandas

With this method, we use only one module pandas to read Excel file and write data to CSV file.

Example code:

import pandas as pd

df = pd.read_excel('ozenero.xlsx')  # parameter (sheetname='sheet_name') is optional
df.to_csv('ozenero.csv', index=False)  # index=True to write row index

Понравилась статья? Поделить с друзьями:
  • Python for word in line
  • Python for excel скачать
  • Python for excel книга
  • Python for excel zumstein pdf на русском
  • Python for excel files