将 Powershell 脚本转换为 Python 3 [英] Convert the Powershell script to Python 3

查看:80
本文介绍了将 Powershell 脚本转换为 Python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Linux 执行上传文件以使用 Python 共享点.但是,我通过谷歌搜索尝试了很多,但没有任何帮助.最后我得到了一个有效的 power shell 脚本.因此请求帮助将以下脚本转换为 Python 3

I'm trying to perform an upload files from Linux to share point using Python. However I tried a lot by googling but nothing help. At last I got a power shell script that is working. So requesting for help to convert the below script to Python 3

Specify tenant admin and site URL
$User = "justin.jacob@spidersoft.in"
$SiteURL = "https://test-my.sharepoint.com/personal/justin_jacob_spidersoftin";


$Folder = "C:\Users\justin.jacob\Desktop\New folder"
$DocLibName = "Documents"

#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"


$Password  = ConvertTo-SecureString ‘123@123’ -AsPlainText -Force


#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)


$Context.Credentials = $Creds

#Retrieve list
$List = $Context.Web.Lists.GetByTitle("$DocLibName")


$Context.Load($List)



$Context.ExecuteQuery()

#Upload file
Foreach ($File in (dir $Folder -File))
{
$FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $File
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}

推荐答案

我了解到您想上传文件到sharepoint,您可以参考以下代码:

I understand you want to upload files to sharepoint, you can take a reference of below code:

import os
from config import config
from shareplum import Site
from shareplum import Office365
from shareplum.site import Version

# get data from configuration
username = config['sp_user']
password = config['sp_password']

authcookie = Office365('https://xxx.sharepoint.com', username=username, password=password).GetCookies()

site = Site('https://xxx.sharepoint.com/sites/abc',version=Version.v365, authcookie=authcookie)
spfolder = site.Folder('Shared Documents/testfolder')

for root, dirs, files in os.walk(r"D:\mytestfolder"): 
    for file in files:
        filepath = os.path.join(root, file)
        print(filepath)

        # perform the actual upload
        with open(filepath, 'rb+') as file_input:
            try: 
                spfolder.upload_file(file_input, file)
            except Exception as err: 
                print("Some error occurred: " + str(err))

代码使用以下python库:

The code uses following python library:

这篇关于将 Powershell 脚本转换为 Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆