Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python 3 Pandas Script to Convert CSV File to Excel & Save it as (.XLSX) File

Posted on January 7, 2023

 

 

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

 

 

Python
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

 

 

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

 

 

 

 

 

Recent Posts

  • React.js Twitter API Tutorial to Embed Profile & Timeline, Hashtags of User in Browser Using Javascript
  • Android Java Tutorial to Change Styles & Visibility of System Bars (Top, Action & Status) Full Example
  • Android Java Project to Display & Play Audio Files From Storage inside ListView Using MediaPlayer Class
  • Android Java Project to Build MP4 Video to MP3 Audio Converter Using MediaMuxer Class
  • Android Java Project to Merge Multiple PDF Documents From Gallery Using iTextPDF Library
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme