class FolderTestCase(unittest.TestCase): def setUp(self): if TEST_SETTINGS["version"] in ["2014", "2016", "2019", "365"]: version = Version.v2016 else: version = Version.v2007 authcookie = Office365(TEST_SETTINGS["server_url"], username=TEST_SETTINGS["username"], password=TEST_PASSWORD).GetCookies() self.site = Site(TEST_SETTINGS["site_url"], version=version, authcookie=authcookie) def tearDown(self): self.site._session.close() def test_folder(self): print("Testing Folder") self.folder = self.site.Folder(TEST_SETTINGS["test_folder"]) self.folder.upload_file("Hello", "new.txt") self.assertEqual(self.folder.get_file("new.txt"), b"Hello") self.folder.delete_file("new.txt") print("Testing Folder and files with apostrophes") self.folder = self.site.Folder(TEST_SETTINGS["test_folder_apostrophe"]) self.folder.upload_file("Hello", "new'.txt") self.assertEqual(self.folder.get_file("new'.txt"), b"Hello") self.folder.delete_file("new'.txt")
def upload_file_sharepoint(self, source_path, sink_path, filename, sharepoint_site): """This fucntion will upload a file from the source path to Sharepoint. Parameters: source_path = r'/full_sink_path/' sink_path = r'Shared Documents/Shared/<Location>' filename = 'filename.ext' sharepoint_site = 'https://xxx.sharepoint.com/sites/<site_name>' """ site = Site(sharepoint_site, version=Version.v2016, authcookie=self.authcookie) full_source_path = os.path.join(source_path, filename) full_sink_path = os.path.join(sink_path, filename) print(full_source_path) print(full_sink_path) folder = site.Folder(sink_path) with open(full_source_path, mode="rb") as file: filecontent = file.read() for attempt in range(0, 3): try: folder.upload_file(filecontent, full_sink_path) print("Attempt #No:", attempt) except Exception as e: if attempt < 2: print("Trying again!") continue print("Error", e) raise e break
class FolderTestCase(unittest.TestCase): def setUp(self): with open("test_server.json") as f: self.server = json.load(f) if self.server["version"] in ["2014", "2016", "2019", "365"]: version = Version.v2016 else: version = Version.v2007 authcookie = Office365( self.server["server_url"], username=self.server["username"], password=os.environ.get('TEST_PASSWORD')).GetCookies() self.site = Site(self.server["site_url"], version=version, authcookie=authcookie) def tearDown(self): self.site._session.close() def test_folder(self): print("Testing Folder") self.folder = self.site.Folder(self.server["test_folder"]) self.folder.upload_file("Hello", "new.txt") self.assertEqual(self.folder.read_txt_file("new.txt"), "Hello") self.folder.delete_file("new.txt")
def download_file_sharepoint(self, source_path, sink_path, filename, sharepoint_site): """This fucntion will download a file from the Sharepoint to specified sink path. Parameters: source_path = r'Shared Documents/Shared/<Location>' sink_path = r'/full_sink_path/' filename = 'filename.ext' sharepoint_site = 'https://xxx.sharepoint.com/sites/<site_name>' """ site = Site(sharepoint_site, version=Version.v2016, authcookie=self.authcookie) full_source_path = os.path.join(source_path, filename) full_sink_path = os.path.join(sink_path, filename) print(full_source_path) print(full_sink_path) folder = site.Folder(source_path) for attempt in range(0, 3): try: output_file = open(full_sink_path, 'wb') input_file = folder.get_file(filename) binary_format = bytearray(input_file) output_file.write(binary_format) output_file.close() print("Attempt #No: ", attempt) print("Downlowded file size is ", round(os.path.getsize(full_sink_path) / 1024, 2), " KB") except Exception as e: if (attempt < 2): print("Try again!") continue print("Error", e) raise e break
def o365_login(tenant, username, password, site_o365, shared_folder): authcookie = Office365( tenant, username=base64.b64decode(username).decode('utf-8'), password=base64.b64decode(password).decode('utf-8')).GetCookies() site = Site(site_o365, version=Version.v365, authcookie=authcookie) folder_shared_o365 = site.Folder(shared_folder) print(time_now(), f' Login to O365 to {site_o365} is successful!') return folder_shared_o365
def download(self) -> list: dl_files = [] # parse url parsed = urlparse(self.site) scheme = 'https' if parsed.scheme == '' else parsed.scheme version = 365 if self.version == '365': sp_version = Version.v365 else: sp_version = Version.v2007 try: if sp_version == Version.v365: authcookie = Office365(f'{scheme}://{parsed.netloc}', username=self.username, password=self.password).GetCookies() else: cred = HttpNtlmAuth(self.username, self.password) except: raise Exception( f'Unable to authenticate using supplied user name and password.' ) else: self.display_info('Sucessfully authnticated') try: if sp_version == Version.v365: site = Site(self.site, version=sp_version, authcookie=authcookie) else: site = Site(self.site, version=sp_version, auth=cred) except: raise Exception(f'{self.site} is not a valid site') else: self.display_info(f'Sucessfully accessed site {self.site}') # build path to document folder doc_path = os.path.join(parsed.path, self.docs) try: folder = site.Folder(doc_path) for f in folder.files: fname = f['Name'] if fnmatch.fnmatch(fname, self.filespec): dest = os.path.join(self.savepath, fname) with open(dest, mode='wb') as file: file.write(folder.get_file(fname)) dl_files.append(dest) except: raise Exception(f'Unable to download files from {self.docs}') return dl_files
def list_item_sharepoint(self, source_path, sharepoint_site): """This function will list all files in a given source path of Sharepoint. Parameters: source_path = r'Shared Documents/Shared/<Location>' sharepoint_site = 'https://xxx.sharepoint.com/sites/<site_name>' """ site = Site(sharepoint_site, version=Version.v2016, authcookie=self.authcookie) folder_source = site.Folder(source_path) #Get object for files in a directory files_item = folder_source.files items_df = pd.DataFrame() for i in files_item: items_df = items_df.append(pd.DataFrame.from_dict([i])) if len(items_df) > 0: # Subset the columns subset_cols = [ "Length", "LinkingUrl", "MajorVersion", "MinorVersion", "Name", "TimeCreated", "TimeLastModified", ] items_df = items_df[subset_cols] # Parse url to remove everything after ? mark items_df["LinkingUrl"] = [ i.split("?")[0] for i in items_df["LinkingUrl"] ] # convert bytes to KB items_df["Length"] = [ round(int(i) / 1000, 2) for i in items_df["Length"] ] # sort based on file names items_df.sort_values("Name", inplace=True) # rename to more friendlier names items_df.columns = [ "FileSize", "FullFileUrl", "FileVersion", "MinorVersion", "FileName", "TimeCreated", "TimeLastModified", ] return items_df else: print(f"No files in {source_path} directory") return pd.DataFrame()
def get_data(team_slug, folder_name, file_name): authcookie = Office365('https://devpatrol.sharepoint.com', username=SP_USERNAME, password=SP_PASSWORD).GetCookies() site = Site('https://devpatrol.sharepoint.com/sites/' + team_slug, version=Version.v365, authcookie=authcookie, verify_ssl=False) folder = site.Folder('Shared Documents/' + folder_name) response = folder.get_file(file_name) return response
class SharepointHelper: def __init__(self, url, folder_path, username, password): self.folder_path = folder_path authcookie = Office365(url, username=username, password=password).GetCookies() self.site = Site(site_url=url, authcookie=authcookie) def create_folder(self, folder_name): # Do some crazy folder creation full_path = f"{self.folder_path}/{folder_name}" result = self.site.Folder(full_path) return True, result
x['Achieved result'] = x['Achieved result'].fillna(0) x['Achieved result'] = x['Achieved result'].replace([''], 0) ##GMS code x['GMS Code'] = x[['Indicator code', 'GMS Code']].apply(lambda x: fix_gms(*x), axis=1) #Export to Excel file output_data = pd.concat([output_data, x]) output_data.to_excel('Output.xlsx', index=False) #upload to sharepoint online #load libraries needed for sharepoint upload from shareplum import Site from shareplum import Office365 from shareplum.site import Version import pandas as pd #setup access to sharepoint online authcookie = Office365('https://urlname.sharepoint.com', username='******', password='******').GetCookies() site = Site('https://urlname.sharepoint.com/sites/', version=Version.v2016, authcookie=authcookie) folder = site.Folder('Shared Documents/Folder/') with open('Output.xlsx', mode='rb') as file: fileContent = file.read() folder.upload_file(fileContent, "Output.xlsx")
from shareplum import Site from shareplum import Office365 from shareplum.site import Version import json import os wd = os.getcwd() json_path = os.path.join(wd, "src", "authentification", "sharepoint_authentification.json") with open(json_path, "r") as read_file: sp_auth = json.load(read_file) authcookie = Office365(sp_auth["url"], username=sp_auth["username"], password=sp_auth['password']).GetCookies() site = Site(sp_auth["url_site"], version=Version.v2016, authcookie=authcookie) folder = site.Folder('Github_upload') csv_path = os.path.join(wd, "src", "data", "51427.csv") with open(csv_path, 'r', encoding="Latin-1") as file: data = file.read() folder.upload_file(data, "51427.csv")
from shareplum import Site from shareplum import Office365 from shareplum.site import Version from config import config # get data from configuration username = config['sp_user'] password = config['sp_password'] site_name = config['sp_site_name'] base_path = config['sp_base_path'] doc_library = config['sp_doc_library'] authcookie = Office365(base_path, username=username, password=password).GetCookies() site = Site('https://my.sharepoint.com/sites/sbdev', version=Version.v365, authcookie=authcookie) folder = site.Folder('Shared Documents/This Folder') folder.download_file('source.txt', 'destination.txt')
from shareplum import Site from shareplum import Office365 from shareplum.site import Version import os import sys authcookie = Office365('https://yourcompany.atsharepoint.com', username='******', password='******').GetCookies() site = Site('https://yourcompany.atsharepoint.com/sites/Your-Sites', version=Version.v2019, authcookie=authcookie) # set the version if necessary for root, subdirs, files in os.walk('/path/to/root/dir'): dest_folder = root.split('/path/to/root')[ 1] # this will get directory after /path/to/dir and its subdir folder = site.Folder( 'Shared Document/your_dir_ifany/%s' % dest_folder ) # this set destination directory or will create directory in sharepoint if doesn't exist for fname in files: fullfname = os.path.join(root, fname) # get the full path of source file with open(fullfname, mode='rb') as file: # read the file in binary fileContent = file.read() folder.upload_file(fileContent, fname) # upload the file
import os from config import config from shareplum import Site from shareplum import Office365 from shareplum.site import Version # get data from configuration username = config['sp_user'] password = config['sp_password'] authcookie = Office365('https://xxx.sharepoint.com', username=username, password=password).GetCookies() site = Site('https://xxx.sharepoint.com/sites/testprivate',version=Version.v365, authcookie=authcookie) spfolder = site.Folder('Shared Documents/allf') for root, dirs, files in os.walk(r"D:\d365\SDK\SampleCode\JS\SOAPForJScript\SOAPForJScript"): for file in files: filepath = os.path.join(root, file) print(filepath) # perform the actual upload with open(filepath, 'rb+') as file_input: try: spfolder.upload_file(file_input, file) except Exception as err: print("Some error occurred: " + str(err))