Ready to get started?

Download a free trial of the Xero Connector to get started:

 Download Now

Learn more:

Xero Icon Xero Python Connector

Python Connector Libraries for Xero Data Connectivity. Integrate Xero with popular Python tools like Pandas, SQLAlchemy, Dash & petl.

How to Build an ETL App for Xero Data in Python with CData



Create ETL applications and real-time data pipelines for Xero data in Python with petl.

The rich ecosystem of Python modules lets you get to work quickly and integrate your systems more effectively. With the CData Python Connector for Xero and the petl framework, you can build Xero-connected applications and pipelines for extracting, transforming, and loading Xero data. This article shows how to connect to Xero with the CData Python Connector and use petl and pandas to extract, transform, and load Xero data.

With built-in, optimized data processing, the CData Python Connector offers unmatched performance for interacting with live Xero data in Python. When you issue complex SQL queries from Xero, the driver pushes supported SQL operations, like filters and aggregations, directly to Xero and utilizes the embedded SQL engine to process unsupported operations client-side (often SQL functions and JOIN operations).

Connecting to Xero Data

Connecting to Xero data looks just like connecting to any relational data source. Create a connection string using the required connection properties. For this article, you will pass the connection string as a parameter to the create_engine function.

To connect, set the Schema connection property in addition to any authentication values. Xero offers authentication for private applications, public applications, and partner applications. You will need to set the XeroAppAuthentication property to PUBLIC, PRIVATE, or PARTNER, depending on the type of application configured. To connect from a private application, you will additionally need to set the OAuthAccessToken, OAuthClientId, OAuthClientSecret, CertificateStoreType, CertificateStore, and CertificateStorePassword.

To connect from a public or partner application, you can use the embedded OAuthClientId, OAuthClientSecret, and CallbackURL, or you can register an app to obtain your own OAuth values.

See the "Getting Started" chapter of the help documentation for a guide to authenticating to Xero.

After installing the CData Xero Connector, follow the procedure below to install the other required modules and start accessing Xero through Python objects.

Install Required Modules

Use the pip utility to install the required modules and frameworks:

pip install petl
pip install pandas

Build an ETL App for Xero Data in Python

Once the required modules and frameworks are installed, we are ready to build our ETL app. Code snippets follow, but the full source code is available at the end of the article.

First, be sure to import the modules (including the CData Connector) with the following:

import petl as etl
import pandas as pd
import cdata.xero as mod

You can now connect with a connection string. Use the connect function for the CData Xero Connector to create a connection for working with Xero data.

cnxn = mod.connect("InitiateOAuth=GETANDREFRESH;OAuthSettingsLocation=/PATH/TO/OAuthSettings.txt")")

Create a SQL Statement to Query Xero

Use SQL to create a statement for querying Xero. In this article, we read data from the Items entity.

sql = "SELECT Name, QuantityOnHand FROM Items WHERE Name = 'Golf balls - white single'"

Extract, Transform, and Load the Xero Data

With the query results stored in a DataFrame, we can use petl to extract, transform, and load the Xero data. In this example, we extract Xero data, sort the data by the QuantityOnHand column, and load the data into a CSV file.

Loading Xero Data into a CSV File

table1 = etl.fromdb(cnxn,sql)

table2 = etl.sort(table1,'QuantityOnHand')

etl.tocsv(table2,'items_data.csv')

In the following example, we add new rows to the Items table.

Adding New Rows to Xero

table1 = [ ['Name','QuantityOnHand'], ['NewName1','NewQuantityOnHand1'], ['NewName2','NewQuantityOnHand2'], ['NewName3','NewQuantityOnHand3'] ]

etl.appenddb(table1, cnxn, 'Items')

With the CData Python Connector for Xero, you can work with Xero data just like you would with any database, including direct access to data in ETL packages like petl.

Free Trial & More Information

Download a free, 30-day trial of the CData Python Connector for Xero to start building Python apps and scripts with connectivity to Xero data. Reach out to our Support Team if you have any questions.



Full Source Code


import petl as etl
import pandas as pd
import cdata.xero as mod

cnxn = mod.connect("InitiateOAuth=GETANDREFRESH;OAuthSettingsLocation=/PATH/TO/OAuthSettings.txt")")

sql = "SELECT Name, QuantityOnHand FROM Items WHERE Name = 'Golf balls - white single'"

table1 = etl.fromdb(cnxn,sql)

table2 = etl.sort(table1,'QuantityOnHand')

etl.tocsv(table2,'items_data.csv')

table3 = [ ['Name','QuantityOnHand'], ['NewName1','NewQuantityOnHand1'], ['NewName2','NewQuantityOnHand2'], ['NewName3','NewQuantityOnHand3'] ]

etl.appenddb(table3, cnxn, 'Items')