×

Writing to Excel using Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses an interpreter instead of the compiler to run the code.

The interpreted language is processed at the run time thus takes less time to run. Python is also interactive, so we can directly run programs in the terminal itself.

Sometimes, we need to handle excel sheets while working in Python because excel sheets can contain loads of data that we want to load and work with. We can perform multiple operations on excel sheets using the xlwt module.

We can use the write method to write an excel sheet in Python. In addition to the write method, there are many other methods and logic to handle excel files.

XlsxWriter module is used to write the excel files in the .xslx extension. We can create multiple worksheets using this module and write texts, numbers, and formulas for them. This module also supports adding images, charts, etc.

We can install the xlsx writer using the command below:

Pip install xlsxwriter

Let us see an example of how we can write data in specific cells. But before writing the code, we should keep in mind that rows and columns in Xlsxwriter are zero-indexed, i.e., the first cell is A1(0,0), B1(0,1)…. and so on.

importxlsxwriter


workbook = xlsxwriter.Workbook('Example.xlsx')
worksheet = workbook.add_worksheet()
row = 0
column = 0


content = ["Roahn", "Nikhil", "Roshan", "Mehak", "Mudit", "Muskaan", "Chandan"]
for item in content :
worksheet.write(row, column, item)


column+=1


workbook.close()

Output:

Running this code will create an excel sheet named Example.csv in the same directory with the content:

RoahnNikhilRoshanMehakMuditMuskaanChandan
Writing to Excel using Python

Writing Data one by one in Excel.

To write data cell by cell in a worksheet, use the code below:

importxlsxwriter


workbook = xlsxwriter.Workbook('Example2.xlsx')
worksheet = workbook.add_worksheet()


worksheet.write(0, 0, 123)     #Int
worksheet.write(1, 0, 123.45)  #Float
worksheet.write(2, 0, 'Rohan')  #String
worksheet.write(3, 0, None)     # None
worksheet.write(4, 0, True)     # bool


workbook.close()

Output

After running this code, you will be able to see this content in an excel file named Example2.xslx:

123
123.45
Rohan


TRUE
Writing to Excel using Python

As you can see, we have written different data types in different cells in this example. Write() method automatically determines the data type of the data inserted and uses the functions for that datatype to enter the value in a cell.

Writing using xlwt module.

We can also use the xlwt module to write data In excel sheets. To install the xlwt module write this command in your console:

Pip install xlwt

After installing the module, let us see how to create and write an excel file using this module.

importxlwt #importing module
fromxlwt import Workbook


# creating workbook
wb = Workbook()


sheet1 = wb.add_sheet('Sheet 1')


sheet1.write(1, 0, 'ISBT DEHRADUN')
sheet1.write(2, 0, 'SHASTRADHARA')
sheet1.write(3, 0, 'CLEMEN TOWN')
sheet1.write(4, 0, 'RAJPUR ROAD')
sheet1.write(5, 0, 'CLOCK TOWER')
sheet1.write(0, 1, 'ISBT DEHRADUN')
sheet1.write(0, 2, 'SHASTRADHARA')
sheet1.write(0, 3, 'CLEMEN TOWN')
sheet1.write(0, 4, 'RAJPUR ROAD')
sheet1.write(0, 5, 'CLOCK TOWER')


wb.save('Example3.xls')

Output

In this code, we are using the write function to write data in an excel sheet. After running the code, an excel sheet will be created with the following content:

Muskaan Chandan Risk Chirag Ayush
Rohan
Nikhil
Roshan
Mudit
Rahul 
Writing to Excel using Python

Formatting Excel

We can add some styles and format the cells of an excel sheet by adding multiple styles to it by using the xslx writer module. To do that, we just need to define our style and pass it to our write function as an argument.

importxlwt #importing module
fromxlwt import Workbook


wb = Workbook()


worksheet = wb.add_sheet('Sheet 1')




style =xlwt.easyxf('font: bold 1, color red;')
row = 0
column = 0


content = ["Roahn", "Nikhil", "Roshan", "Mehak", "Mudit", "Muskaan", "Chandan"]
for item in content :
worksheet.write(row, column, item,style)


column+=1


wb.save('Example4.xls')

Output

After running the code, you will get the following output

RohanNikhilRoshanMehakMuditMuskaanChandan
Writing to Excel using Python

Related Topics

Operator Overloading in Python

In any programming language, + is an arithmetic operator; it adds two numbers to give the sum. Have you ever tried to concatenate two strings? Concatenating two strings is adding...

5 minutes read.

Spell Corrector GUI using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Python List sort() method

The list.sort () method in Python sorts the items of the list in place. Syntax list.sort(key=None, reverse=False) Parameter reverse: If a Boolean value ‘True’ is passed, the  sorting will be done in the descending order else for ‘False’...

2 minutes read.

Alexa Python

The cloud-based voice assistant renders text into speech in a female voice. Alexa is a virtual assistant released by Amazon. Using itself as a system for home automation, Alexa can...

4 minutes read.

Python while loop

Loops are essential in Python or any other programming language, as they help to execute a block of code repetitively. Sometimes, situations arise where you would need to use a piece of code...

2 minutes read.

Check if the directory exists in Python

To prevent any error, while loading or editing a file, we first have to check that the directory or the file exists or not. This is also done to prevent...

5 minutes read.

Python Operators

Operators in Python programming An operator is a symbol that operates on one or more operands. An operand is a value or a variable on which we perform the operation....

5 minutes read.

Python BytesIO

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Inheritance in Python

Inheritance in Python Object-Oriented Programming provides reusable patterns to the code for restricting the redundancy in development projects. One of the basic principles of Object-Oriented Programming that helps achieve recyclable code...

8 minutes read.

Character Set in Python

A collection of legal characters that will recognize by a programming language known as a Character set. Taking python programming language as an instance. The set of characters supported by the...

6 minutes read.

Create the First GUI Application using PyQt5 in Python

GUI: A graphical user interface, or GUI, is present on most personal computers. It provides a simple experience for individuals with various computing skill levels. GUI apps may take more resources...

3 minutes read.

Python Escape Characters

In this tutorial, we will learn how to use the Escape Characters in Python. Escape Character: Escape Characters are used for some special meaning in our statements. It is denoted or represented...

3 minutes read.

Python String Negative Indexing

This page contains all the information how to use “Negative indexing“ in Python with the help of using slicing. What is an Index? An index is a numeric value, which is assigned...

3 minutes read.

How to clear screen in Python?

How to clear screen in python There are times when we execute our program and it results in an unexpected output. The obtained result can contain some kind of garbage values...

4 minutes read.

Writing to Excel using Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

3 minutes read.

Standard GUI Unit Converter using PyQt5 in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Is Python Case Sensitive

Case sensitivity is the mode of dealing with the written alphabet. The cases of the alphabet are examined and based on these words are being treated. The uppercase and lowercase...

3 minutes read.

Wikipedia module in Python

Wikipedia module in Python: The internet has become an essential part of all of our life which is the largest source of all the information. Therefore, it becomes very important...

7 minutes read.

Assertion error in python

In this article, we will discuss assertions in the python programming language. Assertion: Assertions are a way of telling a program to test a certain condition and trigger an error if the...

3 minutes read.

Arguments and parameters in Python

Generally, there is a misunderstanding that parameters and arguments, both are the same. But, as a programmer, you need to understand the difference between these two. Parameters: While defining a function, we...

5 minutes read.