forked from tableau/server-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_wb.py
More file actions
101 lines (78 loc) · 3.14 KB
/
export_wb.py
File metadata and controls
101 lines (78 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
####
# This sample uses the PyPDF2 library for combining pdfs together to get the full pdf for all the views in a
# workbook.
#
# You will need to do `pip install PyPDF2` to use this sample.
#
# To run the script, you must have installed Python 3.6 or later.
####
import argparse
import logging
import tempfile
import shutil
import functools
import os.path
import tableauserverclient as TSC
try:
import PyPDF2
except ImportError:
print("Please `pip install PyPDF2` to use this sample")
import sys
sys.exit(1)
def get_views_for_workbook(server, workbook_id): # -> Iterable of views
workbook = server.workbooks.get_by_id(workbook_id)
server.workbooks.populate_views(workbook)
return workbook.views
def download_pdf(server, tempdir, view): # -> Filename to downloaded pdf
logging.info("Exporting {}".format(view.id))
destination_filename = os.path.join(tempdir, view.id)
server.views.populate_pdf(view)
with file(destination_filename, "wb") as f:
f.write(view.pdf)
return destination_filename
def combine_into(dest_pdf, filename): # -> None
dest_pdf.append(filename)
return dest_pdf
def cleanup(tempdir):
shutil.rmtree(tempdir)
def main():
parser = argparse.ArgumentParser(description="Export to PDF all of the views in a workbook.")
# Common options; please keep those in sync across all samples
parser.add_argument("--server", "-s", required=True, help="server address")
parser.add_argument("--site", "-S", help="site name")
parser.add_argument(
"--token-name", "-p", required=True, help="name of the personal access token used to sign into the server"
)
parser.add_argument(
"--token-value", "-v", required=True, help="value of the personal access token used to sign into the server"
)
parser.add_argument(
"--logging-level",
"-l",
choices=["debug", "info", "error"],
default="error",
help="desired logging level (set to error by default)",
)
# Options specific to this sample
parser.add_argument("--file", "-f", default="out.pdf", help="filename to store the exported data")
parser.add_argument("resource_id", help="LUID for the workbook")
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)
tempdir = tempfile.mkdtemp("tsc")
logging.debug("Saving to tempdir: %s", tempdir)
try:
tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site)
server = TSC.Server(args.server, use_server_version=True)
with server.auth.sign_in(tableau_auth):
get_list = functools.partial(get_views_for_workbook, server)
download = functools.partial(download_pdf, server, tempdir)
downloaded = (download(x) for x in get_list(args.resource_id))
output = reduce(combine_into, downloaded, PyPDF2.PdfFileMerger())
with file(args.file, "wb") as f:
output.write(f)
finally:
cleanup(tempdir)
if __name__ == "__main__":
main()