Openpyxl:“权限被拒绝”但Excel工作表未打开

时间:2018-11-04 15:48:07

标签: python excel pandas openpyxl

我有一段代码已经成功使用了一段时间了。其中有一小部分遍历一小部分员工,并将他们的前20名产品中的每一个都写到Excel工作表中。现在经常(但并非总是)引发错误:(请参阅下文了解完整的追溯信息)

  

PermissionError:[Errno 13]权限被拒绝:

即,如果您在运行代码时不小心打开了电子表格,则会收到错误消息。现在不是这种情况。以下是相关代码:

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()

我曾经以为代码现在可以运行得更快了?在此错误开始发生之前,我对代码进行了一些小的更改以简化代码。当循环返回时,可能是OpenPyXL仍在进行保存吗?

感谢您的帮助!

完整追溯:

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 个答案:

答案 0 :(得分:0)

我知道了。我正在使用Microsoft Onedrive尝试将文件上传到云中。我已经更改了这些设置,现在可以正常使用了!