Converting .xls to .csv File

Platforuma India
5 min readMay 15, 2021

In this blog, we are going to convert the .xls file into the .csv file

Let us comprehend what is .xls and .csv?

.xls:

An Xls document file is the standard file extension for any document constructed in the Microsoft Excel software program. The xls is one of very few document file types that handle spreadsheet documents. It is opened, edited and printed using Microsoft Excel. Xls stands for Excel Spreadsheet.

.csv:

The CSV stands for Comma Separated Values. CSV file format stores data in tabular form like numbers and text. CSV files can not perform operations on the .csv data.

For more information on .xls or .csv, kindly refer this (documentationhttps://www.zamzar.com/convert/xls-to-csv/)

Let’s Get started with the project using Python GUI for .xls to .csv:

Code for the project:

import pandas as pd

from tkinter import *

import csv

from tkinter.filedialog import askopenfile

from tkinter import filedialog as fd

root = Tk()

root.title(“.xls to .csv converter”)

root.geometry(“600x500”)

def convert_file():

file = fd.askopenfile()

if file:

print(file.name)

read_file = pd.read_excel (file.name)

if entry1.get():

read_file.to_csv (entry1.get(),

index = None,

header=True)

# read csv file and convert

# into a dataframe object

df = pd.DataFrame(pd.read_csv(entry1.get()))

# show the dataframe

df

label_1 = Label(root,text=”xls to .csv converter”,font=(“arial”,25),justify=CENTER,fg=”red”)

label_1.pack()

entry1 = Entry(root)

entry1.place(x=200, y=140)

label_2 = Label(root,text=”Give name for new .csv and select .xls file”,font=(“arial”,20))

label_2.place(x=30,y=100)

button_2 = Button(root,text=”Convert to .csv”,font=(“arial”,15),justify=CENTER,fg=”green”,command=convert_file)

button_2.place(x=150,y=200)

root.mainloop()

Define code working:-

import pandas as pd

from tkinter import *

import csv

from tkinter.filedialog import askopenfile

from tkinter import filedialog as fd

Importing the pandas module as pd, means that we are importing the panda module in our project for accessing python libraries that are provided data analysis. Data scientists work with data stored in table formats like .csv, .tsv, or .xls, so for all the formats, we are importing the pandas module in our project.

Wants to know more about Data science refer to our blog on Data science — Career of future

Tkinter is the name of the GUI library in Python. Import * means everything from the library. Import * is allowing a Python file to access the script from another Python file. Importing the CSV module. CSV Module is the most common import and export format for spreadsheets and databases.

The file dialogue is the Tkinter Module in python. Imports the askopenfile() function in Tkinter. In our project, we are using a .xls file as input from the user so this module allows us to open .xls file.

root = Tk()

root.title(“.xls to .csv converter”)

root.geometry(“600x500”)

Tk class is used to create a root window. The root is the name of a root window. Here Tk function provides a window of GUI as well as provides so many features like setting the title, set the geometry of the GUI window. In these steps, we are giving the title of the GUI root window. Tkinter provides many methods, one of them is the geometry() method.

def convert_file():

file = fd.askopenfile()

if file:

print(file.name)

read_file = pd.read_excel (file.name)

if entry1.get():

read_file.to_csv (entry1.get(),

index = None,

header=True)

df = pd.DataFrame(pd.read_csv(entry1.get()))

Define the function convert_file. Define a file variable to hold the open file with the help of an open file function. In this line we are defining the if condition when getting the file, and then apply the condition. In programming if is the conditional statement where we are providing some condition in our project like print the file name. Define a variable read_file to hold the value of pandas DataFrame of excel file from the file name. Here filename is, selecting .xls to file from the user and reading .xls file from the pandas as a pd.

Again in this line we are defining the if condition where entry1 gets the value. About entry1 we learn in the next steps of the project. In this line, we get the file name from the user side and in this file, we read the excel file in CSV file mode with no index.

Define a df to hold the DataFrame in CSV mode from the given file name from the user side.

label_1 = Label(root,text=”xls to .csv converter”,font=(“arial”,25),justify=CENTER,fg=”red”)

label_1.pack()

entry1 = Entry(root)

entry1.place(x=200, y=140)

Now come to Designing part of the GUI project. Here we are using some Label, Entry Box and Button in a project. So let us start to understand the working of Label, EntryBox and Button in a project accordingly to the project code line by line.

Define a Label label_1 on GUI window and write text on the label and set font, fg or justify the text of the Label. Now in this line, we are working on the pack method. Pack method provides to pack the Label area. Define Entry entry1 on GUI window. EntryBox is used for taking the input from the user and also we are used to displaying the output of some using set function. Assign the place of the entry1 Entry using x or y coordinates values.

label_2 = Label(root,text=”Give name for new .csv and select .xls file”,font=(“arial”,20))

label_2.place(x=30,y=100)

button_2 = Button(root,text=”Convert to .csv”,font=(“arial”,15),justify=CENTER,fg=”green”,command=convert_file)

button_2.place(x=150,y=200)

root.mainloop()

Define a Label label_2 on the GUI window and write text on the label and set the font of the label. Assign the place of the label_2 Label using x or y coordinates values.

Now come to the Button part of a project in this Button we are performing an action when we clicked on the Button. So let’s start with how Button works.

Here we define a button_2 Button on the GUI window and set text and font fg and justify button. Give convert_file command on button_2 Button for performing an action when Button click. Assign the place of the button_2 Button using x or y coordinates values.

Now come to the last line of code of the project is the mainloop function that provides an infinite loop. So let’s start to understand how it works. mainloop() is an infinite loop used to run the application, using this function window is not closed as long as.

Here we complete a full GUI Project using Python Tkinter, pandas, CSV and filedialog modules.

Visit Our Website:

Get More Blogs On :

Author: Riya Patidar

Editor: Ashutosh Raghuwanshi, Devendra Patidar

--

--