【发布时间】: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