【问题标题】:Upload file in SharePoint using python使用 python 在 SharePoint 中上传文件
【发布时间】:2020-08-09 16:07:27
【问题描述】:

正在尝试使用 Python 脚本上传文件,当运行代码时它没有给我任何错误,但无法在我的共享点文件夹中上传文件。

import requests
from shareplum import Office365
from config import config

# get data from configuration
username = config['sp_user']
password = config['sp_password']
site_name = config['sp_site_name']
base_path = config['sp_base_path']
doc_library = config['sp_doc_library']

file_name = "cat_pic.jpg"

# Obtain auth cookie
authcookie = Office365(base_path, username=username, password=password).GetCookies()
session = requests.Session()
session.cookies = authcookie
session.headers.update({'user-agent': 'python_bite/v1'})
session.headers.update({'accept': 'application/json;odata=verbose'})


# perform the actual upload
with open( file_name, 'rb') as file_input:
    try: 
        response = session.post( 
            url=base_path + "/sites/" + site_name + "/Shared%20Documents/Forms/AllItems.aspx/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='" 
            + file_name + "',overwrite=true)",
            data=file_input)
    except Exception as err: 
        print("Some error occurred: " + str(err))

config.py

config = dict()
config['sp_user'] = 'email'
config['sp_password'] = 'pass
config['sp_base_path'] = 'https://bboxxeng.sharepoint.com'
config['sp_site_name'] = 'TESTIAN'
config['sp_doc_library'] = 'Test'

这是我的共享点的网址https://bboxxeng.sharepoint.com/sites/TESTIAN/Shared%20Documents/Forms/AllItems.aspx 我已经在其中创建了一个名为 Test...的文件夹...

感谢您回答我的问题。

【问题讨论】:

  • 你能分享你得到的确切错误
  • 嗨@CalebNjiiri,运行脚本后没有显示错误。但是当我检查我的 Sharepoint 文件夹时,上传的文件不存在。

标签: python python-3.x sharepoint file-upload


【解决方案1】:

修改代码如下。

response = session.post( 
            url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents/"+doc_library+"')/Files/add(url='" 
            + file_name + "',overwrite=true)",
            data=file_input)

【讨论】:

  • 谢谢,通过编辑我的配置文件并将 doc_library 更改为 Shared%20Documents/Test
  • 很高兴听到您解决了这个问题,感谢您的分享。如果我的回复对您有帮助,您可以将回复标记为答案,这将使遇到类似问题的其他人更容易在此论坛中搜索有效的解决方案。
猜你喜欢
  • 1970-01-01
  • 2020-08-19
  • 1970-01-01
  • 2018-12-28
  • 2014-07-31
  • 1970-01-01
  • 2023-04-06
  • 2017-02-11
  • 2016-09-23
相关资源
最近更新 更多