1
votes

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]

2
try adding the mode parameter to ExcelWriter - > ExcelWriter(path, engine = 'xlsxwriter', mode= ‘a’)Petronella
Now it says "Append mode is not supported with xlsxwriter"Dias Mashikov
do you have right from the OS to edit the file?Petronella
I am not sure about it. Now the program says nothing whether I do have permission or not. I tried to get permission for my folder, but I was not sure exactly how to do that correctly. I am new to WindowsDias Mashikov
right click on excel file, open properties dialog, go to security tab and you should see the users and groups with permissions. what do you mean the program doesn't say anything now, is it working?Petronella

2 Answers

0
votes

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.

0
votes

right click and run code with administrator permissions.