使用python Office 365 API获取Sharepoint文件的最后修改日期时间。

问题描述 投票:1回答:1

我需要使用python的SharePoint rest API获取SharePoint中文档的最后修改日期。

基本上,我想从SharePoint站点获取最近修改的文件,我想获取最后修改的日期,以验证当前文件是否是最新的。

我试图寻找是否有任何方法可以帮助获得文件的最后修改日期。

python python-3.x office365api last-modified
1个回答
0
投票

我建议使用O365 Python包。

https:/github.comO365python-o365。

下面的例子展示了如何打印修改时间,然后下载文件。在这个例子中,我下载的是excel文件。

注意:我把我所有的密码等存储在我创建的设置文件中的字典中,因此你看到我导入该文件的原因。这是不需要的,而且是个人喜好。

from O365 import Account
from settings import settings

save_path = "\path\to\save\file\\"

# user log in information using client credentials and client id for Microsoft Graph API

credentials = (settings['client_credentials']['client_id'],
               settings['client_credentials']['client_secret'])

# the default protocol will be Microsoft Graph
account = Account(credentials, auth_flow_type='credentials',
                  tenant_id=settings['client_credentials'
                  ]['client_tenant_id'])
if account.authenticate():
    print('Authenticated!')

# initiate share_point account

share_point = account.sharepoint()

# change this site id to whatever your SharePoint site id is

site_id = 'your site id'

# get the site

site = share_point.get_site(site_id)

# create drive item

my_drive = site.get_default_document_library()

# navigate to root folder of the drive

root_folder = my_drive.get_root_folder()

# this will get all the folders under your root folder

child_folders = root_folder.get_child_folders()

for folder in child_folders:
    if folder.name == 'Your SharePoint folder name':
        for item in folder.get_items():
            try:
                if item.name.lower().endswith(('.xls', '.xlsx')):
                    # print last modified date
                    print(item.modified)
                    # download files
                    item.download(save_path)
            except Exception, e:
                print('File not found!')
                print(e)
© www.soinside.com 2019 - 2024. All rights reserved.