Python | Writing to Excel file using openpyxl module



Openpyxl — it is a Python library for reading and writing Excel files (with the extension xlsx / xlsm / xltx / xltm). The openpyxl module allows a Python program to read and modify Excel files.

For example, a user might have to go through thousands of lines and select some useful information to make small changes based on some criteria. Using the Openpyxl module, these tasks can be done very efficiently and easily.

Let`s see how to create and write an Excel worksheet using Python.

Code # 1: Program for printing the title of the title of the active sheet

# import openpyxl module

import openpyxl

  
# Call the Workbook () function from openpyxl
# create a new empty Workbook object

wb = openpyxl.Workbook ()

 
# Get the worksheet of the active sheet
# from the active attribute.

< code class = "plain"> sheet = wb.active

 
# After you have a Worksheet object
# you can get its name from
# title attribute.

sheet_title = sheet.title

 

print ( "active sheet title:" + sheet_title)

Output:

 active sheet title: Sheet 

Code # 2: Program for heading name changes

  # import openpyxl module

import openpyxl

 
# Calling the Workbook () function from openpyxl
# create a new blank Workbook object

wb = openpyxl.Workbook ()

 
# Get a working sheet of active sheet
# from active attribute

sheet = wb.active

 
# You can change the title of the title

sheet.title = "sheet1"

  

print ( " sheet name is renamed as: " + sheet.title)

Output:

 sheet name is renamed as: sheet1 

Code # 3: Excel sheet writer

# import openpyxl module

import openpyxl

 
# Call the Workbook () function from openpyxl
# create a new empty Workbook object

wb = openpyxl.Workbook ()

 < br /> # Get worksheet of active sheet
# from active attribute

sheet = wb.active

 
# Cell objects also have a row, column
# and attribute coordinates, which provide
# location information for the cell.

 
# Note: the first row or column is an integer
# is 1, not 0. The cell object is created
# using the cell () method of the sheet object.

c1 = sheet.cell (row = 1 , column = 1 )

 
# writing values ​​to cells

c1.value = "ANKIT"

 

c2 = sheet.cell (row = 1 , column = 2 )

c2.value = " RAI "

  
# If you have a Worksheet object, you can
# access the cell object also by his name.
# A2 means column = 1 & amp; line = 2.

c3 = sheet [ `A2` ]

c3. value = "RAHUL"

 
# B2 means column = 2 & amp; line = 2.

c4 = sheet [ `B2` ]

c4. value = "RAI"

 
# Every time you change the Workbook object
# or its sheets and cells, spreadsheet
# the file won`t be saved until you call
# save () method of the book.

wb.save ( "C: Users user Desktop demo.xlsx" )

Output:

code # 4: The program for adding sheets to the work book

# importing the openpyxl module

import openpyxl

 
# Calling the Workbook () function from openpyxl
# create a new blank Workbook object

wb = openpyxl.Workbook ()

 

sheet = wb.active

 
# Sheets can be added to the book using
# create_sheet () method of the book object.

wb.create_sheet (index 1 , title = " demo sheet2 " )

  

wb.save ( "C: Users user Desktop demo.xlsx" )

Output: