Examples#
Example 01 - Import CSV and Format Table#
The script imports on the Window
class of type table
with the method importCSV()
. The basic cell formatting like setting the background color, font sizes etc. is also done using the Window
class methods with cell references as arguments.
Conditional formatting dependent on the cell values is achieved by using the TableRule
class. Attributes used are operator
, textColor
, backgroundColor
and cellList
.
1# Import required modules
2import hw
3import hw.hg as hg
4import os
5
6# Link to csv from demo directory
7ALTAIR_HOME = os.path.abspath(os.environ['ALTAIR_HOME'])
8tableCsvFile = os.path.join(ALTAIR_HOME,'demos','report','data','mesh_statistics.csv')
9
10# Create new session and get window object
11ses = hw.Session()
12ses.new()
13tableWin = ses.get(hw.Window)
14
15# Switch to TableView Window
16tableWin.type = 'table'
17
18# Import csv file with ',' symbol as delimiter
19tableWin.importCSV(tableCsvFile,delimiter=',')
20
21# Autofit columns
22tableWin.setAutofitColumnWidth(['a','c:d'],True)
23
24# Format cell text and background colors dependent of filled cells
25lowRightCell=tableWin.getFilledLowerRightCell()
26uppLeftCell=tableWin.getFilledUpperLeftCell()
27tableWin.setCellBackgroundColor([uppLeftCell+':'+lowRightCell],(230,230,230))
28tableWin.setCellBackgroundColor(['a1:j1'],(0,0,0))
29tableWin.setCellTextColor('a1:j1','#ffffff')
30font=hw.Font(size=10,style='bold',family='Noto Sans')
31tableWin.setCellFont('a1:j1',font)
32tableWin.setCellAlignmentHorizontal([uppLeftCell+':'+lowRightCell],'center')
33
34# Add conditional formatting rules
35textColorList=[(0,0,0),(229,83,0),'#000000']
36bgColorList=[(0,255,0),(255,255,0),'#ff0000']
37valueList = [10,50,80]
38for (textColor,bgColor,value) in zip(textColorList,bgColorList,valueList):
39 # Add Rule
40 tableRule=hg.TableRule()
41 tableRule.textColor=textColor
42 tableRule.backgroundColor=bgColor
43 tableRule.cellList = ['b2:b13']
44 tableRule.operator='>='
45 tableRule.value = value
46# Refresh TableView to activate conditional formatting
47tableWin.refresh()

Figure 6. Output of ‘Import CSV and Format Table’