@@ -28,6 +28,7 @@ def list_files(dir):
28
28
r .append (subdir + "/" + file )
29
29
return r
30
30
31
+
31
32
def main ():
32
33
33
34
parser = argparse .ArgumentParser (description = 'Publish a workbook to server.' )
@@ -59,13 +60,13 @@ def main():
59
60
print ("Total file count: " + str (total ))
60
61
databases = csv .DictReader (csvfile , delimiter = ',' , quotechar = '"' )
61
62
for row in databases :
62
- count = count + 1
63
+
63
64
print ("working on item: " + str (count )+ " \n " )
64
65
# Open the workbook
65
66
source = "Source"
66
67
zip = "Zip"
67
68
export = "Export"
68
-
69
+ count = count + 1
69
70
70
71
if (row ['Format' ] == ".twbx" ):
71
72
originaltableauworkbook = zipfile .ZipFile (os .path .join (source , row ['Workbook' ] + ".twbx" ))
@@ -75,22 +76,22 @@ def main():
75
76
for item in test :
76
77
if item .endswith (".twb" ) :
77
78
sourceWB = Workbook (os .path .join (zip , item ))
78
-
79
- # Update the filters
80
79
for datasource in reversed (sourceWB .datasources ):
81
- for children in datasource ._datasourceTree ._root ._children :
80
+ # Update the filters
81
+ for children in datasource ._datasourceXML ._children :
82
82
if "column" in children .attrib and "class" in children .attrib :
83
83
if children .attrib ["column" ] == "[Branch]" and children .attrib ["class" ] == "categorical" :
84
84
for subchildren in children ._children :
85
85
if "member" in subchildren .attrib :
86
- subchildren .attrib ["member" ] = '"' + row ['Branch' ] + '"'
86
+ subchildren .attrib ["member" ] = '"' + row ['Branch' ] + '"'
87
+
87
88
88
89
# Save our newly created workbook with the new file name
89
90
outputpath = os .path .join (export , row ['Workbook' ] + ' - ' + row ['Branch' ] + ".twb" )
90
91
sourceWB .save_as (outputpath )
91
92
92
93
if (row ['Format' ] == ".twbx" ):
93
- z = zipfile .ZipFile (os .path .join (export , row ['Workbook' ] + ' - ' + row ['Branch' ] + row ['Format' ]),mode = 'w' )
94
+ z = zipfile .ZipFile (os .path .join (export , row ['Workbook' ] + ' - ' + row ['Branch' ] + row ['Format' ]),mode = 'w' , compression = zipfile . ZIP_DEFLATED )
94
95
z .write (os .path .join (export , row ['Workbook' ] + ' - ' + row ['Branch' ] + ".twb" ),item .replace ("Export" ,"" ))
95
96
for item in list_files (zip ):
96
97
if item .endswith (".twb" ) != True :
@@ -107,45 +108,33 @@ def main():
107
108
if children .attrib ["column" ] == "[Branch]" and children .attrib ["class" ] == "categorical" :
108
109
for subchildren in children ._children :
109
110
if "member" in subchildren .attrib :
110
- subchildren .attrib ["member" ] = '" ' + row ['Branch' ] + '" '
111
+ subchildren .attrib ["member" ] = '" ' + row ['Branch' ] + '" '
111
112
112
- # Save our newly created workbook with the new file name
113
- outputpath = os .path .join (export , row ['Workbook' ] + ' - ' + row ['Branch' ] + ".twb" )
114
- sourceWB .save_as (outputpath )
115
-
116
-
117
- all_projects , pagination_item = server .projects .get ()
118
- default_project = next ((project for project in all_projects if project .name == row ['Project' ]), None )
119
-
120
-
121
- connection = ConnectionItem ()
122
- connection .server_address = "10ay.online.tableau.com"
123
- connection .server_port = "443"
124
- connection .connection_credentials = ConnectionCredentials (args .username , password , True )
125
-
126
- all_connections = list ()
127
- all_connections .append (connection )
128
-
129
- if default_project is not None :
130
- new_workbook = TSC .WorkbookItem (default_project .id )
131
- if args .as_job :
132
- new_job = server .workbooks .publish (new_workbook ,os .path .join (export , row ['Workbook' ] + ' - ' + row ['Branch' ] + row ['Format' ]), overwrite_true )
133
- print ("Workbook published. JOB ID: {0}" .format (new_job .id ))
134
- else :
135
- new_workbook = server .workbooks .publish (new_workbook , os .path .join (export , row ['Workbook' ] + ' - ' + row ['Branch' ] + row ['Format' ]), overwrite_true )
136
- print ("Workbook published. ID: {0}" .format (new_workbook .id ))
113
+ # Save our newly created workbook with the new file name
114
+ outputpath = os .path .join (export , row ['Workbook' ] + ' - ' + row ['Branch' ] + ".twb" )
115
+ sourceWB .save_as (outputpath )
116
+
117
+
118
+ all_projects , pagination_item = server .projects .get ()
119
+ default_project = next ((project for project in all_projects if project .name == row ['Project' ]), None )
120
+
121
+ if default_project is not None :
122
+ new_workbook = TSC .WorkbookItem (default_project .id )
123
+ if args .as_job :
124
+ new_job = server .workbooks .publish (new_workbook ,os .path .join (export , row ['Workbook' ] + ' - ' + row ['Branch' ] + row ['Format' ]), overwrite_true )
125
+ print ("Workbook published. JOB ID: {0}" .format (new_job .id ))
137
126
else :
138
- error = "The default project could not be found."
139
- raise LookupError (error )
127
+ new_workbook = server .workbooks .publish (new_workbook , os .path .join (export , row ['Workbook' ] + ' - ' + row ['Branch' ] + row ['Format' ]), overwrite_true )
128
+ print ("Workbook published. ID: {0}" .format (new_workbook .id ))
129
+ else :
130
+ error = "The default project could not be found."
131
+ raise LookupError (error )
140
132
print ("Done!!!!!!!!!!!!!!!!!!" )
141
-
142
-
143
-
144
-
133
+
134
+
145
135
if __name__ == '__main__' :
146
136
main ()
147
137
148
138
149
139
150
140
151
-
0 commit comments