Ask a Question

How to select Excel sheet in python?

Occasional Contributor

How to select Excel sheet in python?



I have python code to read Excel file:


def ReadDataFromExcel():
Excel = Sys.OleObject["Excel.Application"]
RowCount = Excel.ActiveSheet.UsedRange.Rows.Count
ColumnCount = Excel.ActiveSheet.UsedRange.Columns.Count
for i in range(1, RowCount + 1):
s = "";
for j in range(1, ColumnCount + 1):
s = s + VarToString(Excel.Cells.Item[i, j]) + '\r\n'
Log.Message("Row: " + VarToString(i), s);


However I didn't find API how to select a sheet for multiple sheet Excel file?   Please advice.





Valued Contributor

i'm not a python guy ...

just try 

sheet = Excel.Workbooks.Open("C:\\Work\\TestBook.xls").Sheets("Sheet1")
rowCount = sheet.UsedRange.Rows.Count
Occasional Contributor

Hi NisHera,


Thank you for your reply. Actually I have tried the same thing, but it didnot work. The error is "The specified object is not callable."


I also tried :

sheet =["Sheet1"],  the error is "The specified object is not indexable."  so it must be some special way in python to select sheet.





Here's the function I use to open an excel spreadsheet for a specific sheet and grab all the columns and their values. It uses the ACE Driver instead of opening Excel itself as an OLE Object. That way you can read from Spreadsheets without needing to have an Excel license on each Test Host. You can download the ACE Driver here


def read_excel_to_dict(file_path, sheet_name):
  Function will open an Excel file at the given sheet, then put the values into a dictionary with the
  key being the column name. The top row in the Excel file is considered the Column Name. The top row
  is only considered a Column if a registry key is set (it normally is by default). 

  Uses ACE driver (Excel runtime not required, but can't modify the spreadsheet)
  file_path : string
    File path to the excel spreadsheet
  sheet_name : string
    Name of the specific sheet to be opened
  return_dict : dict
    Dictionary object with each column (first row) mapped to its respective value (second row)
  DDT.ExcelDriver(file_path, sheet_name, True)
  driver = DDT.CurrentDriver
  return_dict = dict()
for i in range(DDT.CurrentDriver.ColumnCount): return_dict[driver.ColumnName[i]] = aqConvert.VarToStr(driver.Value[i]) return return_dict


Occasional Contributor

Hi ghuff2,


Thank you for your reply. I am sorry I didnot make it clear.   I need to modify the sheet.   Maybe import python library to handle this.  I just think Testcomplete may has its own solution.  

The Sheets collection has to be accessed using Item just like the Cells collection.


For example, your line of code should change to this:


sheet =["Sheet1"]

Hope that helps.

Occasional Contributor

Hi ghuff2,

      With your solution I can read sheet in python now.   Thank you very much.

Occasional Contributor

Sorry, my bad, I didnot check TC's samples installed. Actually I forgot they have bunches of examples there.

SmartBear Alumni (Retired)

Getting data from Excel is always a tough question. There are many ways of how it can be done. It's very important to choose the best way based on the current requirements. Our SmartBear Community Leader in TestComplete, @shankar_r, shares best practices with us. Watch the interview here:

Tanya Yatskovskaya
SmartBear Community and Education Manager

Showing results for 
Search instead for 
Did you mean: