def table(contents, heading=True, colw=None, cwunit='dxa', tblw=0, twunit='auto', borders={}, celstyle=None):
 table = makeelement('tbl')
 columns = len(contents[0])
 tableprops = makeelement('tblPr')
 tablestyle = makeelement('tblStyle', attributes={'val': ''})
 tableprops.append(tablestyle)
 tablewidth = makeelement('tblW', attributes={'w': str(tblw), 'type': str(twunit)})
 tableprops.append(tablewidth)
 if len(borders.keys()):
  tableborders = makeelement('tblBorders')
  for b in ['top', 'left', 'bottom', 'right', 'insideH', 'insideV']:
   if ((b in borders.keys()) or ('all' in borders.keys())):
    k = ('all' if ('all' in borders.keys()) else b)
    attrs = {}
    for a in borders[k].keys():
     attrs[a] = unicode(borders[k][a])
    borderelem = makeelement(b, attributes=attrs)
    tableborders.append(borderelem)
  tableprops.append(tableborders)
 tablelook = makeelement('tblLook', attributes={'val': '0400'})
 tableprops.append(tablelook)
 table.append(tableprops)
 tablegrid = makeelement('tblGrid')
 for i in range(columns):
  attrs = {'w': (str(colw[i]) if colw else '2390')}
  tablegrid.append(makeelement('gridCol', attributes=attrs))
 table.append(tablegrid)
 row = makeelement('tr')
 rowprops = makeelement('trPr')
 cnfStyle = makeelement('cnfStyle', attributes={'val': '000000100000'})
 rowprops.append(cnfStyle)
 row.append(rowprops)
 if heading:
  i = 0
  for heading in contents[0]:
   cell = makeelement('tc')
   cellprops = makeelement('tcPr')
   if colw:
    wattr = {'w': str(colw[i]), 'type': cwunit}
   else:
    wattr = {'w': '0', 'type': 'auto'}
   cellwidth = makeelement('tcW', attributes=wattr)
   cellstyle = makeelement('shd', attributes={'val': 'clear', 'color': 'auto', 'fill': 'FFFFFF', 'themeFill': 'text2', 'themeFillTint': '99'})
   cellprops.append(cellwidth)
   cellprops.append(cellstyle)
   cell.append(cellprops)
   if (not isinstance(heading, (list, tuple))):
    heading = [heading]
   for h in heading:
    if isinstance(h, etree._Element):
     cell.append(h)
    else:
     cell.append(paragraph(h, jc='center'))
   row.append(cell)
   i += 1
  table.append(row)
 for contentrow in contents[(1 if heading else 0):]:
  row = makeelement('tr')
  i = 0
  for content in contentrow:
   cell = makeelement('tc')
   cellprops = makeelement('tcPr')
   if colw:
    wattr = {'w': str(colw[i]), 'type': cwunit}
   else:
    wattr = {'w': '0', 'type': 'auto'}
   cellwidth = makeelement('tcW', attributes=wattr)
   cellprops.append(cellwidth)
   cell.append(cellprops)
   if (not isinstance(content, (list, tuple))):
    content = [content]
   for c in content:
    if isinstance(c, etree._Element):
     cell.append(c)
    else:
     if (celstyle and ('align' in celstyle[i].keys())):
      align = celstyle[i]['align']
     else:
      align = 'left'
     cell.append(paragraph(c, jc=align))
   row.append(cell)
   i += 1
  table.append(row)
 return table

def table ( contents , heading = True , colw = None , cwunit = 'dxa' , tblw = 0 , twunit = 'auto' , borders = { } , celstyle = None ) : table = makeelement ( 'tbl' ) columns = len ( contents [ 0 ] ) tableprops = makeelement ( 'tblPr' ) tablestyle = makeelement ( 'tblStyle' , attributes = { 'val' : '' } ) tableprops . append ( tablestyle ) tablewidth = makeelement ( 'tblW' , attributes = { 'w' : str ( tblw ) , 'type' : str ( twunit ) } ) tableprops . append ( tablewidth ) if len ( borders . keys ( ) ) : tableborders = makeelement ( 'tblBorders' ) for b in [ 'top' , 'left' , 'bottom' , 'right' , 'insideH' , 'insideV' ] : if ( ( b in borders . keys ( ) ) or ( 'all' in borders . keys ( ) ) ) : k = ( 'all' if ( 'all' in borders . keys ( ) ) else b ) attrs = { } for a in borders [ k ] . keys ( ) : attrs [ a ] = unicode ( borders [ k ] [ a ] ) borderelem = makeelement ( b , attributes = attrs ) tableborders . append ( borderelem ) tableprops . append ( tableborders ) tablelook = makeelement ( 'tblLook' , attributes = { 'val' : '0400' } ) tableprops . append ( tablelook ) table . append ( tableprops ) tablegrid = makeelement ( 'tblGrid' ) for i in range ( columns ) : attrs = { 'w' : ( str ( colw [ i ] ) if colw else '2390' ) } tablegrid . append ( makeelement ( 'gridCol' , attributes = attrs ) ) table . append ( tablegrid ) row = makeelement ( 'tr' ) rowprops = makeelement ( 'trPr' ) cnfStyle = makeelement ( 'cnfStyle' , attributes = { 'val' : '000000100000' } ) rowprops . append ( cnfStyle ) row . append ( rowprops ) if heading : i = 0 for heading in contents [ 0 ] : cell = makeelement ( 'tc' ) cellprops = makeelement ( 'tcPr' ) if colw : wattr = { 'w' : str ( colw [ i ] ) , 'type' : cwunit } else : wattr = { 'w' : '0' , 'type' : 'auto' } cellwidth = makeelement ( 'tcW' , attributes = wattr ) cellstyle = makeelement ( 'shd' , attributes = { 'val' : 'clear' , 'color' : 'auto' , 'fill' : 'FFFFFF' , 'themeFill' : 'text2' , 'themeFillTint' : '99' } ) cellprops . append ( cellwidth ) cellprops . append ( cellstyle ) cell . append ( cellprops ) if ( not isinstance ( heading , ( list , tuple ) ) ) : heading = [ heading ] for h in heading : if isinstance ( h , etree . _Element ) : cell . append ( h ) else : cell . append ( paragraph ( h , jc = 'center' ) ) row . append ( cell ) i += 1 table . append ( row ) for contentrow in contents [ ( 1 if heading else 0 ) : ] : row = makeelement ( 'tr' ) i = 0 for content in contentrow : cell = makeelement ( 'tc' ) cellprops = makeelement ( 'tcPr' ) if colw : wattr = { 'w' : str ( colw [ i ] ) , 'type' : cwunit } else : wattr = { 'w' : '0' , 'type' : 'auto' } cellwidth = makeelement ( 'tcW' , attributes = wattr ) cellprops . append ( cellwidth ) cell . append ( cellprops ) if ( not isinstance ( content , ( list , tuple ) ) ) : content = [ content ] for c in content : if isinstance ( c , etree . _Element ) : cell . append ( c ) else : if ( celstyle and ( 'align' in celstyle [ i ] . keys ( ) ) ) : align = celstyle [ i ] [ 'align' ] else : align = 'left' cell . append ( paragraph ( c , jc = align ) ) row . append ( cell ) i += 1 table . append ( row ) return table
restful crud controller for dynamic table contents nb: first argument is the resource name .