Publish Tableau Workbooks from Python

Applies to Extracts

Gaurav Neema
3 min readAug 17, 2020

Publishing the Tableau workbooks by using Tableau Desktop is pretty simple and straightforward. But when it comes to publishing them programmatically through API calls, it can be a pain in the ass. In one of my project, I have to publish the workbook on the server on-the-fly based on some user input. The user input then decides what data to fill in the workbook. The structure of workbook was same in all cases, the difference was just the data. I tried a lot of ways and in this tutorial, let me share the simplest of all 😃

The basic idea behind this is to create a one-time dashboard (skeleton or template) through Tableau desktop, download the packaged workbook (.twbx file), change the data in the packaged workbook and publish it through API. So let’s get started…

Setup

pip3 install tableauserverclient
pip3 install hyperapi

Imports

import os
import shutil
from zipfile import ZipFile
import tableauserverclient as TSC
from tableauhyperapi import *

Step 1: Create a template workbook from Tableau Desktop

This is a one time process. Just create the dashboard with dummy data on Tableau Desktop and dowload the packaged workbook (.twbx). Let’s call it template workbook. Keep this in your project directory. All the subsequent steps will be done programmatically.

Step 2: Unzip the template workbook and create a copy of it

Please note that the twbx file is nothing but some form of zip file. Normal zip unzip operations will work with this.

Step 3: Updating the hyper file with new data

Hyper is a data-engine used by tableau from version 10.5 onwards. Previously it used .tde format. More about hyper engine here.
For simplicity, you can imagine hyper as some sort of SQL file. It’s called an “Extract” in Tableau terms. The Hyper API uses SQL based queries to interact with the .hyper file. Our workbook file (.twb) will ask the hyper file for the data. Also, the schema (or table definition) in the new file should be consistent with the hyper file contained in the template workbook.

Code to generate the hyper file from CSV.

Step 4: Modifying twb file, Zipping back

In twb file, you can change the title of dashboard, workbook name etc. This can be done by XML hacking or simple find and replace in Python.
After this we need to zip the contents back and change the extension to .twbx

Final Step: Publish the workbook

Let’s finally publish the newly created workbook to tableau server.

You can find the full code on this github repository.

This was all from my side. Any feedback/thoughts, feel free to post in the response section down here. Would love to hear them! If you like to connect with me on social media, here is my LinkedIn and twitter handle.

If you find this article helpful and interesting, please let me know by giving some claps 👏.

--

--

Responses (1)