Microsoft 365 & Microsoft Graph Library for Python
npx @tessl/cli install tessl/pypi-office365-rest-python-client@2.6.0A comprehensive Python library for interacting with Microsoft 365 and Microsoft Graph APIs, enabling developers to build applications that integrate with SharePoint, Outlook, OneDrive, Teams, OneNote, and Planner services. It offers a unified client interface for both legacy SharePoint REST APIs and modern Microsoft Graph endpoints, with support for various authentication methods including Azure AD, NTLM, and certificate-based authentication.
pip install Office365-REST-Python-Clientfrom office365.graph_client import GraphClient
from office365.sharepoint.client_context import ClientContext
from office365.azure_env import AzureEnvironmentFor authentication:
from office365.runtime.auth.user_credential import UserCredential
from office365.runtime.auth.client_credential import ClientCredential
from office365.runtime.auth.authentication_context import AuthenticationContextFor specific services:
from office365.directory.users.user import User
from office365.sharepoint.files.file import File
from office365.sharepoint.webs.context_web_information import ContextWebInformation
from office365.sharepoint.request_user_context import RequestUserContext
from office365.onedrive.drives.drive import Drive
from office365.teams.team import Teamfrom office365.graph_client import GraphClient
# Authenticate with client credentials
client = GraphClient(tenant="your-tenant-id").with_client_secret(
client_id="your-client-id",
client_secret="your-client-secret"
)
# Get current user information
me = client.me.get().execute_query()
print(f"Hello {me.display_name}")
# List all users
users = client.users.get().top(10).execute_query()
for user in users:
print(user.display_name, user.mail)
# Access OneDrive files
drive = client.me.drive.get().execute_query()
items = drive.root.children.get().execute_query()
for item in items:
print(item.name, item.web_url)from office365.sharepoint.client_context import ClientContext
# Authenticate to SharePoint site
ctx = ClientContext("https://tenant.sharepoint.com/sites/sitename").with_client_credentials(
client_id="your-client-id",
client_secret="your-client-secret"
)
# Get site information
web = ctx.web.get().execute_query()
print(f"Site title: {web.title}")
# Work with document libraries
docs_lib = ctx.web.lists.get_by_title("Documents")
items = docs_lib.items.get().execute_query()
for item in items:
print(item.properties["Title"])
# Upload a file
with open("local-file.txt", "rb") as file_content:
target_file = docs_lib.root_folder.upload_file("remote-file.txt", file_content)
ctx.execute_query()
print(f"File uploaded: {target_file.serverRelativeUrl}")The library follows a dual-client architecture to support both modern and legacy Microsoft APIs:
Both clients provide fluent interfaces with method chaining, batch operations, and comprehensive error handling. The entity framework ensures consistent patterns across all API surfaces while the runtime layer handles authentication, retry logic, and protocol-specific requirements.
Comprehensive authentication support for enterprise environments including client credentials, certificate-based authentication, interactive flows, and legacy authentication methods for SharePoint.
class GraphClient:
def __init__(self, tenant: str = None, scopes: List[str] = None, token_cache=None, environment=None): ...
def with_client_secret(self, client_id: str, client_secret: str) -> 'GraphClient': ...
def with_certificate(self, client_id: str, thumbprint: str, private_key: str) -> 'GraphClient': ...
def with_token_interactive(self, client_id: str, username: str = None) -> 'GraphClient': ...
def with_username_and_password(self, client_id: str, username: str, password: str) -> 'GraphClient': ...
def with_device_flow(self, tenant: str, client_id: str, scopes: List[str] = None) -> 'GraphClient': ...
def with_access_token(self, token_func: Callable[[], dict]) -> 'GraphClient': ...
class ClientContext:
def with_client_credentials(self, client_id: str, client_secret: str) -> 'ClientContext': ...
def with_client_certificate(self, tenant: str, client_id: str, thumbprint: str, cert_path: str = None, private_key: str = None, scopes: List[str] = None, passphrase: str = None) -> 'ClientContext': ...
def with_interactive(self, tenant: str, client_id: str, scopes: List[str] = None) -> 'ClientContext': ...
def with_user_credentials(self, username: str, password: str) -> 'ClientContext': ...
def with_device_flow(self, tenant: str, client_id: str, scopes: List[str] = None) -> 'ClientContext': ...
def with_access_token(self, token_func: Callable[[], dict]) -> 'ClientContext': ...
def with_credentials(self, credentials: Union[UserCredential, ClientCredential]) -> 'ClientContext': ...Complete Azure Active Directory management including users, groups, applications, devices, and organizational relationships through the Microsoft Graph API.
class GraphClient:
@property
def users(self) -> UserCollection: ...
@property
def groups(self) -> GroupCollection: ...
@property
def applications(self) -> ApplicationCollection: ...
@property
def devices(self) -> DeviceCollection: ...
@property
def me(self) -> User: ...
class User:
display_name: str
mail: str
user_principal_name: str
def get(self) -> 'User': ...
def update(self) -> 'User': ...
def delete_object(self) -> 'User': ...OneDrive storage access with comprehensive file operations including upload, download, sharing, and metadata management through Microsoft Graph API.
class GraphClient:
@property
def drives(self) -> DriveCollection: ...
@property
def sites(self) -> SiteCollection: ...
class Drive:
@property
def root(self) -> DriveItem: ...
def get_by_path(self, path: str) -> DriveItem: ...
class DriveItem:
name: str
web_url: str
@property
def children(self) -> DriveItemCollection: ...
def upload(self, name: str, content: bytes) -> 'DriveItem': ...
def download(self) -> bytes: ...Complete Microsoft Teams functionality including teams, channels, chats, messages, and collaborative features through Microsoft Graph API.
class GraphClient:
@property
def teams(self) -> TeamCollection: ...
@property
def chats(self) -> ChatCollection: ...
class Team:
display_name: str
description: str
@property
def channels(self) -> ChannelCollection: ...
@property
def members(self) -> ConversationMemberCollection: ...
class Chat:
topic: str
chat_type: str
@property
def messages(self) -> ChatMessageCollection: ...Comprehensive SharePoint functionality including sites, lists, libraries, files, folders, and content management through SharePoint REST API.
class ClientContext:
@property
def web(self) -> Web: ...
@property
def site(self) -> Site: ...
@property
def context_info(self) -> ContextWebInformation: ...
@property
def authentication_context(self) -> AuthenticationContext: ...
@property
def base_url(self) -> str: ...
@property
def me(self) -> RequestUserContext: ...
@property
def lists(self) -> ListCollection: ...
class Web:
title: str
url: str
@property
def lists(self) -> ListCollection: ...
@property
def folders(self) -> FolderCollection: ...
class List:
title: str
@property
def items(self) -> ListItemCollection: ...
@property
def root_folder(self) -> Folder: ...Outlook integration for email, calendar, contacts, and mailbox management through Microsoft Graph API with comprehensive messaging capabilities.
class GraphClient:
@property
def me(self) -> User: ...
class User:
@property
def messages(self) -> MessageCollection: ...
@property
def mail_folders(self) -> MailFolderCollection: ...
@property
def events(self) -> EventCollection: ...
@property
def calendar(self) -> Calendar: ...
class Message:
subject: str
body: ItemBody
from_: Recipient
to_recipients: List[Recipient]
def send(self) -> None: ...class Entity:
"""Base class for all API entities with common operations."""
def get(self) -> 'Entity': ...
def update(self) -> 'Entity': ...
def delete(self) -> None: ...
def execute_query(self) -> 'Entity': ...
class EntityCollection:
"""Base class for collections with query and pagination support."""
def get(self) -> 'EntityCollection': ...
def filter(self, expression: str) -> 'EntityCollection': ...
def select(self, properties: List[str]) -> 'EntityCollection': ...
def top(self, count: int) -> 'EntityCollection': ...
def skip(self, count: int) -> 'EntityCollection': ...
def order_by(self, property_name: str, ascending: bool = True) -> 'EntityCollection': ...
class ClientResult:
"""Container for API response data."""
value: Any
class RequestOptions:
"""Configuration options for HTTP requests."""
headers: Dict[str, str]
timeout: int
class AzureEnvironment:
"""Azure cloud environment configuration."""
Global: 'AzureEnvironment'
China: 'AzureEnvironment'
USGovernment: 'AzureEnvironment'
Germany: 'AzureEnvironment'
class ContextWebInformation:
"""SharePoint site context information."""
form_digest_value: str
lib_resource: str
supported_schema_versions: List[str]
class AuthenticationContext:
"""Authentication context for SharePoint requests."""
url: str
environment: str
class RequestUserContext:
"""Current user context for SharePoint."""
login_name: str
title: str
email: str
class UserCredential:
"""User credential for authentication."""
def __init__(self, username: str, password: str): ...
class ClientCredential:
"""Client credential for authentication."""
def __init__(self, client_id: str, client_secret: str): ...