I need to read excel files and compare them and produce new outcomes using Python. i have realized that by suing the following codes I can read an excel file:

import xlrd
wb = xlrd.open_workbook('values.xls')
wb.sheet_names()

But i keep getting error of:
Traceback (most recent call last):
File "C:/Python26/Atest/filename.py", line 2, in <module>
wb = xlrd.open_workbook('values.xls')
File "C:\Python26\Lib\site-packages\xlrd\__init__.py", line 366, in open_workbook
formatting_info=formatting_info,
File "C:\Python26\Lib\site-packages\xlrd\__init__.py", line 725, in __init__
f = open(filename, open_mode)
IOError: [Errno 2] No such file or directory: 'values.xls'

I am new to python and I think teh problem might be misplacement of excel sheets in wrong folder. Do they have to be in a specific folder?

thanks

btw I found the above codes regarding reading excel files in python at:
http://scienceoss.com/read-excel-files-from-python/
it might be of help to others

Member Avatar for leegeorg07

i would say place the document in the first python folder (the one you get when you open python) and it should work, also you might want to add in the mode "r" after your file name:

myfile = open("excelfile.xls", "r")

i hope these help.

Make sure values.xls is in the same folder as your script. If you are using office 2007, make sure you saved the file as values.xls and not values.xlsx (the default).

tnx guys but i still have the same problem. it just says it doesn't find the directory the excel file is in!

Try using the absolute path, such as
"C:\\Documents and Settings\\Paul\\Desktop\\Programming\\Excel\\values.xls"

Remember that the double backslash is used so there are no escape characters such as \n used accidentally. There is a way to get around that and that is putting an r in front of the first " sign. That formats it into a raw string and escape characters are ignored.

Try using the absolute path, such as
"C:\\Documents and Settings\\Paul\\Desktop\\Programming\\Excel\\values.xls"

Remember that the double backslash is used so there are no escape characters such as \n used accidentally. There is a way to get around that and that is putting an r in front of the first " sign. That formats it into a raw string and escape characters are ignored.

Thanks! I used ur advices and also SCRU's and it worked! so two things should be done:
- excel sheets should be placed in script folder
- the name of the excel sheet confuses python and the path should be given using slashes

thnx again!

Thanks a lot..This helped me a lot.

  1. Used absolute path with \
  2. Kept xls in the script's folder
  3. saved file as .xls instead of .xlsx

If you're using openpyxl instead of xlrd to read excel files, you must use .xlsx or .xlsm extension.

just put this together for myself in the event your still in need:

from openpyxl.reader.excel import load_workbook
def readxl():
    database = []
    wb=load_workbook('file name')
    for sheet in wb.worksheets:
        for row in sheet.rows:
            values=[]
            for cell in row:
                values.append(cell.value)
            database.append(values)

    return database
commented: good code snippet +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.