Advertisement
Guest User

python code

a guest
Apr 27th, 2015
1,797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.83 KB | None | 0 0
  1. '''
  2. Created on 27/04/2015
  3. Extract filters from Tableau workbook files (TWB)
  4. @author: jeevananthamganesan
  5.  
  6. '''
  7. from xml.etree import ElementTree
  8.  
  9. twbFileName = "Your twb file"
  10.  
  11.  
  12. with open(twbFileName, 'rt') as f:
  13.     tree = ElementTree.parse(f)
  14.                
  15.        
  16. for i in tree.findall('./worksheets'):  
  17.     for worksheet in i:                 #For each worksheet in the workbook
  18.         print 'Worksheet name = ' + worksheet.attrib.get('name')  #Get the workbook name
  19.         print '\n'
  20.         for j in worksheet.iter('filter'): #For every filter in the workbook
  21.             print 'Filter on Field  = ' + j.attrib.get('column')   #Get the Field name in which filter is applied on
  22.             #for k in j.iter('groupfilter'): To get all child and grandchild
  23.             for k in j.findall('groupfilter'): #To get only the immediate child
  24.                 if k.attrib.get('function') == 'filter':    #To capture th conditional filters like sum(x) > 100 or wildcard filters like contains("USA")
  25.                     print 'Function used = ' + k.attrib.get('function')
  26.                     print 'Expression = ' + k.attrib.get('expression')
  27.                 elif k.attrib.get('function') == 'member':   #To capture  general filter with only one value included or excluded.
  28.                     print 'Function used = ' + k.attrib.get('{http://www.tableausoftware.com/xml/user}ui-enumeration') #Captures the type of filter used
  29.                     print k.attrib.get('level'), '=' +k.attrib.get('member')
  30.                 elif k.attrib.get('function') == 'union':   #To capture the filter with inclusive values
  31.                     if k.attrib.get('{http://www.tableausoftware.com/xml/user}ui-enumeration') != None:
  32.                         print 'Function used = ' + k.attrib.get('{http://www.tableausoftware.com/xml/user}ui-enumeration')
  33.                     else:
  34.                         print 'Function used = ' + k.attrib.get('function')
  35.                     for eachelem in k:
  36.                         if eachelem.attrib.get('member') != None:
  37.                                     print eachelem.attrib.get('level'), '=' +eachelem.attrib.get('member')
  38.                 elif k.attrib.get('function') == 'except':  #To capture the filter with exclusive values
  39.                     if k.attrib.get('{http://www.tableausoftware.com/xml/user}ui-enumeration') != None:
  40.                             print 'Function used = ' + k.attrib.get('{http://www.tableausoftware.com/xml/user}ui-enumeration')
  41.                     else:
  42.                             print 'Function used = ' + k.attrib.get('function')
  43.                     if j.attrib.get('kind') != None: #If this filter was built by right click and hide the values in worksheet
  44.                             print 'Kind = ' +j.attrib.get('kind')
  45.                     for eachelem in k.iter('groupfilter'):
  46.                         if eachelem.attrib.get('member') != None:
  47.                             print eachelem.attrib.get('level'), '=' +eachelem.attrib.get('member')
  48.                 elif k.attrib.get('function') == 'range': #If a range of values are selected
  49.                     print 'Function used = ' + k.attrib.get('function')
  50.                     print eachelem.attrib.get('level'), '--> ' +'From = ' + k.attrib.get('from'), ' to ' + k.attrib.get('to')
  51.                 elif k.attrib.get('function') == 'end': #To capture top or bottom  N % filter
  52.                     print 'Function used = ' + k.attrib.get('function')
  53.                     print (str(k.attrib.get('end'))).upper(),'' + k.attrib.get('count'), 'records'
  54.                 else:
  55.                     print 'Function used = ' + k.attrib.get('function') #To capture other functions which are not captured in the above conditions.
  56.                     print k.tag,k.attrib
  57.                 print '\n'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement