×

Python Write Excel File

Python Write Excel File

The Python xlwt module is used to write an excel file and perform multiple operations on it. It can be used to write text, numbers, and formulas for multiple worksheets. It also supports a feature like formatting, images. Chart, page setup, auto filters, and many others.

Python does not come with the xlwt module; we need to install it using the pip from the command line.

pip install xlswriter

Writing excel file openpyxl module

It is defined as a package which is commonly suggested if you want to read and write .xlsx, xlsm, xltx, and xltm files. You can check it by running type(wb).

The Python provides the load_workbook () function which takes an argument and returns a workbook object, which denotes the file. It should be remembered that you are in the same directory where your spreadsheet is located. Otherwise, the Python interpreter will show an error.

We can easily use for loop with the help of the range() function to help print out the values of the rows that have values in column 2. If those specific cells are empty, we will get none.

import openpyxl
# Workbook() function of openpyxl is used to  
# create a new blank Workbook object
wb = openpyxl.Workbook()
# Get workbook active sheet
# from the active attribute
sheet = wb.active
 # Cell object is created by
# using sheet object's cell() method.
c1 = sheet.cell(row=1, column=1)
# writing values to cells
c1.value = "HIMANSHU"
c2 = sheet.cell(row=1, column=2)
c2.value = "DUBEY"
# Once have a Worksheet object, one can
# access a cell object by its name also.
# A2 is denoted column = 1 & row = 2.
c3 = sheet['A2']
c3.value = "RAHUL"
# B2 is denoted column = 2 & row = 2.
c4 = sheet['B2']
c4.value = "TRIVEDI"
c5 = sheet['A2']
c5.value = "RAHUL"
# B2 is denoted column = 2 & row = 2.
c6 = sheet['B2']
c6.value = "TRIVEDI"
# Anytime we modify the Workbook object
# the file will not be saved until you call
# the save() workbook method.
wb.save("C:\\Users\\DEVANSH SHARMA\\Desktop\\demo.xlsx") 

Output:

We will get the following file

 

Writing excel file openpyxl module

Writing data to Excel file with xlwt

We can use the xlwt package, instead of the XlsxWriter package to create the spreadsheets that contain the data. It is an another package for writing data, formatting information, etc. and suitable for writing the data and format information to files with .xls extension. It can perform multiple operations on the spreadsheet.

It allows performing operations such as formatting, images, charts, page setup, auto filters, conditional formatting, and many others.

The Python library pandas provide excellent methods for reading all kinds of data from excel files. We can also import the results back to pandas.

Consider the following program

import xlwt
from xlwt import Workbook
# Workbook is created
wb = Workbook() 
# add_sheet is used to create sheet.
sheet1 = wb.add_sheet('Sheet 1')
 sheet1.write(1, 0, 'ANAND VIHAR')
 sheet1.write(2, 0, 'LAXMI NAGAR')
 sheet1.write(3, 0, 'SARITA VIHAR')
 sheet1.write(4, 0, 'LODHI GARDEN')
 sheet1.write(5, 0, 'SONIYA VIHAR')
 sheet1.write(6, 0, 'PITAMPURA')
 sheet1.write(7, 0, 'YAMUNA BANK')
 sheet1.write(8, 0, 'RAJIV CHAWK')
 wb.save('xlwt Address1.xls') 

Output:

We will get the excel file named Address1.xls

Writing data to Excel file with xlwt

Writing data to Excel file with pyexcel

We can easily export arrays back to a spreadsheet by using the save_as() function and by passing the name of the destination file to the dest_file_name argument.

It allows us to specify the delimiter and add dest_delimiter argument. You can pass the symbol that you want to use as a delimiter in-between " ".

import pyexcel
# make sure you had pyexcel installed
a_list_of_dictionaries = [
     {
         "Name": 'Adam',
         "Age": 28
     },
     {
         "Name": 'Beatrice',
         "Age": 29
     },
     {
         "Name": 'Ceri',
         "Age": 30
     },
     {
         "Name": 'Dean',
         "Age": 26
    } ]
pyexcel.save_as(records=a_list_of_dictionaries, dest_file_name="Book1.xls")

Related Topics

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Python program to print calendar

Python program to print the calendar This article will explain how to print the calendar of month and year using Python's calendar module. It's a straightforward thing in Python by importing...

1 minute read.

Assignment Operators in Python

The prime usage of Assignment Operators is to assign values to variables. These are taken into account to do operations on values and variables. There are some special symbols in python...

4 minutes read.

Application to get live USD/INR rate Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

SKLearn Linear Module

The SK learn linear module is one such module that helps to study the relationship between the independent and dependent variables.The linear module can be implemented by using the best...

3 minutes read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

Python list() | List class in Python

The list() class in Python creates a list object where the list object is a collection which is ordered and changeable. Syntax class list([iterable]) Parameter Iterable: It is a required parameter which represents a sequence, collection...

1 minute read.

How To Print Colored Text in Python

Changing the colour of certain parts of a string when printing the output of a Python programme to the terminal may make it easier to read. We can approach this...

3 minutes read.

Python EOL (End Of Line)

Introduction An EOL (End Of Line) is defined as a syntax error that indicates that the Python interpreter reached at the end of the line when it tried to scan a...

3 minutes read.

How to Import Files in Python

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Data Structures and Algorithms Using Python | Part 1

Data Structures: Data Structure is defined as a way to organize and store the data so that we can access the data and work more efficiently. Data structures also describe the...

18 minutes read.

How to Concat two Dataframes in Python

Using Pandas dataframe, we can concat two dataframes or series in Python. So let's take a brief introduction to what is Pandas in Python. Pandas is a library typically used for...

7 minutes read.

Array to String in Python

What is an Array: The idea behind an array is to group several items of the same type, making it easier to locate each element by adding an extra offset to...

4 minutes read.

Pillow Python introduction and setup

Introduction Advanced image processing implies handling the picture carefully with the assistance of a PC. Utilizing picture handling we can perform activities like upgrading the picture, obscuring the picture, separating text...

4 minutes read.

Python Sys Stdout

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.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

Python Statistical Module

Python Statistical Module The Python statistical module provides various functions that we can use in our Python program, to perform mathematical statistics operations on the numerical data given to us. The...

8 minutes read.

Data Structures and Algorithms using Python | Part 2

Files: A file is a location or information stored in computer storage devices. File handling is essential when the information or the data is to be held permanently. When we try to...

20 minutes read.

Python object()

Python object() class The object class in Python is a base for all classes. It returns a new featureless object. Syntax class object Parameter NA Return It returns a new featureless object. Example 1 # Python program explaining # the object()...

1 minute read.

Difference between Input() and raw_input() functions in Python

There are two input functions in Python that are used for taking input are given below: raw_input()input() What is raw_input() Function? raw_input() function is a built-in function in Python. It is used to...

6 minutes read.