使用 Python 访问 Microsoft Sharepoint 文件和数据 [英] Accessing Microsoft Sharepoint files and data using Python

查看:363
本文介绍了使用 Python 访问 Microsoft Sharepoint 文件和数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Microsoft 共享点.我有一个 url,通过使用该 url,我需要获取照片、视频、文件夹、子文件夹、文件、帖子等总数据......并且我需要将这些数据存储在数据库中(Sql server).我正在使用 python.

所以,请任何人建议我如何做到这一点,我是访问共享点和处理此类事情的初学者.

解决方案

这是通过 Python 连接到共享点并访问 Sharepoint 的文件列表、文件夹和单个文件内容的起始代码.您可以在此基础上进行构建以满足您的需求.

请注意,此方法适用于可通过 Internet 访问的公共 Sharepoint 站点.对于托管在公司 Intranet 上的受组织限制的 Sharepoint 站点,我尚未测试此代码.

您必须稍微修改 Sharepoint 文件的链接,因为您无法使用从 Web 浏览器复制的该文件的 URL 地址在 Python 中直接访问 Sharepoint 文件.

<预><代码>从 office365.runtime.auth.authentication_context 导入 AuthenticationContext从 office365.sharepoint.client_context 导入 ClientContext从 office365.sharepoint.file 导入文件####输入######### 这将是指向您的 sharepoint 站点的 URL.# 确保只更改链接中以Your"开头的部分url_shrpt = 'https://YourOrganisation.sharepoint.com/sites/YourSharepointSiteName'username_shrpt = '你的用户名'password_shrpt = '你的密码'folder_url_shrpt = '/sites/YourSharepointSiteName/Shared%20Documents/YourSharepointFolderName/'##########################Authentication###用于对您的共享点站点进行身份验证###ctx_auth = AuthenticationContext(url_shrpt)如果 ctx_auth.acquire_token_for_user(username_shrpt, password_shrpt):ctx = ClientContext(url_shrpt, ctx_auth)网络 = ctx.webctx.load(web)ctx.execute_query()print('已通过共享点身份验证为:',web.properties['Title'])别的:打印(ctx_auth.get_last_error())###############################提取sharepoint文件夹文件名的功能######如果要提取文件夹名称而不是文件名称,则必须在下面的函数中将sub_folders = folder.files"更改为sub_folders = folder.folders"全局 print_folder_contentsdef print_folder_contents(ctx, folder_url):尝试:文件夹 = ctx.web.get_folder_by_server_relative_url(folder_url)fold_names = []sub_folders = folder.files #用文件夹替换文件以获取文件夹列表ctx.load(sub_folders)ctx.execute_query()对于 sub_folders 中的 s_folder:fold_names.append(s_folder.properties["Name"])返回 fold_names除了作为 e 的例外:print('打印出库内容的问题:', e)#################################################### 通过将您的文件夹 URL 作为输入来调用该函数filelist_shrpt=print_folder_contents(ctx,folder_url_shrpt)#打印文件夹中存在的文件列表打印(filelist_shrpt)

既然我们已经打印了 Sharepoint 中特定文件夹中存在的文件列表,下面是访问特定文件的文件内容并将其保存到本地磁盘的代码,并且知道 Sharepoint 中的文件名和路径.

#指定sharepoint文件的URL.请记住仅更改以您的"开头的链接部分file_url_shrpt = '/sites/YourSharepointSiteName/Shared%20Documents/YourSharepointFolderName/YourSharepointFileName'#将sharepoint文件内容加载到response"变量中response = File.open_binary(ctx, file_url_shrpt)#将文件保存到离线路径使用 open("Your_Offline_File_Path", 'wb') 作为 output_file:output_file.write(response.content)

您可以参考以下链接连接到SQL服务器并将内容存储在表格中:使用 Python 连接到 Microsoft SQL 服务器

https://datatofish.com/how-to-connect-python-to-sql-server-using-pyodbc/

I am using Microsoft sharepoint. I have an url, by using that url I need to get total data like photos,videos,folders,subfolders,files,posts etc... and I need to store those data in database(Sql server). I am using python.

So,Please anyone suggest me how to do this and I am beginner for accessing sharepoint and working this sort of things.

解决方案

Here's the starter code for connecting to share point through Python and accessing the list of files, folders and individual file contents of Sharepoint as well. You can build on top of this to suit your needs.

Please note that this method works for public Sharepoint sites that are accessible through internet. For Organisation restricted Sharepoint sites that are hosted on a Company's intranet, I haven't tested this code out.

You will have to modify the link to the Sharepoint file a bit since you cannot directly access a Sharepoint file in Python using the URL address of that file which is copied from the web browser.


from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File 

####inputs########
# This will be the URL that points to your sharepoint site. 
# Make sure you change only the parts of the link that start with "Your"
url_shrpt = 'https://YourOrganisation.sharepoint.com/sites/YourSharepointSiteName'
username_shrpt = 'YourUsername'
password_shrpt = 'YourPassword'
folder_url_shrpt = '/sites/YourSharepointSiteName/Shared%20Documents/YourSharepointFolderName/'

#######################



###Authentication###For authenticating into your sharepoint site###
ctx_auth = AuthenticationContext(url_shrpt)
if ctx_auth.acquire_token_for_user(username_shrpt, password_shrpt):
  ctx = ClientContext(url_shrpt, ctx_auth)
  web = ctx.web
  ctx.load(web)
  ctx.execute_query()
  print('Authenticated into sharepoint as: ',web.properties['Title'])

else:
  print(ctx_auth.get_last_error())
############################




####Function for extracting the file names of a folder in sharepoint###
###If you want to extract the folder names instead of file names, you have to change "sub_folders = folder.files" to "sub_folders = folder.folders" in the below function
global print_folder_contents
def print_folder_contents(ctx, folder_url):
    try:

        folder = ctx.web.get_folder_by_server_relative_url(folder_url)
        fold_names = []
        sub_folders = folder.files #Replace files with folders for getting list of folders
        ctx.load(sub_folders)
        ctx.execute_query()

        for s_folder in sub_folders:

            fold_names.append(s_folder.properties["Name"])

        return fold_names

    except Exception as e:
        print('Problem printing out library contents: ', e)
######################################################


# Call the function by giving your folder URL as input  
filelist_shrpt=print_folder_contents(ctx,folder_url_shrpt) 

#Print the list of files present in the folder
print(filelist_shrpt)

Now that we have printed the list of files present in a particular folder in Sharepoint, below is the code to access the file contents of a particular file and save it to local disk having known the file name and path in Sharepoint.

#Specify the URL of the sharepoint file. Remember to change only the the parts of the link that start with "Your"
file_url_shrpt = '/sites/YourSharepointSiteName/Shared%20Documents/YourSharepointFolderName/YourSharepointFileName'

#Load the sharepoint file content to "response" variable
response = File.open_binary(ctx, file_url_shrpt)

#Save the file to your offline path
with open("Your_Offline_File_Path", 'wb') as output_file:  
    output_file.write(response.content)

You can refer to the following links for connecting to SQL server and storing the contents in tables: Connecting to Microsoft SQL server using Python

https://datatofish.com/how-to-connect-python-to-sql-server-using-pyodbc/

这篇关于使用 Python 访问 Microsoft Sharepoint 文件和数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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