Tableau Admin

Login
Insights Home Tableau Admin Home

Publishing a Workbook to Tableau Server with Python

Publishing workbooks manually in Tableau Server can be time-consuming, especially when dealing with frequent updates. Using the Tableau Server Client (TSC) Python library, you can automate workbook publishing, making deployments more efficient and error-free.

1. Introduction

In this guide, we’ll walk through the process of:

  • Setting up authentication with a Personal Access Token (PAT)
  • Configuring workbook metadata and project assignments
  • Publishing a .twb or .twbx file programmatically

2. Prerequisites

Before proceeding, ensure you have:

  • Tableau Server or Tableau Cloud access with publishing permissions
  • A Personal Access Token (PAT) for authentication (Set Up a PAT)
  • Python 3.6+ installed and tableauserverclient library

Installing the Required Library

pip install tableauserverclient

3. Setting Up Authentication

Create a .env file with your credentials (Create .env):


TABLEAU_SERVER_URL=https://your-server-url
TABLEAU_PAT_NAME=your-pat-name
TABLEAU_PAT_SECRET=your-pat-secret
TABLEAU_SITE_ID=your-site-id
            

Load credentials in Python:

import os
import tableauserverclient as TSC
from dotenv import load_dotenv

load_dotenv()  # Load environment variables

SERVER_URL = os.getenv("TABLEAU_SERVER_URL")
PAT_NAME = os.getenv("TABLEAU_PAT_NAME")
PAT_SECRET = os.getenv("TABLEAU_PAT_SECRET")
SITE_ID = os.getenv("TABLEAU_SITE_ID")

tableau_auth = TSC.PersonalAccessTokenAuth(PAT_NAME, PAT_SECRET, SITE_ID)
server = TSC.Server(SERVER_URL, use_server_version=True)
            

4. Selecting the Target Project

Every workbook must be published to a specific project in Tableau Server. Here’s how to find and select the correct project:

with server.auth.sign_in(tableau_auth):
    all_projects, _ = server.projects.get()
    for project in all_projects:
        print(f"Project Name: {project.name} | ID: {project.id}")
            

📌 Note: Replace "default" with the exact project name where you want to publish.

project_name = "default"  
project = next((p for p in all_projects if p.name == project_name), None)
if not project:
    raise ValueError(f"Project '{project_name}' not found.")
            

5. Publishing the Workbook

Now, let’s upload a workbook (.twb or .twbx) and assign it to the project.

workbook_path = "path/to/your/workbook.twbx"
workbook_item = TSC.WorkbookItem(project.id)

with server.auth.sign_in(tableau_auth):
    new_workbook = server.workbooks.publish(
        workbook_item, workbook_path, mode=TSC.Server.PublishMode.Overwrite
    )
    print(f"Workbook '{new_workbook.name}' published successfully!")
            

6. Handling Different Publish Modes

PublishMode determines how Tableau handles existing workbooks.

  • Overwrite → Replaces existing workbooks with the same name
  • Append → Adds a new version while keeping the old ones
  • CreateNew → Fails if the workbook already exists

Example using CreateNew mode:

new_workbook = server.workbooks.publish(
    workbook_item, workbook_path, mode=TSC.Server.PublishMode.CreateNew
)
            

7. Running the Script

Save your Python script and execute it:

python publish_workbook.py

If successful, the workbook will be available on your Tableau Server under the specified project.

8. Conclusion

Automating workbook publishing helps save time, reduce manual errors, and streamline deployments. By using tableauserverclient, you can integrate workbook updates into CI/CD pipelines or scheduled jobs.

For next steps, consider:

  • Refreshing Data Extracts automatically after publishing
  • Updating Permissions on published workbooks
  • Scheduling Workbook Refreshes using Python

🚀 Explore more: