简体   繁体   English

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

[英]Get the last modified date/time for a Sharepoint File using python Office 365 API

I need to get the last modified date of a document in the SharePoint using SharePoint rest API for python我需要使用 SharePoint rest API for python 获取 SharePoint 中文档的上次修改日期

Basically I want to get the most recently modified file from a SharePoint site I want to get the last modified date to verify if the current document is the latest基本上我想从 SharePoint 站点获取最近修改的文件我想获取上次修改日期以验证当前文档是否是最新的

I am trying to find if there is any method which can help get the last modified date of documents.我试图找到是否有任何方法可以帮助获取文件的最后修改日期。

I suggest using O365 Python package.我建议使用 O365 Python 包。

https://github.com/O365/python-o365 https://github.com/O365/python-o365

Below example shows how you can print modified time and then download the file.下面的示例显示了如何打印修改时间然后下载文件。 In this example I am downloading excel files.在这个例子中,我正在下载 excel 文件。

Note: I have all my passwords and etc stored in a dictionary within a settings file I created hence why you see me importing that file.注意:我将所有密码等都存储在我创建的设置文件中的字典中,因此您看到我导入该文件的原因。 This is not needed and is a personal preference.这不是必需的,是个人偏好。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 Python office365 API 下载 Sharepoint Docx 文件 - Download Sharepoint Docx file via Python office365 API 使用 Office365-REST-Python-Client 复制 Sharepoint 中的文件 - Copy file in Sharepoint using Office365-REST-Python-Client 如何使用Python获取文件夹中上次修改文件的时间 - How to get the time of the last modified file in a folder using Python 使用 Python 访问 Office365 Sharepoint REST 端点(Python 的 Office365 Sharepoint REST 客户端) - Accessing Office365 Sharepoint REST EndPoints using Python (Office365 Sharepoint REST client for Python) Using Python to access Excel File on Sharepoint using Office365 Python Module - Using Python to access Excel File on Sharepoint using Office365 Python Module 使用 office365 API 将文件上传到 Sharepoint 中的子文件夹 - Upload file to a subfolder in Sharepoint with office365 API 使用 bigquery 表 GET api 获取表的最后修改日期 - get the last modified date of tables using bigquery tables GET api SharePoint 在线:使用 Office365-REST-Python-Client 库获取站点用户 ID - SharePoint online: Get site user id using Office365-REST-Python-Client library GAE Python:如何获取静态文件的最后修改日期 - GAE Python: how get last modified date of static file 如何获取上次在Python中修改文件的时间? - How do I get the time a file was last modified in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM