首页 > 解决方案 > 使用 python Office 365 API 获取 Sharepoint 文件的最后修改日期/时间

问题描述

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

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

我正在尝试查找是否有任何方法可以帮助获取文档的最后修改日期。

标签: pythonpython-3.xoffice365apilast-modified

解决方案


我建议使用 O365 Python 包。

https://github.com/O365/python-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)

推荐阅读