Saturday, 5 December 2015

Python - Read excel sheet cell value using 'openpyxl' library.

Prerequisite:
Python is already installed.
'openpyxl' library is already installed.

Refer below url for installing 'openpyxl' library.
http://automation-home.blogspot.com/2015/12/python-installing-openpyxl-library.html

Excel sheet with sample data.




Executing the python script.




Sample Program:
import os
from openpyxl import load_workbook

wb = load_workbook(os.path.abspath(os.path.join(os.path.dirname(__file__),'testdata/Sample.xlsx')))
sheet = wb.get_sheet_by_name('Sheet1')
c = sheet['B4'].value
print(c)


Note:
Excel sheet should be closed before executing the script.

Read excel sheet cell value based on row and column number.

Sample Program:
import os
from openpyxl import load_workbook

wb = load_workbook(os.path.abspath(os.path.join(os.path.dirname(__file__),'testdata/Sample.xlsx')))
sheet = wb.get_sheet_by_name('Sheet1')
c = sheet.cell(row=1, column=2).value
print(c) 

Note:
Excel sheet should be closed before executing the script.

2 comments: