Reading multiple cells

In this section, we are going to read multiple cells. We will use the openpyxl module. Create a script called read_multiple.py and write the following content in it:

import openpyxl

book_obj = openpyxl.load_workbook('sample.xlsx')
excel_sheet = book_obj.active
cells = excel_sheet['A1': 'C6']
for c1, c2, c3 in cells:
print("{0:6} {1:6} {2:6}".format(c1.value, c2.value, c3.value))

 Run the script and you will get the following output:

student@ubuntu:~/work$ python3 read_multiple.py

Following is the output:

Id     First Name Last Name
101 John Smith
102 Mary Williams
103 Rakesh Sharma
104 Amit Roy
105 Sandra Ace

In the preceding example, we are reading the data of three columns by using the range operation. Then, we read the data from the cells A1 – C6.

Similarly, we can perform lots of operations, such as merging and, splitting cells, on the Excel file using the openpyxl module.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset