0

I would like to read an excel-file with python. My first attempt is about reading the worksheets, the second attempt would then be about reading cells. Unfortunately, I am stuck with the first step.

The code:

import openpyxl 
wb = openpyxl.load_workbook ("C:\\Users\\Alex\\Documents\\Python\\Übung\\example1.xlxs")

wb.get_sheet_by_name()

the following messages appear:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-26-7b234f637152> in <module>()
      1 import openpyxl
----> 2 wb = openpyxl.load_workbook("\\Users\\Alex\\Documents\\Python\\Übung\\example1.xlxs")
      3 wb.get_sheet_by_name()

~\Anaconda3\lib\site-packages\openpyxl\reader\excel.py in load_workbook(filename, read_only, keep_vba, data_only, guess_types, keep_links)
    169 
    170     """
--> 171     archive = _validate_archive(filename)
    172     read_only = read_only
    173 

~\Anaconda3\lib\site-packages\openpyxl\reader\excel.py in _validate_archive(filename)
    116 
    117     try:
--> 118         archive = ZipFile(filename, 'r', ZIP_DEFLATED)
    119     except BadZipfile:
    120         f = repair_central_directory(filename, is_file_like)

~\Anaconda3\lib\zipfile.py in __init__(self, file, mode, compression, allowZip64)
   1088             while True:
   1089                 try:
-> 1090                     self.fp = io.open(file, filemode)
   1091                 except OSError:
   1092                     if filemode in modeDict:

FileNotFoundError: [Errno 2] No such file or directory: '\\Users\\Alex\\Documents\\Python\\Übung\\example1.xlxs'

I referenced the file using an absolute path and it exits, but why do I get an error that the file is not found nevertheless? And what about the rest of the error messages, I have no clue what they mean or whether this can be dismissed. Thanks for help.

1
  • Have you tried Panda? pd.read_excel('C:\Users\Alex\Documents\Python\Übung\example1.xlxs'). This will read the file then you can store in a data frame Commented May 27, 2018 at 11:01

3 Answers 3

2

You have a typo in your code:

example1.xlxs -> This extension does not exist.

The correct Excelfile extension is xlsx.

Sign up to request clarification or add additional context in comments.

2 Comments

I hadn't even noticed that typo. Even with the correction of his path the code would not have run without that catch.
Thanks for your answer, I had a typo indeed!
0

It's a little bit difficult to read through the error without code blocks, but after sifting a bit, the trace reads,

FileNotFoundError: [Errno2] No such file or directory:
'\Users\Alex\Documents\Python\Übung\example1.xlxs'

And in your code, you have

wb = openpyxl.load_workbook("\Users\Alex\Documents\Python\Übung\example1.xlxs")

So, it seems you haven't supplied the full path, you're missing the C:\ portion. Could it be as easy as that?

Feel free to post back after confirming you've correctly entered the file path.

Comments

-1

For most of the data operations (including Data Analysis), pandas have been a goto library for many. Even I strongly recommend using pandas for you:

import pandas as pd
df = pd.read_excel("excelFilePath.xlsx", sheet_name="Sheet1", usecols="C,D,E")

Note: in the code above usecols="C,D,E" are the column numbers and not the exact column names. JFYI: https://github.com/pandas-dev/pandas/issues/18273

Read more here: https://pandas.pydata.org/pandas-docs/stable/

2 Comments

Pandas is a great library, which in turn relies on other libraries (in this case xlrd) for reading file formats. However, the problem here is a simple typo in the file name.
Exactly, his question is already answered above & he is already reading pandas now. Everyone know what you pointed, I just pointed him to something more useful. Relax man, its nothing against 'openpyxl'. Chill

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.