CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-exchangelib

Client for Microsoft Exchange Web Services (EWS) providing Django-style ORM interface for Exchange mailboxes.

Pending
Overview
Eval results
Files

search.mddocs/

Search and Filtering

Advanced search and filtering capabilities using Django-style query syntax. Supports complex queries across all Exchange item types with efficient server-side filtering.

Capabilities

Query Objects

class Q:
    def __init__(self, **kwargs):
        """Create a query object for filtering."""
    
    def __and__(self, other):
        """Combine queries with AND logic."""
    
    def __or__(self, other):
        """Combine queries with OR logic."""
    
    def __invert__(self):
        """Negate the query with NOT logic."""

Filtering Methods

class QuerySet:
    def filter(self, *args, **kwargs):
        """Filter items by criteria."""
    
    def exclude(self, *args, **kwargs):
        """Exclude items matching criteria."""
    
    def order_by(self, *fields):
        """Order results by specified fields."""
    
    def reverse(self):
        """Reverse the order of results."""
    
    def distinct(self):
        """Return distinct results."""
    
    def count(self):
        """Count matching items."""
    
    def exists(self):
        """Check if any items match."""

Search Operators

# Field lookups (Django-style)
field__exact         # Exact match
field__iexact        # Case-insensitive exact match
field__contains      # Contains substring
field__icontains     # Case-insensitive contains
field__startswith    # Starts with
field__istartswith   # Case-insensitive starts with
field__endswith      # Ends with
field__iendswith     # Case-insensitive ends with
field__in            # In list of values
field__range         # Between two values
field__gt            # Greater than
field__gte           # Greater than or equal
field__lt            # Less than
field__lte           # Less than or equal
field__isnull        # Is null/empty

Usage examples:

from exchangelib import Q

# Simple filtering
recent_emails = account.inbox.filter(
    datetime_received__gte=EWSDateTime.now() - timedelta(days=7)
)

# Complex queries with Q objects
important_from_boss = account.inbox.filter(
    Q(importance='High') & Q(sender__email_address='boss@company.com')
)

# Multiple conditions
project_emails = account.inbox.filter(
    subject__icontains='project',
    datetime_received__range=(start_date, end_date),
    has_attachments=True
).order_by('-datetime_received')

# Exclude spam
clean_inbox = account.inbox.exclude(
    Q(sender__email_address__endswith='spam.com') | 
    Q(subject__icontains='lottery')
)

# Calendar searches
meetings_this_week = account.calendar.filter(
    start__gte=week_start,
    end__lte=week_end,
    is_cancelled=False
).order_by('start')

# Contact searches
company_contacts = account.contacts.filter(
    company_name__iexact='Example Corp'
).order_by('surname', 'given_name')

Install with Tessl CLI

npx tessl i tessl/pypi-exchangelib

docs

account-auth.md

advanced.md

attachments.md

calendar.md

contacts.md

datetime.md

folders.md

index.md

messages.md

search.md

tasks.md

tile.json