如何用Python获取Tableau结果?是否有类似psycopg2的方案或API?
Absolutely! You can pull data/results from Tableau using Python, just like you use psycopg2 with AWS Redshift. There are official and community-supported options to make this straightforward:
1. Tableau Server Client (TSC) Library (Official)
This is the official Python library built for interacting with Tableau Server and Tableau Online. It’s the most convenient way to access view data, workbooks, extracts, and more—no need to manually craft REST API calls.
Here’s a quick example to fetch view data and load it into a pandas DataFrame:
import tableauserverclient as TSC import pandas as pd from io import BytesIO # Set up authentication tableau_auth = TSC.TableauAuth("your_username", "your_password", site_id="your_site_content_url") server = TSC.Server("https://your-tableau-server-domain.com") server.version = "3.21" # Match this to your Tableau version with server.auth.sign_in(tableau_auth): # Retrieve the target view by ID (find this in Tableau Server's URL) view_id = "your_view_id_here" target_view = server.views.get_by_id(view_id) # Populate the view's CSV data server.views.populate_csv(target_view) # Load the CSV directly into a pandas DataFrame df = pd.read_csv(BytesIO(target_view.csv)) print("Fetched Tableau view data:") print(df.head())
2. Tableau REST API (Direct Calls)
If you need more flexibility than the TSC library provides, you can call Tableau’s REST API directly using tools like requests. This is great for custom workflows or edge cases.
Example of fetching view data via REST API:
import requests import pandas as pd from io import StringIO # Step 1: Authenticate to get an access token auth_endpoint = "https://your-tableau-server-domain.com/api/3.21/auth/signin" auth_payload = { "credentials": { "name": "your_username", "password": "your_password", "site": {"contentUrl": "your_site_content_url"} } } response = requests.post(auth_endpoint, json=auth_payload) auth_token = response.json()["credentials"]["token"] site_id = response.json()["credentials"]["site"]["id"] # Step 2: Fetch view data as CSV view_data_endpoint = f"https://your-tableau-server-domain.com/api/3.21/sites/{site_id}/views/your_view_id_here/csv" headers = {"X-Tableau-Auth": auth_token} data_response = requests.get(view_data_endpoint, headers=headers) df = pd.read_csv(StringIO(data_response.text)) print("Fetched Tableau view data via REST API:") print(df.head())
Bonus: Accessing Tableau Desktop Extracts
If you’re working with local Tableau Desktop files (.twbx or .hyper), use the tableauextractapi library to read extract data directly:
from tableauextractapi import Extract, Table # Open the .hyper extract file extract = Extract("path/to/your/extract.hyper") table = extract.open_table("Extract") # Iterate through rows or load into a DataFrame rows = [] for row in table: rows.append([row.get_string(i) for i in range(table.column_count)]) import pandas as pd df = pd.DataFrame(rows, columns=[col.name for col in table.columns]) print(df.head()) extract.close()
Key Notes:
- Make sure to match the API version (e.g.,
3.21) to your Tableau Server/Online version—check Tableau’s documentation for version mappings. - For Tableau Cloud (Online), your server URL will be something like
https://10ax.online.tableau.com. - Permissions matter: Ensure your user account has access to the view/workbook you’re trying to fetch data from.
内容的提问来源于stack exchange,提问作者Prajakta Yerpude

