Convert Excel to CSV using Python (tool included)

Need to convert Excel to CSV 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 stored\File name.xlsx')
read_file.to_csv (r'Path to store the CSV file\File 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 stored\File name.xlsx', sheet_name='Your Excel sheet name')
read_file.to_csv (r'Path to store the CSV file\File name.csv', index = None, header=True)

In the next section, I’ll review the complete steps to convert your Excel file to CSV using Python. I’ll also share the code to create a simple tool to perform the conversion:

File Conversion Tool

Steps to Convert Excel to CSV 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.

For example, I stored an Excel file about products under the following path:

C:\Users\Ron\Desktop\Test\Product_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. In my case, I specified the following path:

C:\Users\Ron\Desktop\Test\New_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 stored\File name.xlsx')
read_file.to_csv (r'Path to store the CSV file\File 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:\Users\Ron\Desktop\Test\Product_List.xlsx')
read_file.to_csv (r'C:\Users\Ron\Desktop\Test\New_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:

Example of file in a folder

Tool to Convert Excel files to CSV files

You may use the tool below in order to convert your Excel files to CSV files.

This tool is based on the tkinter package, which can be used to build a Graphical User Interface (GUI) in Python:

import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import pandas as pd

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 300, height = 300, bg = 'lightsteelblue2', relief = 'raised')
canvas1.pack()

label1 = tk.Label(root, text='File Conversion Tool', bg = 'lightsteelblue2')
label1.config(font=('helvetica', 20))
canvas1.create_window(150, 60, window=label1)

def getExcel ():
    global read_file
    
    import_file_path = filedialog.askopenfilename()
    read_file = pd.read_excel (import_file_path)
    
browseButton_Excel = tk.Button(text="      Import Excel File     ", command=getExcel, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 130, window=browseButton_Excel)

def convertToCSV ():
    global read_file
    
    export_file_path = filedialog.asksaveasfilename(defaultextension='.csv')
    read_file.to_csv (export_file_path, index = None, header=True)

saveAsButton_CSV = tk.Button(text='Convert Excel to CSV', command=convertToCSV, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 180, window=saveAsButton_CSV)

def exitApplication():
    MsgBox = tk.messagebox.askquestion ('Exit Application','Are you sure you want to exit the application',icon = 'warning')
    if MsgBox == 'yes':
       root.destroy()
     
exitButton = tk.Button (root, text='       Exit Application     ',command=exitApplication, bg='brown', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 230, window=exitButton)

root.mainloop()

Run the code, and you’ll see this display:

Convert Excel to CSV using Python

First, click on the ‘Import Excel File‘ button:

Tool to Convert Excel to CSV using Python

You’ll then see a dialogue box to assist you in finding your Excel file. Locate your Excel file that you’d like to convert to CSV, and then click on Open:

Dialogue box

Now click on the ‘Convert Excel to CSV‘ button:

Convert Excel to CSV using Python

In the dialogue box, type a name for your new CSV file. For example, I typed the name of ‘New_Products.’ Once you’re done, click on Save:

Excel to CSV

You’ll then see the new CSV file at your specified location:

Folder with files

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