To read an Excel file in Python, you can use the openpyxl library. Here is an example code that reads an Excel file and prints the data from cell A1 and A2:
import openpyxl # load the Excel file workbook = openpyxl.load_workbook('example.xlsx') # select the sheet you want to read sheet = workbook.active # read data from cell A1 cell_a1 = sheet['A1'].value print(cell_a1) # read data from cell A2 cell_a2 = sheet['A2'].value print(cell_a2)
In this example, we first load the Excel file called “example.xlsx” using the load_workbook method. Then we select the active sheet using the active property.
We read the data from cell A1 using the sheet object and the cell reference ‘A1’. The value attribute returns the value of the cell.
We do the same thing for cell A2, and finally, we print the values of both cells. Note that you can modify the code to read data from any other cell by changing the cell reference in the sheet object.