How to parse a list of files from Sharepoint
Your output status suggests that its a success response. So you can now use that response
in r
and parse it to json
and dump the json
response to another file.
The code below taken from here illustrates exactly how you can get details on the files.
Get file information
data = r.json()
file = open("sharepoint.json", "w")
file.write(json.dumps(data, indent=4))
print("json file has been generated")
Download file
r = s.getfile("https:// example.sharepoint.com/GroupSites/HR/_api/web\
/GetFileByServerRelativeUrl('/GroupSites/HR/Shared Documents/Team.xlsx')/$value"\
, filename = 'team.xlsx')
print("File Downloaded")
Download all files in a Sharepoint folder using Python?
You need to get a list of files
# Get list of all files and folders in library
files = s.get("{}/_api/web/lists/GetByTitle('{}')/items?$select=FileLeafRef,FileRef"
.format(site, library)).json()["d"]["results"]
for the complete solution, check this:
https://github.com/JonathanHolvey/sharepy/issues/5
Accessing Microsoft Sharepoint files and data using Python
Here's the starter code for connecting to share point through Python and accessing the list of files, folders and individual file contents of Sharepoint as well. You can build on top of this to suit your needs.
Please note that this method works for public Sharepoint sites that are accessible through internet. For Organisation restricted Sharepoint sites that are hosted on a Company's intranet, I haven't tested this code out.
You will have to modify the link to the Sharepoint file a bit since you cannot directly access a Sharepoint file in Python using the URL address of that file which is copied from the web browser.
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
####inputs########
# This will be the URL that points to your sharepoint site.
# Make sure you change only the parts of the link that start with "Your"
url_shrpt = 'https://YourOrganisation.sharepoint.com/sites/YourSharepointSiteName'
username_shrpt = 'YourUsername'
password_shrpt = 'YourPassword'
folder_url_shrpt = '/sites/YourSharepointSiteName/Shared%20Documents/YourSharepointFolderName/'
#######################
###Authentication###For authenticating into your sharepoint site###
ctx_auth = AuthenticationContext(url_shrpt)
if ctx_auth.acquire_token_for_user(username_shrpt, password_shrpt):
ctx = ClientContext(url_shrpt, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print('Authenticated into sharepoint as: ',web.properties['Title'])
else:
print(ctx_auth.get_last_error())
############################
####Function for extracting the file names of a folder in sharepoint###
###If you want to extract the folder names instead of file names, you have to change "sub_folders = folder.files" to "sub_folders = folder.folders" in the below function
global print_folder_contents
def print_folder_contents(ctx, folder_url):
try:
folder = ctx.web.get_folder_by_server_relative_url(folder_url)
fold_names = []
sub_folders = folder.files #Replace files with folders for getting list of folders
ctx.load(sub_folders)
ctx.execute_query()
for s_folder in sub_folders:
fold_names.append(s_folder.properties["Name"])
return fold_names
except Exception as e:
print('Problem printing out library contents: ', e)
######################################################
# Call the function by giving your folder URL as input
filelist_shrpt=print_folder_contents(ctx,folder_url_shrpt)
#Print the list of files present in the folder
print(filelist_shrpt)
Now that we are able to retrieve and print the list of files present in a particular folder in Sharepoint, below is the code to access the file contents of a particular file and save it to local disk having known the file name and path in Sharepoint.
#Specify the URL of the sharepoint file. Remember to change only the the parts of the link that start with "Your"
file_url_shrpt = '/sites/YourSharepointSiteName/Shared%20Documents/YourSharepointFolderName/YourSharepointFileName'
#Load the sharepoint file content to "response" variable
response = File.open_binary(ctx, file_url_shrpt)
#Save the file to your offline path
with open("Your_Offline_File_Path", 'wb') as output_file:
output_file.write(response.content)
You can refer to the following links for connecting to SQL server and storing the contents in tables:
Connecting to Microsoft SQL server using Python
https://datatofish.com/how-to-connect-python-to-sql-server-using-pyodbc/
Related Topics
Get Discord User Id from Username
How to Count the Number of Messages
Best Way to Get the Max Value in a Spark Dataframe Column
How to Allocate Array With Shape and Data Type
How to Make a Tkinter Label Background Transparent
Macos: How to Downgrade Homebrew Python
How to Clear/Delete the Contents of a Tkinter Text Widget
Python: How to Check If Cell in CSV File Is Empty
Python Selenium - Element Is Not Currently Interactable and May Not Be Manipulated
How to Change Python Version in Anaconda Spyder
Python: Draw Line Between Two Coordinates in a Matrix
How to Replace Values At Specific Indexes of a Python List
Split/Explode a Column of Dictionaries into Separate Columns With Pandas
Drop Rows With All Zeros in Pandas Data Frame
How to Extract Hours and Minutes from a Datetime.Datetime Object