-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_parameters_twb.py
32 lines (25 loc) · 1.05 KB
/
extract_parameters_twb.py
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
import xml.etree.ElementTree as ET
from pathlib import Path
import pandas as pd
pd.set_option('display.max_rows', 1000)
in_folder = Path(r'/path/to/directory/for/twb/file')
infile = r'tableau_filename.twb' # Note: Must be .twb file, not .twbx file
outfile = r'parameters_from_tableau.xlsx'
tree = ET.parse(in_folder / infile)
root = tree.getroot()
rows = []
for datasources in root.findall('datasources'):
for datasource in datasources:
if datasource.attrib['name'] == 'Parameters':
for column in datasource:
try:
rows.append([column.attrib['caption'], column.attrib['value']])
except:
pass
out_df = pd.DataFrame(rows, columns=['Parameter', 'Value'])
# round values to 2 decimal places and drop non-numeric parameters
out_df['Value'] = pd.to_numeric(out_df['Value'], errors='coerce').round(decimals=2)
out_df = out_df[out_df['Value'].notnull()].reset_index(drop=True)
# output to excel and print to screen
out_df.to_excel(in_folder / outfile, index=None)
print(out_df)