Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Created on 27/04/2015
- Extract filters from Tableau workbook files (TWB)
- @author: jeevananthamganesan
- '''
- from xml.etree import ElementTree
- twbFileName = "Your twb file"
- with open(twbFileName, 'rt') as f:
- tree = ElementTree.parse(f)
- for i in tree.findall('./worksheets'):
- for worksheet in i: #For each worksheet in the workbook
- print 'Worksheet name = ' + worksheet.attrib.get('name') #Get the workbook name
- print '\n'
- for j in worksheet.iter('filter'): #For every filter in the workbook
- print 'Filter on Field = ' + j.attrib.get('column') #Get the Field name in which filter is applied on
- #for k in j.iter('groupfilter'): To get all child and grandchild
- for k in j.findall('groupfilter'): #To get only the immediate child
- if k.attrib.get('function') == 'filter': #To capture th conditional filters like sum(x) > 100 or wildcard filters like contains("USA")
- print 'Function used = ' + k.attrib.get('function')
- print 'Expression = ' + k.attrib.get('expression')
- elif k.attrib.get('function') == 'member': #To capture general filter with only one value included or excluded.
- print 'Function used = ' + k.attrib.get('{http://www.tableausoftware.com/xml/user}ui-enumeration') #Captures the type of filter used
- print k.attrib.get('level'), '=' +k.attrib.get('member')
- elif k.attrib.get('function') == 'union': #To capture the filter with inclusive values
- if k.attrib.get('{http://www.tableausoftware.com/xml/user}ui-enumeration') != None:
- print 'Function used = ' + k.attrib.get('{http://www.tableausoftware.com/xml/user}ui-enumeration')
- else:
- print 'Function used = ' + k.attrib.get('function')
- for eachelem in k:
- if eachelem.attrib.get('member') != None:
- print eachelem.attrib.get('level'), '=' +eachelem.attrib.get('member')
- elif k.attrib.get('function') == 'except': #To capture the filter with exclusive values
- if k.attrib.get('{http://www.tableausoftware.com/xml/user}ui-enumeration') != None:
- print 'Function used = ' + k.attrib.get('{http://www.tableausoftware.com/xml/user}ui-enumeration')
- else:
- print 'Function used = ' + k.attrib.get('function')
- if j.attrib.get('kind') != None: #If this filter was built by right click and hide the values in worksheet
- print 'Kind = ' +j.attrib.get('kind')
- for eachelem in k.iter('groupfilter'):
- if eachelem.attrib.get('member') != None:
- print eachelem.attrib.get('level'), '=' +eachelem.attrib.get('member')
- elif k.attrib.get('function') == 'range': #If a range of values are selected
- print 'Function used = ' + k.attrib.get('function')
- print eachelem.attrib.get('level'), '--> ' +'From = ' + k.attrib.get('from'), ' to ' + k.attrib.get('to')
- elif k.attrib.get('function') == 'end': #To capture top or bottom N % filter
- print 'Function used = ' + k.attrib.get('function')
- print (str(k.attrib.get('end'))).upper(),'' + k.attrib.get('count'), 'records'
- else:
- print 'Function used = ' + k.attrib.get('function') #To capture other functions which are not captured in the above conditions.
- print k.tag,k.attrib
- print '\n'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement