【问题标题】:Upload file created in temporary directory into SharePoint using python使用 python 将在临时目录中创建的文件上传到 SharePoint
【发布时间】:2023-01-13 16:28:35
【问题描述】:

我正在尝试使用 Python 脚本将我创建的临时 XML 文件上传到 Sharepoint。虽然我在运行代码时没有看到任何错误,但是文件没有上传到 Sharepoint 文件夹中。你能帮我吗。

# create XML attachment as temp file
tempdir = tempfile.mkdtemp()
path = os.path.join(tempdir)
temp_file = path + f'/XML_REPORT_{str(uuid.uuid4())}.xml'
with open(temp_file, 'wb') as fp:
    fp.write(tree_string)

print('temp file has been created')

base_path = 'https://company_name.sharepoint.com'
site_name = 'Testing'
doc_library = 'Shared%20Documents/General'

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

with open(temp_file, 'rb') as content_file:
    try:
      response = session.post( 
        url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='" 
        + temp_file + "',overwrite=true)",data=content_file)
      print('file added successfully')
    except Exception as err:
      print("Some error occurred: " + str(err))

这是我的共享点https://company_name.sharepoint.com/sites/Testing/Shared%20Documents/Forms/AllItems.aspx/?id=%2Fsites%2FTesting%2FShared%20Documents%2FGeneral&viewid=6357a30d%2D8562%2D4a01%2Dade1%2Dc1c413193931 的网址,我已经在其中创建了一个名为 General 的文件夹...

谢谢你回答我的问题。

【问题讨论】:

    标签: python file-upload sharepoint-api sharepointdocumentlibrary office365-rest-client


    【解决方案1】:

    根据我的研究和测试,请参考以下文档将文件上传到SharePoint:https://github.com/iamlu-coding/python-upload2sharepoint

    希望它能帮助你。感谢你的理解。

    【讨论】:

      猜你喜欢
      • 2013-12-26
      • 1970-01-01
      • 2017-12-22
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      • 2016-08-04
      • 2013-01-31
      相关资源
      最近更新 更多