Forum Discussion

sreekanth332's avatar
sreekanth332
New Contributor
7 years ago

Unable read excel file with xlrd package

Hi: I wrote a code in python to read excel file (.xlsx).

 

Used the import package xlrd. Placed the appropriate package in C:\Program Files (x86)\SmartBear\TestComplete 11\Bin\Extensions\Python\Python34\Lib

 

import xlrd code works fine but Test complete doesnot recognize attribute open_workbook.

 

Code works fine in Python IDLE but Test Complete does not recognize this attribute.  

 

def xlrd_package_check():
import xlrd
path='C:\\Users\H191725\Desktop\\readwritepython.xlsx'
book=xlrd.open_workbook(path)

# The above line has issue
# print number of sheets
Log.message(book.nsheets)
# print sheet names
message(book.sheet_names())
# get the first worksheet
first_sheet = book.sheet_by_index(2)
# read a row
Log.message(first_sheet.row_values(0))
# read a cell
cell = first_sheet.cell(0,0)
Log.message(cell)
Log.Message(cell.value)

4 Replies

  • baxatob's avatar
    baxatob
    Community Hero

    Hi,

     

    Did you receive any error?

     

    Try to add a path to your package:

     

    from os import sys
    
    sys.path.insert(0, '%PATH_TO_PYTHON_DIRECTORY%\Lib\site-packages')
    • sreekanth332's avatar
      sreekanth332
      New Contributor

      Hi Baxatob:

       

      Yes  Iam receiving an error 

       

      AttributeError 'module' object has no attribute 'load_workbook' Error location: Unit: "pythoncode\pythonworks\Script\Unit1" Line: 39 Column: 1.

      AttributeError
      'module' object has no attribute 'load_workbook'


       

      THanks for your help adding the python path helps with execution of code.

       

      But do i have to install Python in all system where i execute the Test Complete Code. 

      I have few systems with Test Execute and I don't have Python IDLE installed there.

       

       

      • baxatob's avatar
        baxatob
        Community Hero

        Hi,

         

        Glad to hear that it helps.


        You don't need to install Python, because TestComplete/TestExecute already go with Python interpreter. However you need to install your third-party package. Usually we do it using pip-installer, which is a part of a standard Python installation. But pip is not included into TC/TE installation.

         

        So you can:
        - automatically install the standard Python with pip module, then install your package using 'pip install' command, and finally assign the system path to it;
        - manually install pip in TC/TE Python directory, then automatically install your package using 'pip install' command;
        - manually install your package in TC/TE Python directory.

         

        [!] Manual installation of Python libraries is not trivial and requires some attention and understanding of what you do, but all of these procedures are well documented - https://docs.python.org/3/install/

         

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    If your code is a direct copy/paste of what you actually have in TC... I'm not a Python coder, but your path...  Are you missing some backslash characters?  IIRC, Python, like JavaScript, requires the backslash to be doubled to be recognized.  The problem might not be in the package but in your path.