简体   繁体   English

使用 Python 在 OneDrive 文件夹中打开 Excel 工作簿

[英]Open excel workbook in OneDrive folder using Python

I am trying to use pandas.read_excel to read data from the Excel workbook.我正在尝试使用pandas.read_excel从 Excel 工作簿中读取数据。 However, if the workbook is stored in the OneDrive folder and synced to OneDrive, it cannot be read when it is already open.但是,如果工作簿存储在 OneDrive 文件夹中并同步到 OneDrive,则在它已打开时无法读取。 It reports an error message它报告错误消息

[Errno 13] Permission denied: 'C:\\Users\\Name\\OneDrive\\Test\\Test.xlsx' [Errno 13] 权限被拒绝:'C:\\Users\\Name\\OneDrive\\Test\\Test.xlsx'

seems failed when trying to open the file尝试打开文件时似乎失败

with open(filename, "rb") as f:使用 open(filename, "rb") 作为 f:

But when I closed the workbook, pandas.read_excel succeeded fetching data from it.但是当我关闭工作簿时, pandas.read_excel成功地从中获取了数据。 And files not in the OneDrive folder can always be accessed no matter open or not.不在 OneDrive 文件夹中的文件无论打开与否,始终可以访问。 Please see below cases:请看以下案例:

pd.read_excel(r'C:\\Users\\Name\\Test\\Book1.xlsx') # successful when book is open pd.read_excel(r'C:\\Users\\Name\\Test\\Book1.xlsx') # successful when book is closed pd.read_excel(r'C:\\Users\\Name\\OneDrive\\Book1.xlsx') # failed when book is open pd.read_excel(r'C:\\Users\\Name\\OneDrive\\Book1.xlsx') # successful when book is closed pd.read_excel(r'C:\\Users\\Name\\Test\\Book1.xlsx') # successful when book is open pd.read_excel(r'C:\\Users\\Name\\Test\\Book1.xlsx') # successful when book is closed pd.read_excel(r'C:\\Users\\Name\\OneDrive\\Book1.xlsx') # failed when book is open pd.read_excel(r'C:\\Users\\Name\\OneDrive\\Book1.xlsx') # successful when book is closed pd.read_excel(r'C:\\Users\\Name\\OneDrive\\Book1.xlsx') # failed when book is open pd.read_excel(r'C:\\Users\\Name\\OneDrive\\Book1.xlsx') # successful when book is closed

I was wondering if it is something related to OneDrive?我想知道它是否与 OneDrive 相关? And is there any way I can read data from an opened workbook stored in the OneDrive folder?有什么方法可以从存储在 OneDrive 文件夹中的打开的工作簿中读取数据? Since I want to read data from the closed workbook as well, xlwings should not be a good idea, is there a general technique I can use to read data from all excel workbooks no matter open or not, and no matter in OneDrive or not?由于我也想从关闭的工作簿中读取数据,xlwings 应该不是一个好主意,是否有一种通用技术可以用来从所有 excel 工作簿中读取数据,无论是否打开,也无论是否在 OneDrive 中?

I have had success with this by simply turning off the autosave feature in excel.通过简单地关闭excel中的自动保存功能,我已经成功了。

edit: This seems to only work with an older .xls file and not .xlsx编辑:这似乎只适用于较旧的 .xls 文件而不适用于 .xlsx

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

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