[英]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.