Skip to content

Instantly share code, notes, and snippets.

@antichaos
Last active July 25, 2023 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antichaos/c3995a89e66dec53f882964decd61aae to your computer and use it in GitHub Desktop.
Save antichaos/c3995a89e66dec53f882964decd61aae to your computer and use it in GitHub Desktop.
Tableau2csv
import tableauserverclient as TSC
import os
import argparse
import logging
# set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s', filename='tableau2csv.log')
logger = logging.getLogger(__name__)
# set up argument parser
parser = argparse.ArgumentParser(description='Get data from Tableau view as csv using REST API')
parser.add_argument('-s', '--server', help='Tableau server address')
parser.add_argument('-sid', '--site', help='Tableau site')
parser.add_argument('-w', '--workbook', help='Tableau workbook name')
parser.add_argument('-p', '--project', help='Project of Tableau workbook')
parser.add_argument('-v', '--view', help='Tableau view name')
parser.add_argument('-o', '--output', help='output file')
parser.add_argument('-tn', '--token_name', help='personal access token name')
parser.add_argument('-tv', '--token_value', help='personal access token value')
parser.add_argument('-vv', '--verbose', help='verbose mode', action='store_true')
args = parser.parse_args()
# set up variables
server = args.server
site = args.site
workbook = args.workbook
project = args.project
view_name = args.view
csv_file_path = args.output
token_name = args.token_name
token_value = args.token_value
verbose = args.verbose
# set up tableau server client
tableau_auth = TSC.PersonalAccessTokenAuth(token_name=token_name, personal_access_token=token_value, site_id=site)
server = TSC.Server(server, use_server_version=True)
status = 0
with server.auth.sign_in(tableau_auth):
all_workbooks_items, pagination_item = server.workbooks.get()
request_options = TSC.RequestOptions(pagesize=1000)
all_workbooks = list(TSC.Pager(server.workbooks, request_options))
for wb in all_workbooks:
if (wb.name == workbook) and (wb.project_name == project):
server.workbooks.populate_views(wb)
for view in wb.views:
if (view.name == view_name):
server.views.populate_csv(view)
with open(csv_file_path, 'wb') as f:
# Perform byte join on the CSV data
f.write(b''.join(view.csv))
status = 1
if status == 1:
logger.info('Successfully got data from Tableau view as csv')
else:
logger.error('Failed to get data from Tableau view as csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment