"Have you ever wondered how to access Microsoft SharePoint in your Python scripts?
This tutorial addresses just that: you will be able to do it in an easy way by using a tool called Connect Bridge."
Originally posted: July 10, 2019
Last updated: October 14, 2026
TL;DR
- What it does: lets a Python script read and write SharePoint lists and document libraries using plain SQL, through ODBC and the
pyodbcmodule. - How: a commercial tool, Connect Bridge (free trial available), sits between Python and SharePoint and presents the SharePoint API as if it were a database.
- What you need: a Windows machine, Python 3,
pyodbc, Connect Bridge with a SharePoint account configured, and the ODBC connection string it generates. - What you get: a ~15-line core script, plus SQL and stored-procedure examples for creating lists, inserting records, and uploading and downloading documents.
- Caveats: Windows only (as tested by the author); commercial license.
- Best for: teams that think in SQL and want to avoid learning the SharePoint API directly.
Contents
Introduction
This article describes how to make Python connect to SharePoint, that is, how to access data of Microsoft SharePoint systems (2013, 2016, 2019 or Online) from within Python language scripts. The data transfers are made via the ODBC layer.
Update note: The examples in this article were originally built and tested in 2019–2022 with Python 3.7 and pyodbc module ver. 4.0.26. In October 2026, we simplified the script and rewrote its error handling for Python 3.14 and pyodbc 5.3.0. If something behaves differently in your environment, please tell us in the comments.
It is important to note that in this article we use a commercial product called Connect Bridge. This is, in fact, what makes the Python / SharePoint integration possible, by allowing the data communication in a way that you are sure that you are not messing up the SharePoint side (and, trust me, this is VERY important). You can get a free trial for Connect Bridge, so you can try all this for yourself.
What is this "Connect Bridge"?
I'm glad you asked! Connect Bridge is a is an integration platform developed by Connecting Software that allows you to connect any software through ODBC drivers, JDBC drivers or Web Services. The general overview of the architecture of the tool is on this client-server scenario diagram.
(opens in a new tab)Connect Bridge integration platform architecture
As you can see on the architecture diagram, the tool can be used to access not only Microsoft SharePoint data from within your Python script, but you can also grab/put data from/to Microsoft Exchange and Microsoft Dynamics CRM among many others.
The aim of our work was to create a Python script that would allow us to talk to SharePoint via Connect Bridge.
We use SharePoint, but it is possible to use similar logic to connect to Microsoft Exchange or to a CRM like Salesforce or Dynamics.
Getting started...
Now let's start this tutorial! Our goal is to create a simple Python script that accesses a SharePoint instance. We assume the SharePoint instance already exists (make sure you have your login credentials at hand). These are simple steps you need to follow:
1. Request a free trial and install Connect Bridge
2. Install Python for Windows ver. 3.14+. Please note that to make writing, running & debugging my script easier we have used Liclipse 12.0.1 with the pydev plugin, but this is, of course, optional. Any editor works, such as VS Code or PyCharm.
3. Install pyodbc module 5.3.0+
4. Run Connect Bridge Management Studio and
4.1. Add an account for SharePoint (Accounts - Add account). This is where you will need those credentials we mentioned earlier.
4.2. Open the New Query option and then the Connection Browser. Find the SharePoint Connector and open it until you see the DefaultConnection. Right-click on it and choose Get Connection string. Then copy the ODBC connection string. You will need it to pass it on to the script.
4.3. Use the New Query option to test out a query that will access what you need in SharePoint. We will do an example query here, but this is where you should put in what it is that you are looking for in SharePoint. Once you have clicked New Query, open the Connection Browser. Find the SharePoint Connector and open it until you see the Tables option. You'll see that the schema contains a “table” called Site_Pages so we can construct our query as
SELECT UniqueId, ContentType, Created, Modified, ContentVersion FROM Site_Pages LIMIT 10;
to select the first 10 entries from SharePoint's Site Pages list. It is important to note that although it looks as if we are using a database directly, that is not the case.
Connect Bridge is accessing the API and then presenting it as if it were a database. Once you have your query, copy it, as you will also need it to pass it on to the script.
Hands on scripting!
The core and, at the same time, the only file in our solution is CBQuery.py. The full source code is below. Please focus on lines 70-92 which depict the core solution. A complete description of how this script works is below.
#!/usr/bin/env python3
# encoding: utf-8
"""
CBQuery -- query data from, write data to SharePoint
Runs SQL queries via the Connect Bridge ODBC driver.
@author: Ana Neto
@created: 2019-07-04
@updated: 2026-10-14
"""
import sys
from argparse import ArgumentParser
from contextlib import closing
import pyodbc
def main():
parser = ArgumentParser(description=__doc__)
parser.add_argument("connstr", help="ODBC connection string from Connect Bridge Management Studio")
parser.add_argument("query", help="SQL query or EXEC statement to run")
args = parser.parse_args()
try:
# Close the connection when finished
with closing(pyodbc.connect(args.connstr)) as conn:
cursor = conn.cursor()
cursor.execute(args.query)
if cursor.description is None:
# No result rows returned
conn.commit()
print("OK")
else:
for row in cursor:
print(row)
conn.commit()
return 0
except KeyboardInterrupt:
return 130
except pyodbc.Error as err:
print(f"ODBC error: {err}", file=sys.stderr)
return 2
if __name__ == "__main__":
sys.exit(main())Here is what our script is doing:
- The
argparseblock reads the two command line arguments,connstr(the ODBC connection string) andquery. - The variable connstr holds the ODBC connection string that we pass into the pyodbc module to create an ODBC connection (stored in the
connvariable). The connection is wrapped incontextlib.closing, so it is always closed when the block ends, even if an error occurs. - As next step, we open a database cursor using the connection stored in conn
- We execute the SQL query (this is the query that was passed via the command line parameter named query)
- If the statement returns rows, we loop over the cursor and read the results row by row, printing each raw row. This was our choice for this demo, but please note this could be any kind of output… we could format as XML, JSON, CSV… or any other kind of data-interchange format. We could also simply use the raw data row object in further code to perform custom tasks.
- If the statement returns no rows (INSERT, EXEC), the script prints “OK”.
- In both cases the transaction is committed explicitly, so changes made by INSERT statements and stored procedures are saved.
- ODBC errors are printed to stderr and the script exits with code 2. Ctrl+C exits with code 130, and success exits with 0.
Running the CBQuery.py script
CBQuery.py accepts two positional command line arguments: connstr and query.
We need to copy these from Connect Bridge Management Studio as explained above (point 4 of the Getting started section).
Then run the script like this:
python CBQuery.py "<connection string>" "SELECT UniqueId, ContentType FROM Site_Pages LIMIT 10;"
Let’s now suppose we want to create a contact list in SharePoint and create a contact on that list.
We basically need to follow the same process but change the query to use a “Stored Procedure”. Again, this “Stored Procedure” will, in fact, be accessing SharePoint via the API, but it is getting the data into the SharePoint you specified. Here is the query that we need to run:
EXEC SP_CREATE_TABLE 'MyContacts', 'This table was created using Connect Bridge', true, 'Contacts';
The parameters, in order, are: the name of the new list (MyContacts); a description; a boolean flag (true); and the SharePoint list template to base it on (Contacts).
The query creates a new SharePoint list (MyContacts), with a description that will show in the quick launch bar of the SharePoint page.
Now we need to call the following query to update the Connect Bridge ODBC schema reflection of the SharePoint schema. So, our new “table” gets visible to the ODBC client. (This procedure takes no parameters.)
EXEC SP_UPDATE_SCHEMA;
We can insert a contact entry into our contact list at this point.
INSERT INTO MyContacts (FirstName, LastName) VALUES ('Ana', 'Neto');
You should now be able to see our entry and our new SharePoint list in the SharePoint UI.
We can select the entry we just created by running the following query.
SELECT FirstName,LastName FROM MyContacts
For uploading a shared document, we will use the existing SharePoint list “Documents” and the “Stored Procedure” SP_INSERT_SHAREDDOCUMENT. The parameters it takes are:
- Shared documents table name
- Filename
- Folder (relative path, on the server)
- MIME type
- Data for the file (Base64-encoded; in the example below,
SGVsbG8gV29ybGQ=is simply the text “Hello World”)
We can insert the document by running the following statement:
EXEC SP_INSERT_SHAREDDOCUMENT
'Documents',
'hello2.txt',
'/Shared Documents/NewFolder',
'text/plain',
'SGVsbG8gV29ybGQ=';
If you want to check the presence of the folder beforehand, you can run the statement below. Its parameters are the document library name and the relative folder path:
EXEC SP_ENSURE_FOLDER_PATH
'Documents',
'/NewFolder';
After running the SP_INSERT_SHAREDDOCUMENT we can check the presence of the document in the “Documents” table by running the statement:
SELECT Id,Name FROM Documents;
We can also download the binary content of a particular document from a particular document library by running the statement below. Its parameters are the library name and the document’s Id, as returned by the SELECT above:
EXEC SP_SAVE_SHAREDDOCUMENT_BY_ID 'Documents', 2;
At this point, you can check the existence of the shared document in the SharePoint UI.
Constraints
So far, we have been able to use the Connect Bridge tool only from Windows operating systems, because no ODBC Linux client library is available (and we are not sure if that is even possible). So be sure to play with your Python scripts on a Windows machine.
Conclusion
We have seen how accessing SharePoint data in Python can be easily done using the Connect Bridge integration platform. Now imagine that what you have seen in this article is also possible with Microsoft Dynamics CRM and Microsoft Exchange! Yes, with Connect Bridge this is possible and the way to do it is similar.
Who can I reach out to for questions?

By Ana Neto, technical advisor at Connecting Software.
“I have been a software engineer since 1997, with a more recent love for writing and public speaking. Do you have any questions or comments about this article? I would love to have your feedback, leave a comment below!"

Comments 1
Thank you for the clear explanation!