OpenPyXL FileNotFoundError: [Errno2] No such file or directory

Asked 2 years ago, Updated 2 years ago, 124 views

Goal


Read an Excel book called Sales Data.xlsx (There are three sheets in this book: April sales, May sales, and June sales.)

Actual Results

Error Message Occurs When You Enter Second Line Code

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    wb=openpyxl.load_workbook('Sales Data.xlsx')
  File "C:\Users\Name\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\reader\excel.py", line 315, inload_workbook
    reader = ExcelReader(filename, read_only, keep_vba,
  File "C:\Users\Name\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\reader\excel.py", line 124, in __init__
    self.archive=_validate_archive(fn)
  File "C:\Users\Name\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\reader\excel.py", line 96, in_validate_archive
    archive=ZipFile(filename, 'r')
  File "C:\Users\Name\AppData\Local\Programs\Python\Python39\lib\zipfile.py", line 1239, in__init__
    self.fp=io.open(file, filemode)
FileNotFoundError: [Errno2] No such file or directory: 'Sales data.xlsx'

What I've tried so far

"FileNotFoundError" thought the book was missing error.
So I saved the "program files" and "sales data .xlsx" in the same folder.
However, an error message occurs at the second line.

Where is the cause?Please let me know.

This may have been caused by running on the IDLE Shell.
When I tried running File→NewFile at the top of the IDLE Shell with Python File, I initially got an error, but when I changed the file name from sales data to uragedata, I got the desired results.

The second line no longer has an error, but if you enter the code we wrote as a goal or the sample code above from Payaneco, the following error will occur in common:

Traceback (most recent call last): File "C:\Users\Name\Documents\ExcelPython\ch01\xl_book_load.py", line 1, in <module>import openpyxl ModuleNotFoundError: No module named 'openyxyl'

Perhaps there is a problem with the import openpyxl in the first line.
(You should have entered py-mpip install openpyxl at the command prompt to prepare for the installation.)
Why did I change the file name to only half-width?

python excel openpyxl

2022-09-30 20:17

2 Answers

As you can see in the comment, the error type in the file path is likely to be the cause.

Other errors occur if the current directory running python is different from the python file.
For example, if the code in your question was C:\python\test.py, you would still get an error if C:\python\sales data.xlsx was present when you performed the following actions at a command prompt.(C:\Sales data.xlsx is not an error.)

cd/d C:\
python.\python\test.py

Please try to create and load the file as shown in the sample code below.
You can then retrieve the absolute path to see where the Excel file is loaded.
(Current directory can also be retrieved at os.getcwd)

import openpyxl

# Create and save a new sample Excel file
file_name = "Never get a filename .xlsx" 

wb=openpyxl.Workbook()
for sheet in ['April sales', 'May sales', 'June sales']:
    wb.create_sheet(sheet)
wb.remove(wb['Sheet'])#Delete automatically created sheets when new is created

wb.save(file_name)

# Re-open the saved Excel file
wb=openpyxl.load_workbook(file_name)
print(wb.sheetnames)

# Get the full path of the saved Excel file
importos
abs_path=os.path.abspath(file_name)
print('{}' created '.format(abs_path))

It was not reproduced on the IDLE Shell in your Python 3.9.6 environment.
Will the following steps resolve the problem?

Have you changed the running .py file name to openpyxl.py?
Or have you created openpyxl.py in the folder where the running .py file resides?

If it doesn't work, please add the following to the questionnaire.(The purpose is to check if Python IDLE and pip install target Python are different versions.)


2022-09-30 20:17

The contents of the file and the character code of the file name (path name) may be different in Windows series
(Perhaps the directory/filename character code is Shift-JIS?)

The easiest solution is not to create a Japanese file name

If you can't find the file (where it is), it's also a good idea to prepare a code like this

 from pathlib import Path

for fin Path.home().glob('**/*.xlsx'):
    print(f)


2022-09-30 20:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.