Open In App

Create a GUI to convert CSV file into excel file using Python

Last Updated : 20 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites: Python GUI – tkinter, Read csv using pandas

CSV file is a Comma Separated Value file that uses a comma to separate values. It is basically used for exchanging data between different applications. In this, individual rows are separated by a newline. Fields of data in each row are delimited with a comma.

Modules Needed

  • Pandas: Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. To install this module type the below command in the terminal.
    pip install pandas
  • pandastable: This library provides a table widget for Tkinter with plotting and data manipulation functionality. To install this module type the below command in the terminal
    pip install pandastable
  • tkintertable: This library is used for adding tables to a Tkinter application. To install this library type the below command in the terminal.
    pip install tkintertable

Below is the implementation.

Input CSV File:

gui-to-convert-csv-to-excel




import pandas as pd
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox as msg
from pandastable import Table
from tkintertable import TableCanvas
   
  
class csv_to_excel:
   
    def __init__(self, root):
   
        self.root = root
        self.file_name = ''
        self.f = Frame(self.root,
                       height = 200,
                       width = 300)
          
        # Place the frame on root window
        self.f.pack()
           
        # Creating label widgets
        self.message_label = Label(self.f,
                                   text = 'GeeksForGeeks',
                                   font = ('Arial', 19,'underline'),
                                   fg = 'Green')
        self.message_label2 = Label(self.f,
                                    text = 'Converter of CSV to Excel file',
                                    font = ('Arial', 14,'underline'),
                                    fg = 'Red')
   
        # Buttons
        self.convert_button = Button(self.f,
                                     text = 'Convert',
                                     font = ('Arial', 14),
                                     bg = 'Orange',
                                     fg = 'Black',
                                     command = self.convert_csv_to_xls)
        self.display_button = Button(self.f,
                                     text = 'Display',
                                     font = ('Arial', 14), 
                                     bg = 'Green',
                                     fg = 'Black',
                                     command = self.display_xls_file)
        self.exit_button = Button(self.f,
                                  text = 'Exit',
                                  font = ('Arial', 14),
                                  bg = 'Red',
                                  fg = 'Black'
                                  command = root.destroy)
   
        # Placing the widgets using grid manager
        self.message_label.grid(row = 1, column = 1)
        self.message_label2.grid(row = 2, column = 1)
        self.convert_button.grid(row = 3, column = 0,
                                 padx = 0, pady = 15)
        self.display_button.grid(row = 3, column = 1
                                 padx = 10, pady = 15)
        self.exit_button.grid(row = 3, column = 2,
                              padx = 10, pady = 15)
   
    def convert_csv_to_xls(self):
        try:
            self.file_name = filedialog.askopenfilename(initialdir = '/Desktop',
                                                        title = 'Select a CSV file',
                                                        filetypes = (('csv file','*.csv'),
                                                                     ('csv file','*.csv')))
               
            df = pd.read_csv(self.file_name)
              
            # Next - Pandas DF to Excel file on disk
            if(len(df) == 0):      
                msg.showinfo('No Rows Selected', 'CSV has no rows')
            else:
                  
                # saves in the current directory
                with pd.ExcelWriter('GeeksForGeeks.xls') as writer:
                        df.to_excel(writer,'GFGSheet')
                        writer.save()
                        msg.showinfo('Excel file created', 'Excel File created')     
               
        except FileNotFoundError as e:
                msg.showerror('Error in opening file', e)
   
    def display_xls_file(self):
        try:
            self.file_name = filedialog.askopenfilename(initialdir = '/Desktop',
                                                        title = 'Select a excel file',
                                                        filetypes = (('excel file','*.xls'),
                                                                     ('excel file','*.xls')))
            df = pd.read_excel(self.file_name)
              
            if (len(df)== 0):
                msg.showinfo('No records', 'No records')
            else:
                pass
                
            # Now display the DF in 'Table' object
            # under'pandastable' module
            self.f2 = Frame(self.root, height=200, width=300
            self.f2.pack(fill=BOTH,expand=1)
            self.table = Table(self.f2, dataframe=df,read_only=True)
            self.table.show()
          
        except FileNotFoundError as e:
            print(e)
            msg.showerror('Error in opening file',e)
  
# Driver Code 
root = Tk()
root.title('GFG---Convert CSV to Excel File')
   
obj = csv_to_excel(root)
root.geometry('800x600')
root.mainloop()


Output:



Similar Reads

How to create multiple CSV files from existing CSV file using Pandas ?
In this article, we will learn how to create multiple CSV files from existing CSV file using Pandas. When we enter our code into production, we will need to deal with editing our data files. Due to the large size of the data file, we will encounter more problems, so we divided this file into some small files based on some criteria like splitting in
3 min read
Convert CSV to Excel using Pandas in Python
Pandas can read, filter, and re-arrange small and large datasets and output them in a range of formats including Excel. In this article, we will be dealing with the conversion of .csv file into excel (.xlsx). Pandas provide the ExcelWriter class for writing data frame objects to excel sheets. Syntax: final = pd.ExcelWriter('GFG.xlsx') Example:Sampl
1 min read
Convert HTML table into CSV file in python
CSV file is a Comma Separated Value file that uses a comma to separate values. CSV file is a useful thing in today's world when we are talking about machine learning, data handling, and data visualization. In this article, we will discuss how to convert an HTML table into a CSV file. Converting HTML Table into CSV file in Python Example: Suppose HT
2 min read
How to convert CSV File to PDF File using Python?
In this article, we will learn how to do Conversion of CSV to PDF file format. This simple task can be easily done using two Steps : Firstly, We convert our CSV file to HTML using the PandasIn the Second Step, we use PDFkit Python API to convert our HTML file to the PDF file format. Approach: 1. Converting CSV file to HTML using Pandas Framework. P
3 min read
Convert Excel to CSV in Python
In this article, we will be dealing with the conversion of Excel (.xlsx) file into .csv. There are two formats mostly used in Excel : (*.xlsx) : Excel Microsoft Office Open XML Format Spreadsheet file.(*.xls) : Excel Spreadsheet (Excel 97-2003 workbook). Let's Consider a dataset of a shopping store having data about Customer Serial Number, Customer
3 min read
Convert a NumPy array into a CSV file
After completing your data science or data analysis project, you might want to save the data or share it with others. Exporting a NumPy array to a CSV file is the most common way of sharing data. CSV file format is the easiest and most useful format for storing data and is convenient to share with others. So in this tutorial, we will see different
4 min read
Python program to read CSV without CSV module
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the nam
3 min read
How to Import a CSV file into a SQLite database Table using Python?
In this article, we are going to discuss how to import a CSV file content into an SQLite database table using Python. Approach:At first, we import csv module (to work with csv file) and sqlite3 module (to populate the database table).Then we connect to our geeks database using the sqlite3.connect() method.At this point, we create a cursor object to
3 min read
How to Convert a Dash app into an Executable GUI
Dash, a powerful Python framework for building web applications, provides an excellent platform for creating interactive data visualizations and dashboards. In this article, we will see how we can create a Dash app into a GUI using Python. Convert a Dash app into an Executable GUI in PythonBelow are step-by-step explanations to convert a dash app i
2 min read
How to convert PDF file to Excel file using Python?
In this article, we will see how to convert a PDF to Excel or CSV File Using Python. It can be done with various methods, here are we are going to use some methods. Method 1: Using pdftables_api Here will use the pdftables_api Module for converting the PDF file into any other format. It's a simple web-based API, so can be called from any programmin
2 min read
three90RightbarBannerImg