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.
You have a typo in your code:
example1.xlxs
-> This extension does not exist.
The correct Excel
file extension is xlsx
.
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.
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/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.