"PermissionError: [Errno 13] Permission denied" when saving to an Excel-file using pandas
"PermissionError: [Errno 13] Permission denied" when saving to an Excel-file using pandas
我正在尝试从现有的 Excel 文件“file1”将数据加载到熊猫数据帧,对其进行一些修改,然后将数据帧导出到新的 sheet在 same 工作簿(“file1”)中。当“file1”关闭时我的代码工作正常,但如果我在 运行 我的代码同时在 Excel 打开文件,我会收到错误:PermissionError: [错误 13] 权限被拒绝。这里的问题是什么? Python 无法保存在 Excel 中同时打开的工作簿,还是这里有任何解决方法?
这是我的代码:
import pandas as pd
from openpyxl import load_workbook
#Read the input data to a panda dataframe
df = pd.read_excel(workbook_path, sheet_name = 'input_data')
---Do stuff to the dataframe--
#Access the existing excel-file
workbook = load_workbook(workbook_path)
#Create a writer-object
writer = pd.ExcelWriter(workbook_path, engine = 'openpyxl')
writer.book = workbook
#Create a new sheet called "Results"
df.to_excel(writer,sheet_name = 'Results')
#Save the workbook
writer.save()
writer.close()
这是错误的完整输出:
当文件在 Excel 中打开时,来自其他应用程序的写访问被阻止以维护打开文件中的数据完整性。这意味着您应该只能获得 read-only 访问权限。这不是特定于 Python.
某些用例可能有解决方法,让您可以通过链接的 CSV 添加数据(有关详细信息,请参阅 )。
我正在尝试从现有的 Excel 文件“file1”将数据加载到熊猫数据帧,对其进行一些修改,然后将数据帧导出到新的 sheet在 same 工作簿(“file1”)中。当“file1”关闭时我的代码工作正常,但如果我在 运行 我的代码同时在 Excel 打开文件,我会收到错误:PermissionError: [错误 13] 权限被拒绝。这里的问题是什么? Python 无法保存在 Excel 中同时打开的工作簿,还是这里有任何解决方法?
这是我的代码:
import pandas as pd
from openpyxl import load_workbook
#Read the input data to a panda dataframe
df = pd.read_excel(workbook_path, sheet_name = 'input_data')
---Do stuff to the dataframe--
#Access the existing excel-file
workbook = load_workbook(workbook_path)
#Create a writer-object
writer = pd.ExcelWriter(workbook_path, engine = 'openpyxl')
writer.book = workbook
#Create a new sheet called "Results"
df.to_excel(writer,sheet_name = 'Results')
#Save the workbook
writer.save()
writer.close()
这是错误的完整输出:
当文件在 Excel 中打开时,来自其他应用程序的写访问被阻止以维护打开文件中的数据完整性。这意味着您应该只能获得 read-only 访问权限。这不是特定于 Python.
某些用例可能有解决方法,让您可以通过链接的 CSV 添加数据(有关详细信息,请参阅