Welcome folks today in this blog post we will be using the pandas
library to convert the csv
file to excel
file and save it as .xlsx
file. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below pandas
library using the pip
command as shown below
pip install pandas
And after that you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 |
import pandas as pd # Read the CSV file df = pd.read_csv('output.csv') # Convert the CSV file to Excel df.to_excel('file.xlsx',index=False) |
As you can see we are importing the pandas
library at the top and then we are using the read_csv()
method to load the content of the csv
file and then we are using the to_excel()
method to convert the csv
to excel file and by default the sheet is named to Sheet1 in the excel file as shown below
Now if you execute the python script
using the below command as shown below
python app.py
Changing Sheet Name
We can even pass the custom sheet
name into the output excel file as shown below
1 2 3 4 5 6 7 |
import pandas as pd # Read the CSV file df = pd.read_csv('output.csv') # Convert the CSV file to Excel df.to_excel('file.xlsx', sheet_name='Sample Sheet', index=False) |