To convert an Excel file to a CSV file using Python:
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=False, 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=False, header=True)
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:
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.
Here is an example of a path where an Excel file is stored:
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
Next, specify the path where the new CSV file will be stored. For example:
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 convert the 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=False, header=True)
Here is the complete code 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_excel(r"C:\Users\Ron\Desktop\Test\Product_List.xlsx") read_file.to_csv(r"C:\Users\Ron\Desktop\Test\New_Products.csv", index=False, header=True)
Once you run the code (adjusted to you paths), you’ll get the new CSV file at your specified location.
You may also want to check the following source for the steps to convert CSV to Excel using Python.