In this quick guide, you’ll see the complete steps to convert a CSV file to an Excel file using Python.
To start, here is a simple 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, you’ll see 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.
Here is an example of a path where a CSV file is stored:
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. For example:
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)
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 = None, header=True)
Run the code in Python and the new Excel file (i.e., New_Products) will be saved at your specified location.