def test_can_open_twbx_and_save_changes(self): original_wb = Workbook(self.workbook_file.name) original_wb.datasources[0].connections[0].server = 'newdb' original_wb.save() new_wb = Workbook(self.workbook_file.name) self.assertEqual(new_wb.datasources[0].connections[0].server, 'newdb')
def test_can_update_datasource_connection_and_save(self): original_wb = Workbook(self.workbook_file.name) original_wb.datasources[0].connections[0].dbname = 'newdb' original_wb.save() new_wb = Workbook(self.workbook_file.name) self.assertEqual(new_wb.datasources[0].connections[0].dbname, 'newdb')
def test_can_open_twbx_and_save_as_changes(self): new_twbx_filename = 'newtwbx.twbx' original_wb = Workbook(self.workbook_file.name) original_wb.datasources[0].connections[0].server = 'newdb' original_wb.save_as(new_twbx_filename) new_wb = Workbook(new_twbx_filename) self.assertEqual(new_wb.datasources[0].connections[0].server, 'newdb') os.unlink(new_twbx_filename)
def test_can_mixed_connections_workbook(self): wb = Workbook(self.twb_file.name) self.assertTrue(len(wb.datasources), 2) self.assertTrue(len(wb.datasources[1].connections), 2) self.assertEqual(wb.datasources[0].connections[0].dbclass, 'sqlproxy') self.assertEqual(wb.datasources[1].connections[0].dbclass, 'mysql') self.assertEqual(wb.datasources[1].connections[1].dbclass, 'sqlserver')
def test_can_extract_datasourceV10(self): wb = Workbook(self.workbook_file.name) self.assertEqual(len(wb.datasources), 1) self.assertEqual(len(wb.datasources[0].connections), 2) self.assertIsInstance(wb.datasources[0].connections, list) self.assertIsInstance(wb.datasources[0], Datasource) self.assertEqual(wb.datasources[0].name, 'federated.1s4nxn20cywkdv13ql0yk0g1mpdx')
def test_save_has_xml_declaration(self): original_wb = Workbook(self.workbook_file.name) original_wb.datasources[0].connections[0].dbname = 'newdb.test.tsi.lan' original_wb.save() with open(self.workbook_file.name) as f: first_line = f.readline().strip() # first line should be xml tag self.assertEqual(first_line, "<?xml version='1.0' encoding='utf-8'?>")
def print_info_workbook(file_name): ############################################################ # Step 2) Open the .twb or .twbx we want to inspect ############################################################ sourceWB = Workbook(file_name) ############################################################ # Step 3) Print out all of the datasources on this workbook ############################################################ print('----------------------------------------------------------') print('--- {} total datasources in this workbook'.format( len(sourceWB.datasources))) print('----------------------------------------------------------') ############################################################ # Step 4) Print the total field using on each datasource, # the fields and what type they are ############################################################ for key, value in enumerate(sourceWB.datasources): print('----------------------------------------------------------') print('--- {0} total fields in {1} datasource'.format( len(value.fields.values()), value.name)) print('----------------------------------------------------------') print('----------------------------------------------------------') print('-- Info for our .tds:') print('-- name:\t{0}'.format(value.name)) print('-- version:\t{0}'.format(value.version)) print('----------------------------------------------------------') for connKey, connInfo in enumerate(value.connections): print('-------Connection #{0} - {1} ----------------------------'. format(connKey, connInfo)) print('Server: {}'.format(connInfo.server)) print('DbName: {}'.format(connInfo.dbname)) print('Username: {}'.format(connInfo.username)) print('----------------------------------------------------------') for count, field in enumerate(value.fields.values()): print('{:>4}: {} is a {}'.format(count + 1, field.name, field.datatype)) blank_line = False if field.calculation: print(' the formula is {}'.format(field.calculation)) blank_line = True if field.default_aggregation: print(' the default aggregation is {}'.format( field.default_aggregation)) blank_line = True if field.description: print(' the description is {}'.format(field.description)) if blank_line: print('') print('----------------------------------------------------------')
def get_workbook_info(wbds): # Make a temp file for downloading the workbook temp = tempfile.NamedTemporaryFile(delete=False) try: # Downlaod the workbook into a temp file, without the extract server.workbooks.download(wb.id, temp.name, include_extract=False) # Open the workbook in the doc api and pull the info we need parsed = Workbook(temp.name) return parsed except Exception as e: print(e) finally: temp.close() os.remove(temp.name)
def initial(filename): wb = Workbook(os.path.join('twbx', filename)) data = [] for datasource in wb.datasources: fields = [] for field in datasource.fields.values(): fields.append({ 'name': field.name, 'role': field.role, 'datatype': field.datatype, # 'Number Type': field.type, 'caption': field.caption, 'calculation': field.calculation == '1', }) data.append({ 'name': datasource.name, 'caption': datasource.caption or datasource.name, 'fields': fields }) return data
def test_82_workbook_throws_exception(self): with self.assertRaises(TableauVersionNotSupportedException): wb = Workbook(TABLEAU_82_TWB)
def setUp(self): self.wb = Workbook(TEST_TWB_FILE) self.ds = self.wb.datasources[ 0] # Assume the first datasource in the file
from pathlib import Path import requests from tableaudocumentapi import Workbook import os import sys filename = Path('metadata.pdf') url = 'https://www.echr.coe.int/Documents/Application_Form_ENG.pdf' response = requests.get(url) filename.write_bytes(response.content) sourceWB = Workbook(file_name) sourceWB.save_as(os.path.join(os.path.dirname(filename))) def file_save_as(self): fout = asksaveasfilename(defaultextension='.pdf') try: with open(fout, 'w') as output: for x in self.entries: output.write(x.get()) except FileNotFoundError: print("Cancelled save or error in filename") file_save_as() # import sys # import os # import pathlib
def test_exception_when_workbook_given_tdsx(self): with self.assertRaises(TableauInvalidFileException): wb = Workbook(TABLEAU_10_TDSX)
def test_no_exceptions_thrown(self): wb = Workbook(EMPTY_WORKBOOK) self.assertIsNotNone(wb)
from tableaudocumentapi import Workbook from tableaudocumentapi import Connection sourceWB = Workbook('TestCase1.twbx') #Printing workbook information print "Workbook Name : " print sourceWB.filename print "Sheet Names : " print sourceWB.worksheets #Printing data connection information for x in sourceWB.datasources: print "Datasource details in following order (Server name, database name, user name, database type, port, authentication) :" for j in x.connections: print j.server print j.dbname print j.username print j.dbclass print j.port print j.authentication print " "
def test_can_get_worksheets(self): wb = Workbook(self.workbook_file.name) self.assertIsNotNone(wb.worksheets)
from tableaudocumentapi import Workbook from tableaudocumentapi import Datasource from tableaudocumentapi import Field from tableaudocumentapi import Connection import pprint as pp sourceWB = Workbook('ZenoWorkbook.twb') sourceDB = Datasource.from_file('Beatlog (BeatlogV1).tdsx') print("Filename: " + sourceWB.filename) print(sourceWB.worksheets) pp.pprint(sourceWB.datasources) def listTDS(sourceTDS): print('----------------------------------------------------------') print('-- Info for our .tds:') print('-- name:\t{0}'.format(sourceTDS.name)) print('-- version:\t{0}'.format(sourceTDS.version)) print('----------------------------------------------------------') return pp.pprint(sourceDB.connections) pp.pprint(sourceDB.fields) def showFields(sourceTDS): print('----------------------------------------------------------')
for field in datasource.fields.values(): field_attributes = [ field.id, field.caption, field.alias, field.datatype, field.role, field.is_quantitative, field.is_ordinal, field.is_nominal, field.calculation, field.default_aggregation, field.description ] print(field_attributes) # file_name = "C:\\Users\\jj2362\\Desktop\\docs in\\standard frequent flyer.tds" file_name = "C:\\Users\\jj2362\\Desktop\\Sheet1 (Visual_Analytics_TOC_DataSimulated).tds" # file_name = "C:\\Users\\jj2362\\Desktop\\docs in\\Master.twb" file_type = xml_open(file_name) base = file_type.getroot() print(base.tag) if base.tag == 'datasource': document = tableaudocumentapi.Datasource.from_file(file_name) process_datasources(document) else: document = Workbook(file_name) for datasource in document.datasources: process_datasources(datasource) print("") # for datasource in workbook.datasources: test = Field() print(test._attributes)
def test_ephemeral_fields_do_not_cause_errors(self): wb = Workbook(EPHEMERAL_FIELD_FILE) self.assertIsNotNone(wb)
cur = db.cursor() cur.execute( "SELECT uniqueField, schema, DB_Username, DB_Password, Server_Username FROM tablename" ) for row in cur.fetchall(): uniqueField.append(row[0]) schema.append(row[1]) db_username.append(row[2]) db_password.append(row[3]) server_username.append(row[4]) counter = counter + 1 db.close() ###Creating duplicate copies of workbooks### sourceWB = Workbook('Base.twbx') sourceDS = Datasource.from_file('Base.tdsx') for i in range(0, len(uniqueField)): for x in sourceWB.datasources: for j in x.connections: j.dbname = schema[i] j.username = db_username[i] for j in sourceDS.connections: j.dbname = schema[i] j.username = db_username[i] #Saving the workbook and datasource Workbook.save_as(sourceWB, uniqueField[i] + '.twbx') Datasource.save_as(sourceDS, uniqueField[i] + '.tdsx') ###Creating sites, projects and users if they don't exist, and publishing the workbooks###
tableau_auth = TSC.TableauAuth('login', 'password',site.content_url) server.auth.sign_in(tableau_auth) all_workbooks = list(TSC.Pager(server.workbooks)) ##For Each Workbook for workbook in all_workbooks: if workbook.id=="54fb14ca-7395-47fa-b6db-af568699bd92": try: ##Download Workbook workbooks.append([workbook.id,workbook.name,workbook.owner_id,workbook.project_id,workbook.project_name,site.name]) file_path = server.workbooks.download(workbook.id,no_extract=True) print("Downloaded the file to {0}".format(file_path)) extension=file_path.split(".")[len(file_path.split("."))-1] tree="" ##Manage extenssion if extension == "twb": wb = Workbook(file_path) tree = ET.parse(file_path) os.remove(file_path) else: mzip = os.rename(file_path,"temp.zip" ) zip = zipfile.ZipFile("temp.zip") zip=zip.extractall('temp') name="" for f in glob.glob('temp/*.twb'): name=f wb = Workbook(name) tree = ET.parse(name) os.remove("temp.zip") shutil.rmtree("temp") root = tree.getroot() ##Get Data for Dashboards
from tableaudocumentapi import Workbook sourceWB = Workbook('workbook__31__5ebd2e77c610660027445090_output.twbx') for i in range(0, (len(sourceWB.datasources))): sourceWB.datasources[i].connections[ 0].server = "sandbox3.opexanalytics.com" sourceWB.datasources[i].connections[ 0].dbname = "qa_tableau_test_app_5ed5fba9c966080027ea792a" sourceWB.datasources[i].connections[ 0].username = "******" sourceWB.datasources[i].connections[0].password = "******" print("Updated for : ", (sourceWB.datasources[i].caption)) sourceWB.save()
def test_can_extract_datasource(self): wb = Workbook(self.workbook_file.name) self.assertEqual(len(wb.datasources), 1) self.assertIsInstance(wb.datasources[0], Datasource) self.assertEqual(wb.datasources[0].name, 'sqlserver.17u3bqc16tjtxn14e2hxh19tyvpo')
def test_has_filename(self): wb = Workbook(self.workbook_file.name) self.assertEqual(wb.filename, self.workbook_file.name)
def main(): parser = argparse.ArgumentParser( description='Connect and publish a workbook to a server.') parser.add_argument('--server', '-s', required=True, help='server address') parser.add_argument('--username', '-u', required=True, help='username to sign into server') parser.add_argument('--password', '-p', required=True, help='password for the user') parser.add_argument('--dest', '-D', required=True, help='destination server address') parser.add_argument('-U', required=True, help='username to sign into destination server') parser.add_argument('-P', required=True, help='password for the user on the destination server') parser.add_argument('--source', '-S', default=None) parser.add_argument('--target', '-T', default=None) parser.add_argument('--directory', '-d', default='migrated') parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', help='desired logging level (set to error by default)') parser.add_argument('workbook', help='one or more workbooks to publish', nargs='+') args = parser.parse_args() # Set logging level based on user input, or error by default logging_level = getattr(logging, args.logging_level.upper()) logging.basicConfig(level=logging_level) # Step 1: Sign in to server. server, server_auth = connect(args.server, args.username, args.password) dest, dest_auth = connect(args.dest, args.U, args.P) overwrite_true = TSC.Server.PublishMode.Overwrite os.makedirs(args.directory, exist_ok=True) with server.auth.sign_in(server_auth) and dest.auth.sign_in(dest_auth): # Step 2: Get the project on server. source = filter_project(args.source, server) target = filter_project(args.target, dest) print("source: {0} [{1}]".format(server.baseurl, source.name)) print("destination: {0} [{1}]".format(dest.baseurl, target.name)) # Step 4: If project is found, build lookup tables. if source is not None and target is not None: # Step 3: Build a list of datasources. ds_source = extract_ds(server, source) ds_target = extract_ds(dest, target) dbnames_from_to = map_content_url_from_to(ds_source, ds_target) # Step 4: Form a new workbook item and publish. for wb in args.workbook: pub = Workbook(wb) for ds in pub.datasources: value = '' if len(ds.connections ) == 1 and ds.connections[0].dbclass == 'sqlproxy': try: if len(ds.caption) == 0 and ds.name in ds_target: value = ds.name ds.connections[0] = ds_target[ value].connections[0] elif ds.connections[0].dbname in dbnames_from_to: value = dbnames_from_to[ ds.connections[0].dbname] ds.connections[0].dbname = value else: value = clean_name(ds.caption) ds.connections[0] = ds_target[ value].connections[0] except LookupError as e: raise LookupError( "lookup information between target and source is inconsistent, datasource caption: {0}, connection: {1}" .format(ds.caption, ds.connections[0].dbname)) print("{0}, {1} --> {2}".format(ds.caption, ds.name, value)) wb_migrated = os.path.join(args.directory, wb) pub.save_as(wb_migrated) new_workbook = TSC.WorkbookItem(target.id) try: new_workbook = dest.workbooks.publish( new_workbook, wb_migrated, overwrite_true) except TSC.server.endpoint.exceptions.ServerResponseError: dest.version = '2.4' new_workbook = dest.workbooks.publish( new_workbook, wb_migrated, overwrite_true) print("workbook published ID: {0}".format(new_workbook.id))
def main(): parser = argparse.ArgumentParser(description='dump output from workbooks') parser.add_argument('--server', '-s', required=True, help='server address') parser.add_argument('--site', '-S', default='') parser.add_argument('--project', required=True, default=None) parser.add_argument('--username', '-u', help='username to sign into server') parser.add_argument('-p', '--password', default=None) parser.add_argument('--filepath', '-f', required=True, help='filepath to save the image(s) returned') parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', help='desired logging level (set to error by default)') parser.add_argument( 'workbook', help='one or more workbooks to process, "all" means all workbooks', nargs='+') args = parser.parse_args() if args.password is None: password = getpass.getpass("Password: "******"2.5" with server.auth.sign_in(tableau_auth): server.use_server_version() calculations = dict() # Step 2: Query for the workbook that we want info on for wb in TSC.Pager(server.workbooks): if (args.workbook[0] == "all" or wb.name in args.workbook) and wb.project_name == args.project: filename = server.workbooks.download(wb.id, filepath=args.filepath, include_extract=False) contents = Workbook(filename) print("workbook [{0}]: {1}".format(wb.name, contents.filename)) for ds in contents.datasources: for name in ds.fields: field = ds.fields[name] if field.calculation is not None and 'false' != field.calculation: calculations[field.id] = field for ds in contents.datasources: for name in ds.fields: field = ds.fields[name] s = substitute_calculations(calculations, field.calculation) if len(field.worksheets) > 0: print('field [{1}]({0}): {3}; "{4}":"{5}"'.format( field.datatype, field.name, field.caption, ", ".join(field.worksheets), field.calculation, s))
def test_can_open_twbx(self): wb = Workbook(self.workbook_file.name) self.assertTrue(wb.datasources) self.assertTrue(wb.datasources[0].connections)
import csv # so we can work with our database list (in a CSV file) ############################################################ # Step 1) Use Workbook object from the Document API ############################################################ from tableaudocumentapi import Workbook ############################################################ # Step 2) Open the .twb we want to replicate ############################################################ sourceWB = Workbook('sample-superstore.twb') ############################################################ # Step 3) Use a database list (in CSV), loop thru and # create new .twb's with their settings ############################################################ with open('databases.csv') as csvfile: databases = csv.DictReader(csvfile, delimiter=',', quotechar='"') for row in databases: # Set our unique values for this database sourceWB.datasources[0].connections[0].server = row['Server'] sourceWB.datasources[0].connections[0].dbname = row['Database'] sourceWB.datasources[0].connections[0].username = row['User'] # Save our newly created .twb with the new file name sourceWB.save_as(row['DBFriendlyName'] + ' - Superstore' + '.twb')
def view_conn_info(adv_dir, file_name): source = Workbook(adv_dir + '/' + file_name) for ds in source.datasources: for conn in ds.connections: # print conn write_log('output/run.log', conn, print_log_value=True)
from tableaudocumentapi import Workbook, Datasource import os import pandas as pd baseLoc = "../../data/" baseTab = os.path.join(baseLoc, 'tableau') baseTabout = os.path.join(baseLoc, 'tableauout') Details = [] print(baseTab) for file in os.listdir(baseTab): if file.endswith(".twbx"): # print(file) sourceWB = Workbook(os.path.join(baseTab, file)) worksheets = sourceWB.worksheets for worksheet in worksheets: for datasource in sourceWB.datasources: for count, field in enumerate(datasource.fields.values()): for fieldWorksheet in field.worksheets: if worksheet == fieldWorksheet: # print(file,worksheet, "==", fieldWorksheet, "|", count, field.name, "-", field.caption) Details.append([ file, worksheet, datasource.caption, count, field.name, field.caption, field.role ]) detailsdc = [] for file in os.listdir(baseTab):