How to Read Excel xlsx File and convert to CSV by Pandas

In this tutorial, we will show you how to read a .xlsx file (an Excel file) and then converting to CSV (Comma Separated Values) by using Pandas (A Python library).

Read and convert Excel .xlsx file into CSV by Pandas

In this tutorial, we will show you how to read a .xlsx file (an Excel file) and then converting to CSV (Comma Separated Values) by using Pandas (A Python library).

Step by step to read and convert xlsx file

Step 1:

Import the pandas into Python program:

import pandas as pd_csv

Step 2:

Load the workbook (.xlsx file) that you want to convert to CSV:

dt_dict = pd_csv.read_excel(‘test_Excel.xlsx’, sheet_name=”Product Information”, usecols=[‘Product Name’, ‘Status’])

The above line of code specifies:

  • Our Excel file – test_Excel.xlsx
  • Load one of its sheets “Product Information”
  • From that sheet, only return two columns i.e. Product Name and Status

This is our sample sheet:

Pandas-xlsx-sample

Step 3:

Using the data frame to_csv method

This line of code converts the loaded Excel file into CSV:

dt_dict.to_csv(index=False)

Complete code and output

By combining all of the above code, we have the following Python program:

Output:

Saving xlsx data into CSV file

The above code only generated CSV based on an Excel xlsx file.

For saving that comma separated values into a CSV file, you may use the data frame to_csv method as follows:

Output:

The above code creates a new CSV file with the name = “csv_data.csv” in the same directory where Python code file exists.

As we opened the above generated CSV file, this is how it looks in Notepad:

Pandas-xlsx-csv-notepad