【问题标题】:Upload excel file to SharePoint Online using Python使用 Python 将 Excel 文件上传到 SharePoint Online
【发布时间】:2018-12-28 02:35:07
【问题描述】:

我正在尝试将我的 Excel 电子表格上传到我的 SharePoint Online 网站上的文档库。 SharePoint 网站上的 Sharepoint URL 和文件夹位置列在 Excel 电子表格中。

这是我现在拥有的代码:

import numpy as np
import pandas as pd
import xlwings as xw
from xlwings.constants import Direction
import sys
import requests
from requests_ntlm import HttpNtlmAuth
pd.options.mode.chained_assignment = None

def Upload():

    wb = xw.Book.caller()
    ws = wb.sheets['Sheet1']

    #Read filename from excel
    fileName = sys.argv[1]

    #Enter SharePoint ONline site and target library
    SP_URL = ws.range('C7').value
    folder_URL = ws.range('C8').value

    #Set up the url for requesting file upload
    request_URL = SP_URL + '/_api/web/getfolderbyserverrelativeurl(\'' + 
    folder_URL + '\')/Files/asdd(url=\'' + fileName + '\',overwrite=true)'

    #read in the file that we are going to upload
    file = open(fileName, 'rb')

    headers = {'Content-Type': 'application/json; odata=verbose', 'accept': 
    'application/json;odata=verbose'}
    r = requests.post(SP_URL + 
    "/_api/contextinfo",auth=HttpNtlmAuth('Domain\\username','password'), 
    headers=headers)
    formDigestValue = r.json()['d']['GetContextWebInformation'] 
    ['FormDigestValue']
    headers = {'Content-Type': 'application/json; odata=verbose', 'accept': 
    'application/json;odata=verbose', 'x-requestdigest' : formDigestValue}
    uploadResult = 
    requests.post(request_URL,auth=HttpNtlmAuth('Domain\\username','password'), 
    headers=headers, data=file.read())

我收到以下错误:

formDigestValue = r.json()['d']['GetContextWebInformation']['FormDigestValue']

KeyError: 'd'

【问题讨论】:

    标签: python excel rest python-requests sharepoint-online


    【解决方案1】:

    requests_ntlm

    允许使用请求库进行 HTTP NTLM 身份验证

    SharePoint Online 不支持 NTLM

    我建议使用 Office365-REST-Python-Client它支持指定用户凭据并使用 SharePoint REST API)包来将文件上传到 SharePoint Online,而不是 requests_ntlm,例如:

    ctx_auth = AuthenticationContext(url=settings['url'])
    if ctx_auth.acquire_token_for_user(username=settings['user_credentials']['username'],
                                           password=settings['user_credentials']['password']):
    
        ctx = ClientContext(settings['url'], ctx_auth)
        target_list = ctx.web.lists.get_by_title("Documents")
        info = FileCreationInformation()
        file_name = "Book.xlsx"
        path = "{0}/data/{1}".format(os.path.dirname(__file__), file_name)
        with open(path, 'rb') as content_file:
            info.content = content = content_file.read()
        info.url = file_name
        info.overwrite = True
        upload_file = target_list.root_folder.files.add(info)
        ctx.execute_query()
    

    【讨论】:

    • 嗨 Vadim - 感谢您提供信息。我正在使用 Anaconda Prompt 通过键入“pip install Office365-REST-Python-Client”来安装软件包,但我收到一条错误消息“无法获取 URL pypi.org/simple/Office365-REST-Python-Client - 我知道简单实际上应该是“项目” ...有什么建议吗?PS 我对此完全陌生,所以有什么帮助
    • @MMac11,刚刚尝试通过 Anaconda Prompt (Windows 10) 安装,没有问题.. 似乎问题与 pip details
    • 所以,解决方案是更新点子:python -m pip install --upgrade pip
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    相关资源
    最近更新 更多