Skip to main content

Microsoft SharePoint client Python package that uses the requests library

Project description

sharepointlib

Package Description

Microsoft SharePoint client Python package that uses the requests library.

[!IMPORTANT]
This packages uses pydantic~=1.0!

Usage

from a script:

import sharepointlib
import logging
import pandas as pd

logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")

client_id = "123..."
tenant_id = "456..."
client_secret = "xxxx"
sp_domain = "companygroup.sharepoint.com"

sp_site_name = "My Site"
sp_site_id = "companygroup.sharepoint.com,1233124124"
sp_site_drive_id = "b!1234567890"

# Initialize SharePoint client
sharepoint = sharepointlib.SharePoint(client_id=client_id, 
                                      tenant_id=tenant_id, 
                                      client_secret=client_secret, 
                                      sp_domain=sp_domain)
# Gets the site ID for a given site name
response = sharepoint.get_site_info(name=sp_site_name)
if response.status_code == 200:
    print(response.content["id"])
    df = pd.DataFrame([response.content])
    print(df)
# Gets the hostname and site details for a specified site ID
response = sharepoint.get_hostname_info(site_id=sp_site_id)
if response.status_code == 200:
    df = pd.DataFrame([response.content])
    print(df)

Drives:

# Gets a list of the Drive IDs for a given site ID
response = sharepoint.list_drives(site_id=sp_site_id)
if response.status_code == 200:
    df = pd.DataFrame(response.content)
    print(df)
# Gets the folder ID for a specified folder within a drive ID
response = sharepoint.get_dir_info(drive_id=sp_site_drive_id,
                                   path="Sellout/Support")
if response.status_code == 200:
    df = pd.DataFrame([response.content])
    print(df)
# List content (files and folders) of a specific folder
response = sharepoint.list_dir(drive_id=sp_site_drive_id, 
                               path="Sellout/Support")
if response.status_code == 200:
    df = pd.DataFrame(response.content)
    print(df)
# Creates a new folder in a specified drive ID
response = sharepoint.create_dir(drive_id=sp_site_drive_id, 
                                 path="Sellout/Support",
                                 name="Archive")
if response.status_code in (200, 201):
    df = pd.DataFrame([response.content])
    print(df)

response = sharepoint.create_dir(drive_id=sp_site_drive_id, 
                                 path="Sellout/Support",
                                 name="Test")
if response.status_code in (200, 201):
    df = pd.DataFrame([response.content])
    print(df)
# Deletes a folder from a specified drive ID
response = sharepoint.delete_dir(drive_id=sp_site_drive_id, 
                                 path="Sellout/Support/Test")
if response.status_code in (200, 204):
    print("Folder deleted successfully")
# Renames a folder in a specified drive ID
response = sharepoint.rename_folder(drive_id=sp_site_drive_id, 
                                    path="Sellout/Support",
                                    new_name="Old")
if response.status_code == 200:
    df = pd.DataFrame([response.content])
    print(df)
# Retrieves information about a specific file in a drive ID
response = sharepoint.get_file_info(drive_id=sp_site_drive_id, 
                                    filename="Sellout/Support/Sellout.xlsx")
if response.status_code in (200, 202):
    print(response.content.id)
    df = pd.DataFrame([response.content])
    print(df)
# Copy a file from one folder to another within the same drive ID
response = sharepoint.copy_file(drive_id=sp_site_drive_id, 
                                filename="Sellout/Support/Archive/My Book.xlsx",
                                target_path="Sellout/Support/",
                                new_name="My Book Copy.xlsx")
if response.status_code in (200, 202):
    print("File copied successfully")
# Moves a file from one folder to another within the same drive ID
response = sharepoint.move_file(drive_id=sp_site_drive_id, 
                                filename="Sellout/Support/Book1.xlsx", 
                                target_path="Sellout/Support/Archive/",
                                new_name="My Book.xlsx")
if response.status_code == 200:
    df = pd.DataFrame([response.content])
    print(df)
# Deletes a file from a specified drive ID
response = sharepoint.delete_file(drive_id=sp_site_drive_id, 
                                  filename="Sellout/Sellout.xlsx")
if response.status_code in (200, 204):
    print("File deleted successfully")
# Renames a file in a specified drive ID.
response = sharepoint.rename_file(drive_id=sp_site_drive_id, 
                                  filename="Sellout/Support/Archive/Sellout.xlsx", 
                                  new_name="Sellout_New_Name.xlsx")
if response.status_code == 200:
    df = pd.DataFrame([response.content])
    print(df)
# Downloads a file from a specified remote path in a drive ID to a local path
# Examples for local_path (databricks):
#   local_path=r"/Workspace/Users/admin@admin.com/Sellout.xlsm"
#   local_path=r"/Volumes/lakehouse/sadp/Sellout.xlsm"
response = sharepoint.download_file(drive_id=sp_site_drive_id, 
                                    remote_path=r"Sellout/Support/Sellout.xlsx",
                                    local_path=r"C:\Users\admin\Downloads\Sellout.xlsx")
if response.status_code == 200:
    print("File downloaded successfully")
# Downloads all files from a specified remote path in a drive ID to a local path
# Examples for local_path (databricks):
#   local_path=r"/Workspace/Users/admin@admin.com/"
#   local_path=r"/Volumes/lakehouse/sadp/"
response = sharepoint.download_all_files(drive_id=sp_site_drive_id,
                                         remote_path=r"Sellout/Support",
                                         local_path=r"C:\Users\admin\Downloads")
if response.status_code == 200:
    df = pd.DataFrame(response.content)
    display(df)  # print(df)
# Downloads an Excel file from SharePoint directly into memory and loads it into a Pandas DataFrame
from io import BytesIO

response = sharepoint.download_file_to_memory(drive_id=sp_site_drive_id,
                                              remote_path="Sellout/Support/Sellout.xlsx")

if response.status_code == 200 and response.content is not None:
    excel_data = BytesIO(response.content)
    df = pd.read_excel(excel_data)
    print(df)
# Uploads a file to a specified remote path in a SharePoint drive ID
response = sharepoint.upload_file(drive_id=sp_site_drive_id, 
                                  local_path=r"C:\Users\admin\Downloads\Sellout.xlsx",
                                  remote_path=r"Sellout/Support/Archive/Sellout.xlsx")
if response.status_code in (200, 201):
    df = pd.DataFrame([response.content])
    print(df)

Installation

Install python and pip if you have not already.

Then run:

pip install pip --upgrade

For production:

pip install sharepointlib

This will install the package and all of it's python dependencies.

If you want to install the project for development:

git clone https://github.com/aghuttun/sharepointlib.git
cd sharepointlib
pip install -e ".[dev]"

To test the development package: Testing

License

BSD License (see license file)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sharepointlib-0.0.20.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sharepointlib-0.0.20-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file sharepointlib-0.0.20.tar.gz.

File metadata

  • Download URL: sharepointlib-0.0.20.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sharepointlib-0.0.20.tar.gz
Algorithm Hash digest
SHA256 26f5ecb82955c34be078e05a8ea18b6a789d1135f2f78464e96ea06abeb09bbd
MD5 7d4acace615d5902eb41fa2fd012d13b
BLAKE2b-256 7850a4612db24b5b08fb5254fc680c26f4e1512429ad214f4503e96e83e49966

See more details on using hashes here.

File details

Details for the file sharepointlib-0.0.20-py3-none-any.whl.

File metadata

  • Download URL: sharepointlib-0.0.20-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sharepointlib-0.0.20-py3-none-any.whl
Algorithm Hash digest
SHA256 b52ffa6b004b495fc1e7a80f06ef72353227ee5af45f555143b07425f1820128
MD5 0a70c7575a2f9b8e0686d874c883a99f
BLAKE2b-256 20f9c0e7a67f83e803c03ce6b3790d65b6c447a31f936aa20a23c71f54d6d441

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page