使用Office365 API读取python中的.txt文件

发布于 2025-02-06 01:14:01 字数 2122 浏览 1 评论 0原文

我希望阅读使用Python存储在OneDrive中的文本文件。我有一个办公室365订阅。我使用以下代码:

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File 
username, password = "<username>", "<password>" #Fields replaced by placeholders
site_url = 'https://<company>-my.sharepoint.com/personal/<email_id>/'
file_url = 'Documents/textfile.txt'
ctx_auth = AuthenticationContext(site_url)
if ctx_auth.acquire_token_for_user(username, password):
    ctx = ClientContext(site_url, ctx_auth)
    response = File.open_binary(ctx, file_url)
    print(response)
    info = str(response.content)
    print(info)

但是运行此导致HTTP不良请求:

<Response [400]>
b'{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"serverRelativeUrl\\r\\nParameter name: Specified value is not supported for the serverRelativeUrl parameter."}}}'

在Stackoverflow上搜索,我发现file.open_binary(ctx,file_url)必须以/</< /code>用于通过Python访问SharePoint文件,但是如果我使用file_url ='/documents/textfile.txt' for OneDrive,则会提出一个错误的请求,但会有错误消息:

<Response [400]>
b'{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"Server relative urls must start with SPWeb.ServerRelativeUrl"}}}'

但是,我可以阅读文本存储在SharePoint Hassle无需的文件:

site_url = 'https://<company>.sharepoint.com/sites/sitename/'
file_url = '/sites/sitename/Shared Documents/myfile.txt'
ctx_auth = AuthenticationContext(site_url)
if ctx_auth.acquire_token_for_user(username, password):
    ctx = ClientContext(file_url, ctx_auth)
    response = File.open_binary(ctx, file_url)
    print(response)
    info = str(response.content).replace('b', '').replace('\'', '')
    print(info)

输出

<Response [200]>
Hello World!

也是我在读取OneDrive作为Pandas DataFrames中存储的Excel文件时遇到的问题。

在阅读OneDrive的文本文件时,我会做错什么?

I wish to read a text file stored in OneDrive using Python. I have an Office 365 subscription. I used the following code:

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File 
username, password = "<username>", "<password>" #Fields replaced by placeholders
site_url = 'https://<company>-my.sharepoint.com/personal/<email_id>/'
file_url = 'Documents/textfile.txt'
ctx_auth = AuthenticationContext(site_url)
if ctx_auth.acquire_token_for_user(username, password):
    ctx = ClientContext(site_url, ctx_auth)
    response = File.open_binary(ctx, file_url)
    print(response)
    info = str(response.content)
    print(info)

But running this leads to an HTTP bad request:

<Response [400]>
b'{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"serverRelativeUrl\\r\\nParameter name: Specified value is not supported for the serverRelativeUrl parameter."}}}'

Searching on StackOverflow, I found that the relative URL in File.open_binary(ctx, file_url) must begin with / for accessing SharePoint files through Python, but if I use file_url = '/Documents/textfile.txt' for OneDrive, a bad request is raised with an error message:

<Response [400]>
b'{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"Server relative urls must start with SPWeb.ServerRelativeUrl"}}}'

However, I can read text files stored in SharePoint hassle-free:

site_url = 'https://<company>.sharepoint.com/sites/sitename/'
file_url = '/sites/sitename/Shared Documents/myfile.txt'
ctx_auth = AuthenticationContext(site_url)
if ctx_auth.acquire_token_for_user(username, password):
    ctx = ClientContext(file_url, ctx_auth)
    response = File.open_binary(ctx, file_url)
    print(response)
    info = str(response.content).replace('b', '').replace('\'', '')
    print(info)

Output

<Response [200]>
Hello World!

Also, I face no problem in reading Excel files stored in OneDrive as Pandas dataframes.

What could I be doing wrong in reading text files from OneDrive?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文