Python Tutorial

Introduction Python Features Python Applications System requirements for Python Python Installation Python Basics Python Variables Python Data Types Python IDE Python Keywords Python Operators Python Comments Python Pass Statement

Python Conditional Statements

Python if Statement Python elif Statement Python If-else statement Python Switch Case

Python Loops

Python for loop Python while loop Python Break Statement Python Continue Statement Python Goto Statement

Python Arrays

Python Array Python Matrix

Python Strings

Python Strings Python Regex

Python Built-in Data Structure

Python Lists Python Tuples Python Lists vs Tuples Python Dictionary Python Sets

Python Functions

Python Function Python min() function Python max() function Python User-define Functions Python Built-in Functions Anonymous/Lambda Function in Python

Python File Handling

Python File Handling Python Read CSV Python Write CSV Python Read Excel Python Write Excel Python Read Text File Python Write Text File Read JSON File in Python

Python Exception Handling

Python Exception Handling Python Errors and exceptions Python Assert

Python OOPs Concept

OOPs Concepts in Python Classes & Objects in Python Inheritance in Python Polymorphism in Python Python Encapsulation Python Constructor Static Variables in Python Abstraction in Python

Python Iterators

Iterators in Python Yield Statement In Python

Python Generators

Python Generator

Python Decorators

Python Decorator

Python Functions and Methods

Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods

Python Modules

Python Modules Python Datetime Module Python Calendar Module  

Python MySQL

Python MySQL Python MySQL Update Operation Python MySQL Delete Operation

Python MongoDB

Python MongoDB

Python Data Structure Implementation

Python Stack Python Queue Python Hash Table Python Graph

Python Advance Topics

Speech Recognition in Python Face Recognition in Python Python Rest API Python Command Line Arguments Python JSON Python Virtual Environment Type Casting in Python Collections in python Python Enumerate Python Debugger Python DefaultDict


Python PPTX Python Pickle Python Seaborn Python Coroutine Python EOL Python Infinity Python math.cos and math.acos function Python Project Ideas Based On Django Reverse a String in Python Reverse a Number in Python Python Word Tokenizer Python Trigonometric Functions Python try catch exception GUI Calculator in Python Implementing geometric shapes into the game in python Installing Packages in Python Python Try Except Python Sending Email Socket Programming in Python Python CGI Programming Python Data Structures Python abstract class Python Compiler Python K-Means Clustering List Comprehension in Python3 NSE Tools In Python Operator Module In Python Palindrome In Python Permutations in Python Pillow Python introduction and setup Python Functionalities of Pillow Module Python Argmin Python whois Python JSON Schema Python lock Return Statement In Python Reverse a sentence In Python tell() function in Python Why learn Python? Write Dictionary to CSV in Python Write a String in Python Binary Search Visualization using Pygame in Python Latest Project Ideas using Python 2022 Closest Pair of Points in Python ComboBox in Python Python vs R Python Ternary Operators Self in Python Python vs Java Python Modulo Python Packages Python Syntax Python Uses Python Logical Operators Python Multiprocessing Python History Difference between Input() and raw_input() functions in Python Conditional Statements in python Confusion Matrix Visualization Python Python Algorithms Python Modules List Difference between Python 2 and Python 3 Is Python Case Sensitive Method Overloading in Python Python Arithmetic Operators Design patterns in python Assignment Operators in Python Is Python Object Oriented Programming language Division in Python Python exit commands Continue And Pass Statements In Python Colors In Python Convert String Into Int In Python Convert String To Binary In Python Convert Uppercase To Lowercase In Python Convert XML To JSON In Python Converting Set To List In Python Covariance In Python CSV Module In Python Decision Tree In Python Difference Between Yield And Return In Python Dynamic Typing In Python Abstract design pattern in python Builder design pattern in python Prototype design pattern in Python Creational design patterns in Python

How to

How to convert integer to float in Python How to reverse a string in Python How to take input in Python How to install Python in Windows How to install Python in Ubuntu How to install PIP in Python How to call a function in Python How to download Python How to comment multiple lines in Python How to create a file in Python How to create a list in Python How to declare array in Python How to clear screen in Python How to convert string to list in Python How to take multiple inputs in Python How to write a program in Python How to compare two strings in Python How to create a dictionary in Python How to create an array in Python How to update Python How to compare two lists in Python How to concatenate two strings in Python How to print pattern in Python How to check data type in python How to slice a list in python How to implement classifiers in Python How To Print Colored Text in Python How to develop a game in python How to print in same line in python How to create a class in python How to find square root in python How to import numy in python How to import pandas in python How to uninstall python How to upgrade PIP in python How to append a string in python How to open a file in python


Python Sort List Sort Dictionary in Python Python sort() function Python Bubble Sort


Factorial Program in Python Prime Number Program in Python Fibonacci Series Program in Python Leap Year Program in Python Palindrome Program in Python Check Palindrome In Python Calculator Program in Python Armstrong Number Program in Python Python Program to add two numbers Anagram Program in Python Even Odd Program in Python GCD Program in Python Python Exit Program Python Program to check Leap Year Operator Overloading in Python Pointers in Python Python Not Equal Operator Raise Exception in Python Salary of Python Developers in India What is a Script in Python Singleton design pattern in python

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 =
 # 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."C:\\Users\\DEVANSH SHARMA\\Desktop\\demo.xlsx") 


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')'xlwt Address1.xls') 


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