Instantly share code, notes, and snippets.
Created
September 6, 2022 14:14
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save nuriozbey/18f1bbe099680416d357cb370876d820 to your computer and use it in GitHub Desktop.
MS Sharepoint File Operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Client Id: ******************************************* | |
Client Secret: ******************************************= | |
Title: AccessForFileUpload | |
App Domain: www.nuriozbey.com | |
Redirect URI: https://nuriozbey.sharepoint.com/ | |
""" | |
import os | |
from office365.runtime.auth.client_credential import ClientCredential | |
from office365.runtime.client_request_exception import ClientRequestException | |
from office365.sharepoint.client_context import ClientContext | |
from office365.sharepoint.files.file import File | |
import pandas as pd | |
from io import BytesIO | |
import json | |
#relative_url = "/sites/<my_site/Shared Documents/<folder>/<sub_folder>" | |
""" | |
Example Usage: | |
from sharepointops import * | |
import pandas as pd | |
df = pd.DataFrame({'num_legs': [2, 4, 8, 0], | |
'num_wings': [2, 0, 0, 0], | |
'num_specimen_seen': [10, 2, 1, 8]}, | |
index=['falcon', 'dog', 'spider', 'fish']) | |
ctx = connect_sharepoint() | |
dir = "/teams/<channel>/Shared Documents/General/<site_folder>" | |
file_name = "sample_df.xlsx" | |
cloud_path = upload_file(ctx, dir, file_name, df) | |
print(cloud_path) | |
""" | |
def connect_sharepoint(cwd= os.getcwd()): # provide json contained folder | |
with open(os.path.join(cwd,"sharepoint_credentials.json")) as file: | |
credentials = json.load(file) | |
sp_site = 'https://<org>.sharepoint.com/sites/<my_site>/' # you should enter correct | |
client_credentials = ClientCredential(credentials['client_id'], credentials['client_secret']) | |
ctx = ClientContext(sp_site).with_credentials(client_credentials) | |
return ctx | |
""" | |
Usage: | |
folder_url = "/teams/<channel>/Shared Documents" | |
list_folders(ctx = ctx, relative_url = folder_url) | |
""" | |
def list_folders(ctx,relative_url): | |
libraryRoot = ctx.web.get_folder_by_server_relative_path(relative_url) | |
ctx.load(libraryRoot) | |
ctx.execute_query() | |
#if you want to get the folders within <sub_folder> | |
folders = libraryRoot.folders | |
ctx.load(folders) | |
ctx.execute_query() | |
url_list = [] | |
for myfolder in folders: | |
print("Folder name: {0}".format(myfolder.properties["ServerRelativeUrl"])) | |
url_list.append(myfolder.properties["ServerRelativeUrl"]) | |
return url_list | |
""" | |
Usage: | |
folder_url = "/teams/<channel>/Shared Documents/General/DummyFolder" | |
list_files(ctx = ctx, relative_url = folder_url) | |
""" | |
def list_files(ctx,relative_url): | |
libraryRoot = ctx.web.get_folder_by_server_relative_path(relative_url) | |
ctx.load(libraryRoot) | |
ctx.execute_query() | |
files = libraryRoot.files | |
ctx.load(files) | |
ctx.execute_query() | |
url_list = [] | |
for file in files: | |
print("Folder name: {0}".format(file.properties["ServerRelativeUrl"])) | |
url_list.append(file.properties["ServerRelativeUrl"]) | |
return url_list | |
""" | |
Usage: | |
old_file_dir = '/teams/<channel>/Shared Documents/General/DummyFolder/dummy2.txt' | |
new_file_dir = '/teams/<channel>/Shared Documents/General/dummy_3.txt' | |
move_file(ctx=ctx, old_file_dir=old_file_dir, new_file_dir=new_file_dir) | |
""" | |
def move_file(ctx, old_file_dir, new_file_dir): | |
source_file = ctx.web.get_file_by_server_relative_url(old_file_dir) | |
source_file.moveto(new_relative_url=new_file_dir, flag=1) | |
ctx.execute_query() | |
""" | |
# Usage: | |
dir = '/teams/<channel>/Shared Documents/General/DummyFolder/' | |
old_name = 'dummy2_n.txt' | |
new_name = "dummy2.txt" | |
rename_file(ctx=ctx, dir=dir, old_name=old_name,new_name=new_name) | |
""" | |
def rename_file(ctx, dir, old_name, new_name): | |
old_url = os.path.join(dir,old_name) | |
source_file = ctx.web.get_file_by_server_relative_url(old_url) | |
new_url = os.path.join(dir,new_name) | |
source_file.moveto(new_relative_url=new_url, flag=1) | |
ctx.execute_query() | |
""" | |
# Usage: | |
file_relative_url = '/teams/<channel>/Shared Documents/General/DummyFolder/dummy.txt' | |
delete_file(ctx = ctx , file_relative_url = file_relative_url) | |
""" | |
def delete_file(ctx, file_relative_url): | |
file_to_delete = ctx.web.get_file_by_server_relative_url(file_relative_url) | |
file_to_delete.delete_object() | |
ctx.execute_query() | |
""" | |
# Usage: | |
dir = '/teams/<channel>/Shared Documents/General/DummyFolder/' | |
file_name = 'dummy6.txt' | |
local_file_dir= 'E:\dummy_local.txt' | |
upload_file(ctx = ctx , file_relative_url = file_relative_url) | |
""" | |
def upload_file(ctx,dir,file_name,local_file_dir): | |
with open(local_file_dir, 'rb') as content_file: | |
file = content_file.read() | |
target_folder = ctx.web.get_folder_by_server_relative_url(dir) | |
target_file = target_folder.upload_file(file_name, file).execute_query() | |
print("File has been uploaded to url: {0}".format(target_file.serverRelativeUrl)) | |
""" | |
# Usage: | |
df = pd.DataFrame({'num_l': [2, 4, 8, 0], | |
'num_w': [2, 0, 0, 0], | |
'num_s': [10, 2, 1, 8]}, | |
index=['falcon9', 'falcon1', 'python', 'anaconda']) | |
dir = "/teams/<channel>/Shared Documents/General/DummyFolder/" | |
file_name = "dummy_dataframe.xlsx" | |
cloud_path = upload_dataframe(ctx, dir, file_name, df) | |
""" | |
def upload_dataframe(ctx, dir, file_name, dataframe): | |
#name = "sample2.xlsx" | |
bio = BytesIO() | |
# Pass engine explicitly, as there is no file path to infer from. | |
writer = pd.ExcelWriter(bio, engine='openpyxl') | |
dataframe.to_excel(writer) | |
writer.save() | |
bio.seek(0) | |
file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(file_name, bio).execute_query() | |
print("Dataframe has been uploaded to url: {0}".format(file.serverRelativeUrl)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment