Convert CSV to Excel using Python

To convert a CSV file to an Excel file 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=False, header=True)

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:

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.

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

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

Next, specify the path where the new Excel file will be stored. For example:

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, 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=False, header=True)

Here is the complete syntax for our example (note that 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=False, header=True)

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

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

Leave a Comment