Tables
Word tables are
sophisticated object hierarchies in their own right, and you can
manipulate them to any level of detail. However, there’s also
an AutoFormat
option for tables that works in a
similar way to styles. Here’s the full declaration:
Table.AutoFormat(Format,ApplyBorders,ApplyShading,ApplyFont,ApplyColor,ApplyHeadingRows,ApplyLastRow,ApplyFirstColumn,ApplyLastColumn,AutoFit)
All you have to do is insert a block of tab-delimited text with the
table contents, and call the method to convert text to a table, then
call the table’s
AutoFormat
method. Fortunately, almost all the
arguments are optional:
def addTable(self, table, styleid=None):
# Takes a 'list of lists' of data.
# first we format the text. You might want to preformat
# numbers with the right decimal places etc. first.
textlines = []
for row in table:
textrow = map(str, row) #convert to strings
textline = string.join(textrow, '\t')
textlines.append(textline)
text = string.join(textlines, '\n')
# add the text, which remains selected
self.wordSel.InsertAfter(text)
#convert to a table
wordTable = self.wordSel.ConvertToTable(Separator='\t')
#and format
if styleid:
wordTable.AutoFormat(Format=styleid)Unfortunately, to specify a style, you need to supply a numeric format constant instead of a name. If you are using MakePy, this is easy; an alternate approach is to use Word’s VB editor to look up the constants. Be warned: some constants vary across different language editions of Word.
Tables can be accessed ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access