Building a Tableau Documentation Generator with Python (Tkinter)

If you work with Tableau Server, you know how quickly dashboards become hard to manage:

  • Too many calculated fields
  • Unknown datasources
  • No proper documentation

To solve this, we can build a simple Python desktop tool that automatically extracts metadata from Tableau workbooks (.twb / .twbx) and generates documentation.


🎯 What This Tool Does

This application allows you to:

βœ” Select a Tableau file (.twb / .twbx)
βœ” Extract workbook XML automatically
βœ” Parse:

  • Dashboard names
  • Datasource names
  • Calculated fields

βœ” Generate a clean .txt documentation file


🧠 Full Python Application Code

πŸ–₯️ 1. Import Libraries

import tkinter as tk
from tkinter import filedialog, messagebox
import zipfile
import xml.etree.ElementTree as ET
import html
import os

πŸ“¦ 2. Extract .twb from .twbx

def extract_twb(file_path):
if file_path.endswith(".twb"):
with open(file_path, "rb") as f:
return f.read() elif file_path.endswith(".twbx"):
with zipfile.ZipFile(file_path, "r") as z:
for file in z.namelist():
if file.endswith(".twb"):
return z.read(file) raise Exception("No .twb found inside file")

🧩 3. Build Field Mapping (Fix Internal Names)

def build_field_map(root):
field_map = {} for col in root.findall(".//column"):
internal = col.attrib.get("name")
caption = col.attrib.get("caption") if internal and caption:
field_map[internal] = caption return field_map

πŸ”„ 4. Replace Internal Field Names

def replace_internal_names(formula, field_map):
for internal, caption in field_map.items():
if internal in formula:
formula = formula.replace(internal, f"[{caption}]")
return formula

πŸ“Š 5. Parse Tableau Workbook XML

def parse_twb(xml_bytes):
root = ET.fromstring(xml_bytes) dashboards = []
datasources = set()
calculations = [] field_map = build_field_map(root) # Dashboards
for d in root.findall(".//dashboard"):
dashboards.append(d.attrib.get("name")) # Datasources
for ds in root.findall(".//datasource"):
name = ds.attrib.get("name")
if name:
datasources.add(name) # Calculated Fields
for col in root.findall(".//column"):
calc = col.find("calculation")
if calc is not None:
name = col.attrib.get("caption") or col.attrib.get("name")
formula_raw = calc.attrib.get("formula", "") formula = html.unescape(formula_raw)
formula = replace_internal_names(formula, field_map) calculations.append((name, formula)) return dashboards, list(datasources), calculations

πŸ“ 6. Generate Documentation File

def generate_txt(file_path, dashboards, datasources, calculations):
output_file = os.path.splitext(file_path)[0] + "_documentation.txt" with open(output_file, "w", encoding="utf-8") as f: f.write("===== DASHBOARDS =====\n")
for d in dashboards:
f.write(f"- {d}\n") f.write("\n===== DATASOURCES =====\n")
for ds in datasources:
f.write(f"- {ds}\n") f.write("\n===== CALCULATED FIELDS =====\n")
for name, formula in calculations:
f.write(f"\n{name} =\n{formula}\n") return output_file

πŸ–±οΈ 7. File Processing Logic

def process_file():
file_path = filedialog.askopenfilename(
filetypes=[("Tableau Files", "*.twbx *.twb")]
) if not file_path:
return try:
xml_bytes = extract_twb(file_path)
dashboards, datasources, calculations = parse_twb(xml_bytes) output_file = generate_txt(file_path, dashboards, datasources, calculations) messagebox.showinfo(
"Success",
f"Documentation created:\n{output_file}"
) except Exception as e:
messagebox.showerror("Error", str(e))

🧾 8. UI (Tkinter Interface)

app = tk.Tk()
app.title("Tableau Documentation Generator")
app.geometry("420x220")label = tk.Label(
app,
text="Select Tableau (.twbx / .twb) file",
font=("Arial", 12)
)
label.pack(pady=20)btn = tk.Button(
app,
text="Choose File",
command=process_file,
height=2,
width=25
)
btn.pack()app.mainloop()

πŸ“„ Output Example

After running the tool, you get a file like:

===== DASHBOARDS =====
- Sales Overview
- Ticket Dashboard===== DATASOURCES =====
- PostgreSQL_DB
- Salesforce===== CALCULATED FIELDS =====Closed_tickets =
IF CONTAINS([STATUS], 'Abgeschlossen') THEN "Closed"
ELSE "Pending"
END

πŸš€ Why This Tool Is Useful

Instead of manually inspecting Tableau workbooks:

βœ” You get instant documentation
βœ” No need for Tableau API
βœ” Works offline
βœ” Great for governance and audits
βœ” Easy onboarding for new analysts


🧠 Final Thought

Tableau workbooks are just structured XML. Once you understand that, you can automate a huge part of analytics documentation and governance.

Click to rate this post!
Spread the love