![](/img/trans.png)
[英]Python CSV to Excel sheet by sheet with openpyxl [NOT OPTIMIZED]
[英]Cant save excel spread sheet in openpyxl - Python
當我在 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,它會創建一個新文件並為我保存。
所以問題只是保存我所在的當前工作簿本身
謝謝大家。 非常感激
MS Office 應用程序通常會對它們打開的文件進行寫鎖定。 由於您在 Excel 中打開了工作簿,因此 Python 將無法打開同一工作簿。 這表現為您看到的PermissionError
。 簡單的解決方法是當你想在其他地方使用它時,關閉 Excel 中的文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.