Openpyxl:“权限被拒绝"但 Excel 工作表未打开 [英] Openpyxl: "permission denied" but Excel sheet not open

查看:280
本文介绍了Openpyxl:“权限被拒绝"但 Excel 工作表未打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码我已经成功使用了一段时间.其中有一段循环遍历一小部分员工并将其排名前 20 的产品中的每一个写入 Excel 表格.现在它经常(但不总是)抛出一个错误:(完整回溯见下文)

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)

权限错误:[Errno 13] 权限被拒绝:

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

我有一个想法,也许代码现在运行得更快了?在此错误开始发生之前,我对代码进行了一些小的更改以简化它.当循环回来时,OpenPyXL 是否仍在努力保存?

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?

感谢任何帮助!

完整回溯:

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'

推荐答案

我想通了.当我使用它时,Microsoft Onedrive 试图将文件上传到云.我已经更改了这些设置,现在可以正常工作了!

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!

这篇关于Openpyxl:“权限被拒绝"但 Excel 工作表未打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆