Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
Convert CSV to HTML Table in Python
Next article icon

Convert Excel to CSV in Python

Last Updated : 13 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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

# importing pandas as pd
import pandas as pd
  
# read an excel file and convert 
# into a dataframe object
df = pd.DataFrame(pd.read_excel("Test.xlsx"))
  
# show the dataframe
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

#importing pandas as pd
import pandas as pd
  
# Read and store content
# of an excel file 
read_file = pd.read_excel ("Test.xlsx")
  
# Write the dataframe object
# into csv file
read_file.to_csv ("Test.csv", 
                  index = None,
                  header=True)
    
# read csv file and convert 
# into a dataframe object
df = pd.DataFrame(pd.read_csv("Test.csv"))
  
# show the dataframe
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 all required library
import xlrd 
import csv
import pandas as pd
  
# open workbook by sheet index,
# optional - sheet_by_index()
sheet = xlrd.open_workbook("Test.xlsx").sheet_by_index(0)
  
# writer object is created
col = csv.writer(open("T.csv", 
                      'w', 
                      newline=""))
  
# writing the data into csv file
for row in range(sheet.nrows):
    # row by row write 
    # operation is perform
    col.writerow(sheet.row_values(row))
  
# read csv file and convert 
# into a dataframe object
df = pd.DataFrame(pd.read_csv("T.csv"))
  
# show the dataframe
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

# importe required libraries
import openpyxl
import csv
import pandas as pd
  
# open given workbook 
# and store in excel object 
excel = openpyxl.load_workbook("Test.xlsx")
  
# select the active sheet
sheet = excel.active
  
# writer object is created
col = csv.writer(open("tt.csv",
                      'w', 
                      newline=""))
  
# writing the data in csv file
for r in sheet.rows:
    # row by row write 
    # operation is perform
    col.writerow([cell.value for cell in r])
  
# read the csv file and 
# convert into dataframe object 
df = pd.DataFrame(pd.read_csv("tt.csv"))
  
# show the dataframe
df
                      
                       

 Output: 

shopping dataframefiles show


Next Article
Convert CSV to HTML Table in Python

A

abhishekkharmale
Improve
Article Tags :
  • Python
  • python-csv
  • Python-pandas
Practice Tags :
  • python

Similar Reads

  • Convert XML to CSV in Python
    XML (Extensible Markup Language) is widely used to store and transport structured data. However, for tasks like data analysis or spreadsheet editing, CSV (Comma-Separated Values) is far more user-friendly and easier to work with. To make XML data easier to process, we often convert it to a CSV file.
    2 min read
  • Convert CSV to Excel using Pandas in Python
    Pandas can read, filter, and re-arrange small and large datasets and output them in a range of formats including Excel. In this article, we will be dealing with the conversion of .csv file into excel (.xlsx). Pandas provide the ExcelWriter class for writing data frame objects to excel sheets. Syntax
    1 min read
  • Convert JSON to CSV in Python
    We can convert JSON to CSV in Python using the built-in json and csv modules. What is JSON and CSV?JSON is a lightweight, text-based data format commonly used to exchange data between web servers and clients. It stores data in key-value pairs and is typically used for data transmission in web applic
    3 min read
  • Convert CSV to HTML Table in Python
    CSV file is a Comma Separated Value file that uses a comma to separate values. It is basically used for exchanging data between different applications. In this, individual rows are separated by a newline. Fields of data in each row are delimited with a comma.Example :   Name, Salary, Age, No.of year
    2 min read
  • How to Convert JSON to Excel in Python
    JSON (JavaScript Object Notation) is a widely used data format that is both easy to read and write for humans and simple for machines to parse and generate. However, there may be times when we need to convert this JSON data into an Excel spreadsheet for analysis or reporting. In this article, we'll
    3 min read
  • Convert Excel to PDF Using Python
    Python is a high-level, general-purpose, and very popular programming language. Python programming language (latest Python 3) is being used in web development, Machine Learning applications, along with all cutting-edge technology in Software Industry. In this article, we will learn how to convert an
    2 min read
  • Convert nested JSON to CSV in Python
    In this article, we will discuss how can we convert nested JSON to CSV in Python. An example of a simple JSON file: As you can see in the example, a single key-value pair is separated by a colon (:) whereas each key-value pairs are separated by a comma (,). Here, "name", "profile", "age", and "locat
    9 min read
  • Convert HTML table into CSV file in python
    CSV file is a Comma Separated Value file that uses a comma to separate values. CSV file is a useful thing in today's world when we are talking about machine learning, data handling, and data visualization. In this article, we will discuss how to convert an HTML table into a CSV file. Converting HTML
    2 min read
  • Convert PDF to CSV using Python
    Python is a high-level, general-purpose, and very popular programming language. Python programming language (the latest Python 3) is being used in web development, Machine Learning applications, along with all cutting-edge technology in Software Industry. Python Programming Language is very well sui
    2 min read
  • Python | Convert an HTML table into excel
    MS Excel is a powerful tool for handling huge amounts of tabular data. It can be particularly useful for sorting, analyzing, performing complex calculations and visualizing data. In this article, we will discuss how to extract a table from a webpage and store it in Excel format. Step #1: Converting
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences