Question
Hi, I am trying to download two excel files from SharePoint where file names are dynamic (ex: 'Longs Data PowerBI - Template_v1.0' or 'Longs Data
Hi,
I am trying to download two excel files from SharePoint where file names are dynamic (ex: 'Longs Data PowerBI - Template_v1.0' or 'Longs Data PowerBI - Template_v1.1') and another file with ('Ore Data PowerBi -Templete_v1.0' or 'Ore Data PowerBi -Templete_v1.1') by last modified date using python. condition is if the file is modified today then only need to download.
find the script below where i can download the file irespective of modifed date.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Validate the domain models.
#
import json
import argparse
import urllib.parse
import os
import requests
import logging
import shutil
import glob
import datetime
import time
# Logging level
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
from office365.sharepoint.caml_query import CamlQuery
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File
from office365.sharepoint.file_creation_information import FileCreationInformation
"""
Register app at https://.sharepoint.com/sites/ /_layouts/15/AppRegNew.aspx
Grant permission at https://.sharepoint.com/sites/ /_layouts/15/AppInv.aspx
Refer to
- https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/register-sharepoint-add-ins
- https://piyushksingh.com/2018/12/26/register-app-in-sharepoint/
"""
_SHAREPOINT_SITE = "https://.sharepoint.com/sites/ "
_CLIENT_ID = " "
_CLIENT_SECRET = " "
def read_folder_and_files_alt(context, list_title):
"""Read a folder example"""
list_obj = context.web.lists.get_by_title(list_title)
qry = CamlQuery.create_all_items_query()
items = list_obj.get_items(qry)
context.load(items)
context.execute_query()
for cur_item in items:
print("File name: {0}".format(cur_item.properties["Title"]))
def read_folder_and_files(context, list_title):
"""Read a folder example"""
#list_obj = context.web.lists.get_by_title(list_title)
folder = context.web.get_folder_by_server_relative_url(app_settings['urlrel'])
ctx.load(folder)
ctx.execute_query()
#print(folder.url)
files = folder.files
ctx.load(files)
ctx.execute_query()
for myfile in files:
if (myfile.properties["Name"].startswith('Longs Data PowerBI - Template_v')):
return myfile.properties["Name"]
print(myfile.properties["Name"])
elif (myfile.properties["Name"].startswith('Ore')):
return myfile.properties["Name"]
print(myfile.properties["Name"])
def download_file(context, sharepoint_file_path, local_file_path):
response = File.open_binary(context, sharepoint_file_path)
with open(local_file_path, "wb") as local_file:
local_file.write(response.content)
if __name__ == '__main__':
ctx_auth = AuthenticationContext(url=_SHAREPOINT_SITE)
if ctx_auth.acquire_token_for_app(client_id=_CLIENT_ID, client_secret=_CLIENT_SECRET):
print("Connected")
app_settings = {'urlrel': '/sites/ / / '}
ctx = ClientContext(_SHAREPOINT_SITE, ctx_auth)
file_name = read_folder_and_files(ctx, 'Documents/Shared Documents')
file_name1 = read_folder_and_files(ctx, 'Documents/Shared Documents')
print(file_name,file_name1)
#forfile in file_name
download_file(ctx, "/sites/ / / /"+file_name, "./"+file_name)
else:
print(ctx_auth.get_last_error())
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started