简体   繁体   English

无法在 openpyxl 中保存 excel 电子表格 - Python

[英]Cant save excel spread sheet in openpyxl - Python

I can't save the active file I'm working in when I'm in openpyxl.当我在 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")

I get this error back我得到这个错误

 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'

But if I change the file name to test1.xlsx It creates a new file and saves it for me.但是,如果我将文件名更改为 test1.xlsx,它会创建一个新文件并为我保存。

So the issue is only saving the current workbook I am in as itself所以问题只是保存我所在的当前工作簿本身

Thank you all in advance.谢谢大家。 Much appreciated非常感激

MS Office applications generally write-lock the files that they open. MS Office 应用程序通常会对它们打开的文件进行写锁定。 Since you have your workbook open in Excel, Python will not be able to open the same workbook.由于您在 Excel 中打开了工作簿,因此 Python 将无法打开同一工作簿。 This manifests itself as the PermissionError that you are seeing.这表现为您看到的PermissionError The simple solution is to close the file in Excel when you want to use it elsewhere.简单的解决方法是当你想在其他地方使用它时,关闭 Excel 中的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM