无法在 openpyxl 中保存 Excel 电子表格 - Python [英] Cant save excel spread sheet in openpyxl - Python

查看:84
本文介绍了无法在 openpyxl 中保存 Excel 电子表格 - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 openpyxl 中时,我无法保存我正在处理的活动文件.

I can't save the active file I'm working in when I'm in openpyxl.

wb_obj =  load_workbook(filename="C:\\Users\\timde\PycharmProjects\\starshipit\\test.xlsx", read_only=False)
sheet_obj = wb_obj.active
sheet_obj.cell(row=2, column=10).value = 500
wb_obj.save("test.xlsx")

我恢复了这个错误

 File "C:/Users/timde/PycharmProjects/starshipit/writeback_to_sheet.py", line 22, in <module>
    write_back()
  File "C:/Users/timde/PycharmProjects/starshipit/writeback_to_sheet.py", line 15, in write_back
    wb_obj.save("test.xlsx")
  File "C:\Users\timde\PycharmProjects\starshipit\venv\lib\site-packages\openpyxl\workbook\workbook.py", line 392, in save
    save_workbook(self, filename)
  File "C:\Users\timde\PycharmProjects\starshipit\venv\lib\site-packages\openpyxl\writer\excel.py", line 291, in save_workbook
    archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
  File "C:\Users\timde\AppData\Local\Programs\Python\Python38-32\lib\zipfile.py", line 1251, in __init__
    self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'test.xlsx'

但是如果我将文件名更改为 test1.xlsx 它会创建一个新文件并为我保存.

But if I change the file name to test1.xlsx It creates a new file and saves it for me.

所以问题只是保存我所在的当前工作簿

So the issue is only saving the current workbook I am in as itself

先谢谢大家.非常感谢

推荐答案

MS Office 应用程序通常对它们打开的文件进行写锁定.由于您在 Excel 中打开了工作簿,因此 Python 将无法打开同一个工作簿.这表现为您看到的 PermissionError.简单的解决方法是,当你想在别处使用它时,在 Excel 中关闭它.

MS Office applications generally write-lock the files that they open. Since you have your workbook open in Excel, Python will not be able to open the same workbook. This manifests itself as the PermissionError that you are seeing. The simple solution is to close the file in Excel when you want to use it elsewhere.

这篇关于无法在 openpyxl 中保存 Excel 电子表格 - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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