#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Trouble getting started with Python and Excel


    It has been years since I last worked with Python, and even then it was on an extremely basic level.
    Now I have been asked to develop a test harness in Python for a Java application.

    So... I want to sort out reading in data from spreadsheets that will then be sent off to the Java application. I have downloaded and installed the xlrd, xlwt, xlutils modules. I am just going through some simple tutorials at the moment and I am unsure as to why the following does not work:

    Code:
    from xlrd import *
    from xlwt import *
    from xlutils import *
    from xlrd import open_workbook
    book = open_workbook('simple.xls')
    print book.nsheets
    for sheet_index in range(book.nsheets):
         print book.sheet_by_index(sheet_index)
    print book.sheet_names()
    for sheet_name in book.sheet_names():
         print book.sheet_by_name(sheet_name)
    for sheet in book.sheets():
         print sheet
    I am getting the following error:
    AttributeError: 'module' object has no attribute 'nsheets'

    Can anyone tell me why this is happening? I mean, I was under the impression that everything needed for working with excel is included in the modules I downloaded.

    If anyone can shed some light that would be great - I have next to no idea what I am doing.....
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0
    I should add that, in case I need to come back and ask more questions later, my spreadsheet is just 2 columns, 10 rows, counting 1-20. Nothing fancy.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,839
    Rep Power
    480
    And the error occurs on the line

    print book.nsheets

    ?

    I've downloaded but not installed the xlrd package. Indeed, it has code to set an nsheets attribute of something.

    bk.nsheets = len(bk._sheet_list)

    Was that line executed? Was the bk object returned? Sorry, I don't know the answer.


    Does the example program work?
    Code:
    xlrd-0.9.2\xlrd\examples
      namesdemo.xls
      xlrdnameAPIdemo.py
    Last edited by b49P23TIvg; September 23rd, 2013 at 08:11 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0
    Yes - the example works just fine. It is strange!

    The tutorial I am going though is here:
    Python with Excel Tutorial

    I am beginning to wonder if there is anyway around working with Excel files?
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,839
    Rep Power
    480
    I always export the page I'm interested in as one of the text formats. If I had to do this more than a few times a year I'd learn to use one of these direct connection modules.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0
    We work with a lot of spreadsheets so maybe be able to work with this is a better idea.

    Though I am not sure why they have asked someone with no programming skills when they have a team of software engineers!

IMN logo majestic logo threadwatch logo seochat tools logo