首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Python从Microsoft团队抓取文件

使用Python从Microsoft团队抓取文件
EN

Stack Overflow用户
提问于 2019-07-19 06:58:33
回答 2查看 11.9K关注 0票数 5

团队似乎缺乏将文件镜像到共享目录的原生方法。我正在尝试使用Python (或者其他语言,但最好使用python!)执行以下任一操作:

a.使用Python直接从microsoft团队拉入内存,使用Pandas进行处理

b.将团队中的文件复制到共享网络文件夹( Python随后可以读取该文件夹)

我找到了这个,但无法让它在团队中工作-团队的URL看起来一点都不像这些。

如何使用工作或学校帐号在Python中读取SharePoint Online (Office365) Excel文件?

这看起来很接近我想要做的事情。我还在PyPi库中找到了"pymsteams“。

https://pypi.org/project/pymsteams/

这似乎只是让你向团队发送消息,而不是其他什么?除非我误解了什么。

https://pypi.org/project/Office365-REST-Python-Client/

https://pypi.org/project/pymsteams/

代码语言:javascript
运行
复制
from office365.runtime.auth.authentication_context
import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File 

url = 'https://teams.microsoft.com/l/file'
username = 'myusername'
password = 'mypassword'
relative_url ='myurl'

ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username, password)

尝试运行上面的代码会得到文本:'NoneType‘对象没有属性’AttributeError‘

完整堆栈跟踪:

代码语言:javascript
运行
复制
runfile('H:/repos/foo/untitled0.py', wdir='H:/repos/foo')
Traceback (most recent call last):

  File "", line 1, in 
    runfile('H:/repos/foo/untitled0.py', wdir='H:/foo/image_ai')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "H:/repos/image_ai/untitled0.py", line 10, in 
    ctx_auth.acquire_token_for_user(username, password)

  File "C:\ProgramData\Anaconda3\lib\site-packages\office365\runtime\auth\authentication_context.py", line 18, in acquire_token_for_user
    return self.provider.acquire_token()

  File "C:\ProgramData\Anaconda3\lib\site-packages\office365\runtime\auth\saml_token_provider.py", line 57, in acquire_token
    self.acquire_service_token(options)

  File "C:\ProgramData\Anaconda3\lib\site-packages\office365\runtime\auth\saml_token_provider.py", line 88, in acquire_service_token
    token = self.process_service_token_response(response)

  File "C:\ProgramData\Anaconda3\lib\site-packages\office365\runtime\auth\saml_token_provider.py", line 119, in process_service_token_response
    return token.text

AttributeError: 'NoneType' object has no attribute 'text'
EN

回答 2

Stack Overflow用户

发布于 2020-09-11 20:43:57

我已经设法使用你链接的Office-365-REST-Python-Client让它工作起来了。

如果您使用的是SharePoint Online,则需要获取

App-Only原则

使用acquire设置和连接

_

令牌

_

对于

_

app函数而不是acquire

_

令牌

_

对于

_

用户,然后传递一个客户端

_

id和客户端

_

加密,而不是用户名和密码。

代码语言:javascript
运行
复制
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File

client_id = 'yourclientid'
client_secret = 'yourclientsecret'
url = 'https://yoursharepointsite.com/teams/yourteam'
relative_url = '/teams/yourteam/Shared%20Documents/yourteamschannel/yourdoc.extension'
  
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_app(client_id, client_secret):
    ctx = ClientContext(url, ctx_auth)
    with open(filename, 'wb') as output_file:
        response = File.open_binary(ctx, relative_url)
        output_file.write(response.content) 
else:
    print(ctx_auth.get_last_error())

这应该会将您的文件下载到本地驱动器(通过filename变量指定),然后您可以加载到pandas等中进行处理

票数 4
EN

Stack Overflow用户

发布于 2020-09-11 20:43:57

我喜欢答案4,并尝试了一下,但它给出了这个错误:

回溯(最近一次调用):文件“读取

_

团队

_

xy

_

py.py",第3行,来自office365.runtime.auth.authentication

_

上下文导入AuthenticationContext

ImportError:没有名为office365.runtime.auth.authentication的模块

_

上下文

我发现我忽略了Ofice-365-REST-Python

_

客户端部分,因为当我通过pip安装它时,我遇到了一个不同的错误:

回溯(最近一次调用):文件“读取

_

团队

_

xy

_

py.py",第3行,来自office365.runtime.auth.authentication

_

上下文导入AuthenticationContext文件"/home/markv/.local/lib/python2.7/sitepackages/office365/runtime/auth/authentication

_

context.py",第33行

语法错误:无效语法

我希望这能帮助其他犯同样错误的人。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57103597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档