I am trying to automate my weekly reporting by using Python to query the Tableau Server REST API. Specifically, I need to sign in, locate a specific view by its ID, and then export that view as a high-resolution PNG or PDF. Are there any existing Python libraries that simplify this, and how do I handle passing filters or parameters so the exported file shows specific data subsets?
3 answers
The most efficient way to handle this is by using the Tableau Server Client (TSC) library, which is a Python wrapper for the REST API. First, you authenticate using a Personal Access Token (PAT). Once signed in, you use the server.views.populate_pdf or server.views.populate_image methods. To pass filters, you can use the PDFRequestOptions class and its .vf() (view filter) method. This allows you to dynamically change the dashboard state before the export happens. After calling the populate method, the data is stored in the view object itself (e.g., view.pdf), which you can then write to a local file using a standard binary write open('report.pdf', 'wb').
Does the TSC library handle large dashboard exports better than a direct HTTP request to the .pdf URL endpoint, or is there a timeout risk I should be aware of?
You should definitely use Personal Access Tokens instead of passwords for your Python scripts. It's much more secure and prevents issues with MFA.
I agree with Sarah. PATs are the industry standard now. Just make sure to store the token secret in an environment variable rather than hardcoding it into your .py file for safety!
TSC is generally more reliable because it manages the session headers and authentication tokens for you. However, for extremely complex dashboards that take over 60 seconds to render, you might need to adjust the server.add_http_options to increase the timeout limit. If you use the direct URL method, you risk the session expiring mid-download if the server is under heavy load.