使用 Python 从 Microsoft Teams 获取文件

Chr*_*ich 10 python-3.x office365 office365api microsoft-teams

Teams 似乎缺乏将文件镜像到共享目录的任何本机方法。我正在尝试使用 Python(或其他语言,但首选 python!):

A。使用Python直接从微软团队拉入内存并与Pandas一起处理

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

我发现了这个,但无法让它与团队一起工作 - 团队 URL 看起来与这些完全不同。如何使用工作或学校帐户在 Python 中读取 SharePoint Online (Office365) Excel 文件?

不过,这似乎很接近我想做的事情。我还在 PyPi 存储库上找到了“pymsteams”。https://pypi.org/project/pymsteams/似乎只是让您向 Teams 发送消息而没有其他功能?除非我误解了什么。

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

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

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)
Run Code Online (Sandbox Code Playgroud)

尝试运行上面的代码会出现 AttributeError: 'NoneType' object has no attribute 'text'

完整的堆栈跟踪:

runfile('H:/repos/foo/untitled0.py', wdir='H:/repos/foo')
Traceback (most recent call last):

  File "<ipython-input-35-314ab7dc63c9>", line 1, in <module>
    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 <module>
    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'
Run Code Online (Sandbox Code Playgroud)

小智 8

我已设法使用您链接的 Office-365-REST-Python-Client 使其正常工作。

如果您使用的是 SharePoint Online,则需要设置仅应用程序原则,并使用 acquire_token_for_app 函数而不是 acquire_token_for_user 进行连接,然后传递 client_id 和 client_secret 而不是用户名和密码。

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())
Run Code Online (Sandbox Code Playgroud)

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