Use python3.8.0. TSC needs to be installed. github( https://github.com/tableau/server-client-python ) PyPI( https://pypi.org/project/tableauserverclient/ )
pip install tableauserverclient
import tableauserverclient as TSC
Enter the account and server information used for username, password, siteid, and server url. Also, I want to use the latest version, so I set the user_server_version option to True.
tableau_auth = TSC.TableauAuth('username', 'password', 'siteid')
server = TSC.Server('serverurl', use_server_version=True)
Run the following code to see if the view exists on your site. Also, be sure to execute ʻall_views` as it will be used later.
all_views, pagination_item = server.views.get()
print([view.name for view in all_views])
If you want to get the view in file format: pdf, size: A4, orientation: landscape, use the following code.
pdf_req_option = TSC.PDFRequestOptions(page_type=TSC.PDFRequestOptions.PageType.A4,
orientation=TSC.PDFRequestOptions.Orientation.Landscape)
with server.auth.sign_in(tableau_auth):
for view_item in all_views:
#Generate pdf image
server.views.populate_pdf(view_item, req_options=pdf_req_option)
with open(f'{view_item.name}.pdf', 'wb') as f:
f.write(view_item.pdf)
PDFRequestOptions
Use this class to specify the format of the PDF that is returned for the view. See views.populate_pdf.
PDFRequestOptions(page_type=None, orientation=None)
page_type
You can specify the size of the pdf. The default is Legal
.
-PageType.A3
-PageType.A4
-PageType.A5
-PageType.B5
-PageType.Executive
-PageType.Folio
-PageType.Ledger
-PageType.Legal
-PageType.Letter
-PageType.Note
-PageType.Quarto
-PageType.Tabloid
orientation
You can specify the orientation of the page. The default is Portrait
(vertical).
-ʻOrientation.Portrait: Vertical -ʻOrientation.Landscape
: Horizontal
You can also use the vf
method on this class to filter the view.
For example
# (optional) set a view filter
pdf_req_option.vf('Region', 'West')
Then, the view with Region
filtered to West
will be converted to pdf.
See also: PDFRequestOptions class
To get the view in file format: png and orientation: landscape, use the following code.
image_req_option = TSC.ImageRequestOptions(imageresolution=TSC.ImageRequestOptions.Resolution.High)
with server.auth.sign_in(tableau_auth):
for view_item in all_views:
#Generate png image
server.views.populate_image(view_item, req_options=image_req_option)
with open(f'{view_item.name}.png', 'wb') as f:
f.write(view_item.image)
ImageRequestOptions
Use this class to specify the resolution of the view returned as an image. You can also use this class to specify view filters to be applied when the image is generated. See views.populate_image.
It seems that you can specify the resolution with image resolution.
ImageRequestOptions(imageresolution=None)
imageresolution
If not specified, the output will be 784 pixels wide, and if you specify Rresolution.High
(highest value) as above, it will be 1568 pixels (the image will maintain the aspect ratio, so the vertical width will change according to the horizontal width. To do).
You can also filter the view in image format.
# (optional) set a view filter
image_req_option.vf('Category', 'Furniture')
See also: ImageRequestOptions class
# -*- coding: utf-8 -*-
# Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
import tableauserverclient as TSC
#Credential input
username= 'input'
password= 'input'
siteid = 'input'
serverurl = 'input'
tableau_auth = TSC.TableauAuth(username, password, siteid)
server = TSC.Server(serverurl, use_server_version=True)
with server.auth.sign_in(tableau_auth):
all_views, pagination_item = server.views.get()
pdf_req_option = TSC.PDFRequestOptions(page_type=TSC.PDFRequestOptions.PageType.A4,
orientation=TSC.PDFRequestOptions.Orientation.Landscape)
with server.auth.sign_in(tableau_auth):
for view_item in all_views:
#Generate pdf image
server.views.populate_pdf(view_item, req_options=pdf_req_option)
with open(f'{view_item.name}.pdf', 'wb') as f:
f.write(view_item.pdf)
image_req_option = TSC.ImageRequestOptions(imageresolution=TSC.ImageRequestOptions.Resolution.High)
with server.auth.sign_in(tableau_auth):
for view_item in all_views:
#Generate png image
server.views.populate_image(view_item, req_options=image_req_option)
with open(f'{view_item.name}.png', 'wb') as f:
f.write(view_item.image)
Please note that images and pdfs of all views on the server are output in the same hierarchy as the above script. Please specify the output file as you like.
Tableau Server Client (Python) API reference Tableau REST API - API Reference—All Methods
Recommended Posts