Openpyxl: “permission denied” but Excel sheet not open

大憨熊 提交于 2020-12-08 04:47:57

问题


I have a piece of code I've been using successfully for quite a while. There is a piece of it that loops through a small list of employees and writes each of their top 20 products to an Excel sheet. Now it is often (but not always) throwing an error: (see below for full traceback)

PermissionError: [Errno 13] Permission denied:

I.e., the error you'd get if you accidentally had the spreadsheet open when running the code. This is not the case now. Here's the relevant code:

for e in employee_list:
    df4 = e
    df4 = pd.DataFrame()
    df4 = df4.append(df3.loc[df3['Employee'] == e], sort = False)

    book = load_workbook(filename)
    sheet = e + '_qtr'
    writer = pd.ExcelWriter(filename, engine='openpyxl')
    writer.book = book
    writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
    df4.to_excel (writer, sheet)
    writer.save()

A thought I had was perhaps the code is running faster now? I made some minor changes to the code meant to streamline it before this error started happening. Could it be that OpenPyXL is still working on saving when the loop comes back around?

Any help is appreciated!

Full traceback:

Traceback (most recent call last):
  File "scorecard_3.py", line 390, in <module>
    sc_attrib(f, p)
  File "scorecard_3.py", line 367, in sc_attrib
    writer.save()
  File "C:\Users\arbit\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\io\excel.py", line 1018, in save
    return self.book.save(self.path)
  File "C:\Users\arbit\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\workbook\workbook.py", line 367, in save
    save_workbook(self, filename)
  File "C:\Users\arbit\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\writer\excel.py", line 282, in save_workbook
    archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
  File "C:\Users\arbit\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1090, in __init__
    self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\arbit\\OneDrive\\Documents\\Scorecard 3.0.xlsx'

回答1:


I figured it out. Microsoft Onedrive was trying to upload the file to the cloud while I was working with it. I've changed those settings and now it works fine!



来源:https://stackoverflow.com/questions/53142528/openpyxl-permission-denied-but-excel-sheet-not-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!