3 Replies Latest reply on Jan 16, 2019 4:28 PM by Israel Curiel

    Publish tableau workbooks using python

    Sabari A



      I am trying to publish multiple tableau workbooks using python. I have many workbooks that are present in a folder and I want to publish it to tableau server using a python code that will upload the workbooks overnight one after other. I have successfully achieved it for a different set of workbooks, but for this set I am getting an error as "Could not obtain lock on ' " .


      My code works for a different set of workbooks but fails for the other set. I am not sure what this error means and why my python code is failing.


      Looking forward for suggestions. Thanks in advance for the great help.



        • 1. Re: Publish tableau workbooks using python

          Hi Sabari A,


          Could you please first try to refresh your extract in the workbook?

          Let me know how it goes



          Lénaïc RIÉDINGER, Global Community Engineer Tableau

          Tableau Community Forums | Knowledge Base

          If you see a Helpful or Correct response, please mark it thanks to the buttons below the targeted post!


          • 2. Re: Publish tableau workbooks using python
            Sabari A

            Hi Lenaic,


            I tried refreshing the extracts and I am still getting the same error. The problem here is some workbooks get published when I run the same code for the second time but this time a different set of workbooks gets failed to publish.



            • 3. Re: Publish tableau workbooks using python
              Israel Curiel

              I'm trying to synchronize our workbooks from our production environment to our Business Continuity Plan server (BCP). I'm able to download my production workbooks and loop through all the connections. When the code is run I receive no error but the workbooks are not published. Can you share the code you used? Below is the function I'm using.


              def SyncWorkbooksAcrossServers(source_server_name, destination_server_name, username, logging_level):
                  #This function currently downloads all the workbooks from the server and project
                  #The example of this program was derived from https://github.com/tableau/server-client-python/blob/master/samples/move_workbook_projects.py
                  #I also used this page for publishing the workbook https://github.com/tableau/server-client-python/blob/master/samples/publish_workbook.py
                  #password = getpass.getpass("Password: ")
                  logging_level = logging_level.upper()
                  # Set logging level based on user input, or error by default
                  # Step 1: Sign in to both sites on server
                  tableau_auth = TSC.TableauAuth(username, password)
                  source_server = TSC.Server(source_server_name)
                  source_server.add_http_options({'verify': False})
                  dest_server = TSC.Server(destination_server_name)
                  dest_server.add_http_options({'verify': False})
                  default_project_ID = "b2d621ae-0d5c-48cc-a1de"
                  overwrite_true = TSC.Server.PublishMode.Overwrite
                  # Step 2: Create directory to download all the report files to.
                  tmpdir = tempfile.mkdtemp()
                  with source_server.auth.sign_in(tableau_auth):
                      src_site_id = source_server.site_id
                      # Step 2: Query workbook to move
                      req_option = TSC.RequestOptions()
                      #                                 TSC.RequestOptions.Operator.Equals, workbook_name))     
                      #all_workbooks, pagination_item = source_server.workbooks.get(req_option)
                      all_workbooks, pagination_item = source_server.workbooks.get()
                      # Step 3: Download workbook to a temp directory

                      if len(all_workbooks) == 0:
                          print('No workbook named {} found.'.format(workbook_name))

                              #Loop through all workbooks
                              for workbook in all_workbooks:
                                  if(workbook.project_name == 'Corporate Dashboards' and workbook.name == "Performance Workbook"):
                                      print("Name: " + workbook.name + " Project: " + workbook.project_name + " WorkbookID: " + str(workbook.id) )
                                      workbook_path = source_server.workbooks.download(workbook.id, tmpdir)
                                      #wb_dict_ = dir(workbook)
                                      print("Total Connections: " + str(len(workbook.connections)))
                                      #Create a container for all connections
                                      all_connections = list()
                                      for conn in workbook.connections:           
                                          #Replace all the production connections with BCP connections.
                                          #if (conn.connection_type == "teradata") and (conn.server_address == "myprodserver.com"):
                                          #    conn.server_address = "mybcpserver.com"
                                          #    workbook.connections.update(conn)

                                          newConnection = ConnectionItem()
                                          newConnection.server_address = conn.server_address
                                          newConnection.connection_credentials = ConnectionCredentials(conn.username, conn.password, True)
                                          #conn_dict_ = dir(conn)
                                          #conn_cred = conn.connection_credentials
                                          #cc = dir(conn_cred)
                                      new_workbook = TSC.WorkbookItem(default_project_ID)
                                      new_workbook = dest_server.workbooks.publish(new_workbook, workbook_path, overwrite_true, connections=all_connections, as_job=0)

                                      print("-Workbook published Name: ",workbook.name, " -Workbook ID: ",workbook.id)

                              print("All Done")                  
                              os.system(tmpdir.replace('\\', '/'))
                              return "Added Workbook: " + workbook.name