Convert CSV to Excel using Python (tool included)

In this quick guide, I’ll show you how to convert a CSV to Excel using Python.

I’ll also include the Python code for a tool that can facilitate the conversion:

File conversion tool from CSV to Excel

To start, here is a template that you can use to convert a CSV to Excel using Python:

import pandas as pd

read_file = pd.read_csv (r'Path where the CSV file is stored\File name.csv')
read_file.to_excel (r'Path to store the Excel file\File name.xlsx', index = None, header=True)

In the next section, I’ll show you how to apply this template in practice.

Steps to Convert a CSV to Excel using Python

Step 1: Install the pandas package

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

pip install pandas

Step 2: Capture the path where the CSV file is stored

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

For example, I stored a CSV file about products under the following path:

C:\Users\Ron\Desktop\Test\Product_List.csv

Where ‘Product_List’ is the current CSV file name, and csv is the file extension.

Step 3: Specify the path where the new excel file will be stored

Now, you’ll need to specify the path where the new Excel file will be stored. In my case, I specified the following path:

C:\Users\Ron\Desktop\Test\New_Products.xlsx

Where ‘New_Products’ is the new file name, and xlsx is the Excel file extension.

Step 4: Convert the CSV to Excel using Python

For this final step, you’ll need to use the following template to perform the conversion:

import pandas as pd

read_file = pd.read_csv (r'Path where the CSV file is stored\File name.csv')
read_file.to_excel (r'Path to store the Excel file\File name.xlsx', index = None, header=True)

This is the code that I applied for 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_csv (r'C:\Users\Ron\Desktop\Test\Product_List.csv')
read_file.to_excel (r'C:\Users\Ron\Desktop\Test\New_Products.xlsx', index = None, header=True)

Run the code in Python and the new Excel file (i.e., New_Products) will get created at your specified location:

How to Convert CSV to Excel using Python

Tool to Convert CSV files to Excel files

In the final section of this guide, I’ll share with you a universal code that you may run in Python. This code will create a simple GUI to convert CSV files to Excel files:

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 getCSV ():
    global read_file
    
    import_file_path = filedialog.askopenfilename()
    read_file = pd.read_csv (import_file_path)
    
browseButton_CSV = tk.Button(text="      Import CSV File     ", command=getCSV, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 130, window=browseButton_CSV)

def convertToExcel ():
    global read_file
    
    export_file_path = filedialog.asksaveasfilename(defaultextension='.xlsx')
    read_file.to_excel (export_file_path, index = None, header=True)

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

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

Once you run the code in Python, you’ll see the following tkinter GUI:

File conversion tool from CSV to Excel

Click on the ‘Import CSV File’ button:

Convert CSV to Excel using Python

A dialogue box will appear on your screen. Locate and then select your CSV file (and then click on Open):

Dialogue box tkinter

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

CSV to Excel using Python

Finally, type a name for your new Excel file (e.g., New_Products) and then click on Save:

dialogue box

You’ll now see the new Excel file at your saved location:

How to Convert CSV to Excel using Python