Openpyxl Read Data From Cell



  • We can read the data that we have previously written in the cell.
  • There are two methods to read a cell, firstly we can access it by cell name, and secondly, we can access it by the cell() function.

For example, we are reading the data from the Wikitechy.xlsx file.

Sample Code

Import openpyxl
wb = openpyxl.load_workbook('Wikitechy.xlsx')  
sheet = wb.active
x1 = sheet['A1']  
x2 = sheet['A2']  
#using cell() function  
x3 = sheet.cell(row=3, column=1)  
print("The first cell value:",x1.value)  
print("The second cell value:",x2.value)  
print("The third cell value:",x3.value)  

Output

The first cell value: 618
The second cell value: Wikitechy
The third cell value: 30.15

Open Read Multiple Cells

We can read the values from the multiple cells. within the following example, we've marks.xlsx named excel file and that we will read each cell of file using the range operator.

 Openpyxl Multiple Data

Openpyxl Multiple Data

Sample Code

import openpyxl
wb = openpyxl.load_workbook('marks.xlsx')  
sheet = wb.active
cells = sheet['A1','B7']  
# cells behave like range operator  
for i1,i2 in cells:  
print("{0:8} {1:8}".format(i1.value,i2.value))  

Output

subjectmark
html89
css67
php 96
cloud 79
CN67
python85


Related Searches to Openpyxl Read Data From Cell