OSError: [Errno 22] Invalid argument in load_workbook - python

When I use openpyxl operating excel, the code below raises the error. I cannot figure out the the reason.
wb=load_workbook(r'C:\Users\Administrator\Desktop\11.xls')
Error message:
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe
C:/Users/Administrator/Desktop/data_process.py
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/data_process.py", line 3, in <module>
wb = load_workbook(r'‪C:\Users\Administrator\Desktop\11.xls')
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\reader\excel.py", line 171, in load_workbook
archive = _validate_archive(filename)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\reader\excel.py", line 118, in _validate_archive
archive = ZipFile(filename, 'r', ZIP_DEFLATED)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\zipfile.py", line 1082, in __init__
self.fp = io.open(file, filemode)
OSError: [Errno 22] Invalid argument: '\u202aC:\\Users\\Administrator\\Desktop\\11.xls'

Try the following,
instead of 1 '\' try using double \
from openpyxl import load_workbook
wb=load_workbook(r'C:\\Users\\Administrator\\Desktop\\11.xls')
Let me know if this works.

Related

Why can't I access the excel file in python?

I'm trying to use some data that I have in an excel file. However, I'm getting an error saying that it doesn't find the file. I've looked up and the directory and the file name are correct, What am I doing wrong?
Here is the code:
import os
import pandas as pd
print(os.getcwd())
df = pd.read_excel(r'C:/Users/Eder/Desktop/TFG/Data/Interpolation_sample.xlsx',
index_col =0,parse_dates=True, sheet_name='sheet3')
And the answer from the console:
runcell(0, 'C:/Users/Eder/untitled0.py')
C:\Users\Eder\Desktop\TFG\Data
Traceback (most recent call last):
File "C:\Users\Eder\untitled0.py", line 14, in <module>
index_col =0,parse_dates=True, sheet_name='sheet3')
File "E:\Anaconda3\lib\site-packages\pandas\util\_decorators.py", line 299, in wrapper
return func(*args, **kwargs)
File "E:\Anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 336, in read_excel
io = ExcelFile(io, storage_options=storage_options, engine=engine)
File "E:\Anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 1072, in __init__
content=path_or_buffer, storage_options=storage_options
File "E:\Anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 950, in inspect_excel_format
content_or_path, "rb", storage_options=storage_options, is_text=False
File "E:\Anaconda3\lib\site-packages\pandas\io\common.py", line 651, in get_handle
handle = open(handle, ioargs.mode)
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Eder\\Desktop\\TFG\\Data\\Interpolation_sample.xlsx'
I've figured out a way to solve the problem. I just changed the name of the file from 'Interpolation_sample' to 'Interpolation sample'. I don't know why, but the underscore in the file name is what was causing this error.

How to get permission for file editing in python(pandas)

I am trying to modify existing excel file on Windows using Python via Pandas, but the program gives me an error.
This is a sample of my simple program:
df_read = pd.read_excel("C:\\Users\\77888\\Desktop\\HKR_ОТЧЕТЫ\\Ноябрь 2020\\2020-11-03.xlsx")
df = pd.DataFrame({"Время":[1], "Сумма счета":[1], "Столы":[1], "Заказ":[1]})
df = df.append({"Время":1, "Сумма счета":1, "Столы":1, "Заказ":1}, ignore_index = True)
path = "C:\\Users\\77888\\Desktop\\HKR_ОТЧЕТЫ\\Ноябрь 2020\\2020-11-03.xlsx"
assert os.path.isfile(path)
writer = pd.ExcelWriter(path, engine = 'xlsxwriter')
writer.save()
Here is the error:
Traceback (most recent call last):
File "C:\Users\77888\AppData\Local\Programs\Python\Python39\lib\site-packages\xlsxwriter\workbook.py", line 320, in close
self._store_workbook()
File "C:\Users\77888\AppData\Local\Programs\Python\Python39\lib\site-packages\xlsxwriter\workbook.py", line 638, in _store_workbook
raise e
File "C:\Users\77888\AppData\Local\Programs\Python\Python39\lib\site-packages\xlsxwriter\workbook.py", line 635, in _store_workbook
xlsx_file = ZipFile(self.filename, "w", compression=ZIP_DEFLATED,
File "C:\Users\77888\AppData\Local\Programs\Python\Python39\lib\zipfile.py", line 1239, in init
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'C:\Users\77888\Desktop\HKR_ОТЧЕТЫ\Ноябрь 2020\2020-11-03.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\77888\Desktop\HKR_ОТЧЕТЫ\check.pu", line 12, in
writer.save()
File "C:\Users\77888\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\excel_xlsxwriter.py", line 193, in save
return self.book.close()
File "C:\Users\77888\AppData\Local\Programs\Python\Python39\lib\site-packages\xlsxwriter\workbook.py", line 322, in close
raise FileCreateError(e)
xlsxwriter.exceptions.FileCreateError: [Errno 13] Permission denied: 'C:\Users\77888\Desktop\HKR_ОТЧЕТЫ\Ноябрь 2020\2020-11-03.xlsx'
[Finished in 1.531s]
The most common cause of this issue on Windows is that the xlsx file being created is already open in Excel. For example:
# A simple pandas/xlsxwriter program.
C:\jmcnamara>type pandas_simple.py
import pandas as pd
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
# Run the program.
C:\jmcnamara>python pandas_simple.py
# Open the file in Excel.
C:\jmcnamara>pandas_simple.xlsx
# Try to rerun the program while the file is open.
C:\jmcnamara>python pandas_simple.py
Traceback (most recent call last):
File "C:\python\xlsxwriter\workbook.py", line 320, in close
File "C:\python\xlsxwriter\workbook.py", line 638, in _store_workbook
File "C:\python\xlsxwriter\workbook.py", line 636, in _store_workbook
File "C:\python\zipfile.py", line 1204, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'pandas_simple.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pandas_simple.py", line 8, in <module>
writer.save()
File "C:\python\site-packages\pandas\io\excel.py", line 1732, in save
return self.book.close()
File "C:\python\xlsxwriter\workbook.py", line 322, in close
xlsxwriter.exceptions.FileCreateError: [Errno 13] Permission denied: 'pandas_simple.xlsx'
As you can see the error is similar to yours.
This warning can also happen if you don't have write permissions for the output directory. That should be easy to check by trying to create a file in the target directory.
right click and run code with administrator permissions.
Through changing the privileges by running the programs you want use as "Run as administrator" for example I was using Excel in Pycharm so I changed to administrator privileges for both excel and Pycharm and got no error.

FileNotFoundError: [Errno 2] No such file or directory - Can't solve a Path problem

I have this problem, I'm trying to run the script to download Springers free books [https://towardsdatascience.com/springer-has-released-65-machine-learning-and-data-books-for-free-961f8181f189], but many things start to go wrong.
I solved some of the problems but now I'm stuck.
C:\Windows\system32>python C:\Users\loren\Desktop\springer_free_books-master\main.py
Traceback (most recent call last):
File "C:\Users\loren\Desktop\springer_free_books-master\main.py", line 42, in <module>
books.to_excel(table_path)
File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\generic.py", line 2175, in to_excel
formatter.write(
File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\formats\excel.py", line 738, in write
writer.save()
File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\excel\_openpyxl.py", line 43, in save
return self.book.save(self.path)
File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\workbook\workbook.py", line 392, in save
save_workbook(self, filename)
File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\writer\excel.py", line 291, in save_workbook
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\zipfile.py", line 1251, in __init__
self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: 'downloads\\table_v4.xlsx'
This is part of the code, were table_path is introduced.
table_url = 'https://resource-cms.springernature.com/springer-cms/rest/v1/content/17858272/data/v4'
table = 'table_' + table_url.split('/')[-1] + '.xlsx'
table_path = os.path.join(folder, table)
if not os.path.exists(table_path):
books = pd.read_excel(table_url)
# Save table
books.to_excel(table_path)
else:
books = pd.read_excel(table_path, index_col=0, header=0)
Try to create the destination directory before calling .to_excel() to ensure a valid writable directory exists. Make sure the os module is imported:
import os # add to your imports
and replace
books.to_excel(table_path)
with
os.makedirs(folder, exist_ok=True)
books.to_excel(table_path)

Unable to open xlsx file with xlrd

I am getting an error file is not support in xlrd-0.7.1.
The file is saved in xlsx format
Traceback (most recent call last):
File "C:\Users\jawed\workspace\test\Excelproject.py", line 8, in <module>
workbook=xlrd.open_workbook(file_location)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 425, in open_workbook
on_demand=on_demand,
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 878, in biff2_8_load
f = open(filename, open_mode)
IOError: [Errno 2] No such file or directory: 'C:\\Users\\jawed\\workspace\\IAMarks.xls'
The file doesn't exist.
Check the location of the file before calling the function:
import os
if os.path.isfile(file_location):
workbook = xlrd.open_workbook(file_location)
else:
# tell the user they've done something wrong
A possibly more Pythonic way to do it (see EAFP) is in a try/except block:
try:
workbook = xlrd.open_workbook(file_location)
except IOError as error:
print(error)
# tell the user they've done something wrong

Saving Workbook in Python using xlwt gives error

This problem has been happening to me a lot lately.
When I run this code:
import xlwt
wb = xlwt.Workbook()
sheet = wb.add_sheet("Random")
sheet.write(0,0,"hello!")
wb.save("test.xls")
It gives me the error:
Traceback (most recent call last):
File "<module2>", line 16, in <module>
File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 662, in save
doc.save(filename, self.get_biff_data())
File "C:\Python27\lib\site-packages\xlwt\CompoundDoc.py", line 261, in save
f = open(file_name_or_filelike_obj, 'w+b')
IOError: [Errno 13] Permission denied: 'test.xls'
I've searched for the answer, but I couldn't find it.
Any help would be greatly appreciated!

Categories

Resources