Using the CData ODBC Driver for Outlook in PyCharm

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Connect to Outlook as an ODBC data source in PyCharm using the CData ODBC Driver for Outlook.

The CData ODBC Drivers can be used in any environment that supports loading an ODBC Driver. In this tutorial we will explore using the CData ODBC Driver for Outlook from within PyCharm. Included are steps for adding the CData ODBC Driver as a data source, as well as basic PyCharm code to query the data source and display results.

To begin, this tutorial will assume that you have already installed the CData ODBC Driver for Outlook as well as PyCharm.

Add Pyodbc to the Project

Follow the steps below to add the pyodbc module to your project.

  1. Click File -> Settings to open the project settings window.
  2. Click Project Interpreter from the Project: YourProjectName menu.
  3. To add pyodbc, click the + button and enter pyodbc.
  4. Click Install Package to install pyodbc.

Connect to Outlook

You can now connect with an ODBC connection string or a DSN. See the Getting Started section in the CData driver documentation for a guide to creating a DSN on your OS.

Using OAuth Authentication

Microsoft Graph API uses OAuth 2.0 for authentication. You must register an application in the Microsoft Azure Portal to obtain OAuth credentials (Client ID and Client Secret).

Obtaining OAuth Credentials

  1. Log in to the Azure Portal.
  2. Navigate to Azure Active Directory > App registrations.
  3. Click New registration to create a new application.
  4. Enter an application name and select the appropriate account types.
  5. Set the Redirect URI to your application's callback URL (e.g., http://localhost:33333 for desktop apps).
  6. Click Register to create the application.
  7. On the application overview page, copy the Application (client) ID - this is your OAuthClientId.
  8. Navigate to Certificates & secrets and create a new client secret.
  9. Copy the client secret value - this is your OAuthClientSecret.
  10. Navigate to API permissions and add the required Microsoft Graph API permissions:
    • Mail.Read - For accessing email messages
    • Contacts.Read - For accessing contacts
    • Calendars.Read - For accessing calendar events
    • Tasks.Read - For accessing To Do tasks
    • offline_access - For obtaining refresh tokens
  11. Click Grant admin consent to grant these permissions.

Connecting with OAuth

After setting the following connection properties, you are ready to connect:

  • AuthScheme: Set this to OAuth.
  • InitiateOAuth: Set this to GETANDREFRESH. The CData API Profile for Outlook will automatically walk through the OAuth process in order to obtain the access token.
  • OAuthClientId: Set this to the Application (client) ID from Azure Portal.
  • OAuthClientSecret: Set this to the client secret value from Azure Portal.
  • TenantId: Set this to your Azure AD tenant identifier (GUID or domain name like 'contoso.onmicrosoft.com').
  • CallbackURL: Set this to the Redirect URI you specified in your app registration (e.g., http://localhost:33333 for desktop apps).

Example connection string

Profile=C:\profiles\Outlook.apip;AuthScheme=OAuth;InitiateOAuth=GETANDREFRESH;OAuthClientId=your_client_id;OAuthClientSecret=your_client_secret;TenantId=your_tenant_id;CallbackUrl=http://localhost:33333;

Below is the syntax for a DSN:

[CData API Source]
Driver = CData ODBC Driver for Outlook
Description = My Description
Profile = C:\profiles\Outlook.apip
AuthScheme = OAuth
InitiateOAuth = GETANDREFRESH
OAuthClientId = your_client_id
OAuthClientSecret = your_client_secret
TenantId = your_tenant_id
CallbackUrl = http://localhost:33333

Execute SQL to Outlook

Instantiate a Cursor and use the execute method of the Cursor class to execute any SQL statement.

import pyodbc

cnxn = pyodbc.connect('DRIVER={CData ODBC Driver for API};Profile = C:\profiles\Outlook.apip;AuthScheme = OAuth;InitiateOAuth = GETANDREFRESH;OAuthClientId = your_client_id;OAuthClientSecret = your_client_secret;TenantId = your_tenant_id;CallbackUrl = http://localhost:33333;')
cursor = cnxn.cursor()
cursor.execute("SELECT ,  FROM CalendarGroupCalendars WHERE CalendarGroupId = 'group_id'")
rows = cursor.fetchall()
for row in rows:
  print(row., row.)

After connecting to Outlook in PyCharm using the CData ODBC Driver, you will be able to build Python apps with access to Outlook data as if it were a standard database. If you have any questions, comments, or feedback regarding this tutorial, please contact us at [email protected].

Ready to get started?

Connect to live data from Outlook with the API Driver

Connect to Outlook